1

不确定这是 django 问题还是 python 问题。我试图通过从返回多个值的函数中接受值来使缓存工作。

目前页面在heavy_view函数上给了我这个错误:“'NoneType'对象不可迭代”

有没有办法让“counter,college_user,name_college_list = cache.get(cache_key)”行工作?然后检查它是否为空。谢谢!

 #Views.py

 def filter_results():
     #some code here

     return counter, college_user, name_college_List    


 def heavy_view(request):
     cache_key = 'facebookcache'
     cache_time = 180 # time to live in seconds
     counter, college_user, name_college_list = cache.get(cache_key)
     if not cache.get(cache_key):
         result = filter_results() # some calculations here
         cache.set(cache_key, result, cache_time)
     return result
4

2 回答 2

0

这是一个 Python 的东西

当你这样做时

counter, college_user, name_college_list = cache.get(cache_key)

第一次没有值存在 ->None被返回。这行得通吗?

result = cache.get(cache_key)
if result is None:
    result = filter_results()
    cache.set(cache_key, result, cache_time)
else:
    counter, college_user, name_college_list = result
return result
于 2012-08-05T22:36:45.643 回答
0

你可以这样做:

result = cache.get(cache_key)
if not result:
    result = filter_result()
    cache.set(cache_key, result, cache_time)

counter, college_user, name_college_list = result
于 2012-08-05T22:38:53.417 回答