我从此代码中收到以下错误:
def render(template, **kw):
if not cache.has("galleries"):
cache.set('galleries', getTable(Gallery))
return render_template(template, galleries=galleries, **kw)
错误:
File "/vagrant/diane/diane.py", line 38, in render
if cache.has("galleries"):
AttributeError: 'SimpleCache' object has no attribute 'has'
之前我曾多次使用相同的代码而没有任何问题。我也复制了这个并运行了一个简单的测试,它可以工作
from werkzeug.contrib.cache import SimpleCache
cache = SimpleCache()
def x():
if cache.has('y'):
print 'yes'
print cache.get("y")
else:
print 'no'
x()
任何想法都会非常感激。
答案 0 :(得分:3)
从@JacobIRR的评论中,很明显它是一个可选字段。
文档说如下:
has(key)检查缓存中是否存在密钥而不返回密钥。 这是一个廉价的操作,绕过加载实际数据 后端。
此方法是可选的,可能无法在所有缓存上实现。
参数:key - 要检查的键
为避免这种情况,我们可以使用get(key)
方法
get(key)在缓存中查找密钥并返回其值。
参数:key - 要查找的键。返回:如果它的值 存在且可读,否则无。 来自werkzeug.contrib.cache导入SimpleCache cache = SimpleCache()
以下是我们使用get(key)
:
from werkzeug.contrib.cache import SimpleCache
cache = SimpleCache()
def x():
if cache.get("y"): # if 'y' is not present it will return None.
print 'yes'
else:
print 'no'
x()