0

我正在查询我的数据库以获取商店列表,并希望将此列表保存在 memcache 中,因为它在每个页面上都重复使用并且实际上不会更改。代码对我来说似乎很简单,但它给了我以下错误:

Traceback (most recent call last):
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1546, in __call__
return response(environ, start_response)
TypeError: 'Query' object is not callable

我的代码是:

    shops = memcache.get('shops')
    if shops is not None:
        return shops
    else:
        shops =  Shop.all().filter('active = ', True).order('abbrev')
        memcache.set('shops', shops)
        return shops

任何建议将不胜感激。

更新

如果我解释如何使用它的输出,也许会有所帮助。然后我在活动模板中使用 Django 调用此操作的结果,使用 {{ for shop in stores }}。也许我需要在设置 memchache 后再次声明“shops”变量?

更新2

然后将变量传递给模板,如下所示:

    template_values = {
      'shops': shops,
    }
    self.response.out.write(template.render('templates/home.html', template_values))
4

2 回答 2

3

使用以下方法缓存查询结果,而不是对实际查询对象进行缓存:

# this will cache the first 50 results
memcache.set('shops', shops.fetch(50)) 

或者

# this will cache all (not recommended if your set is very big)
memcache.set('shops', list(shops)) 

去除多余的,在商店里

template_values = {
  'shops': shops
}
self.response.out.write(template.render('templates/home.html', template_values))
于 2013-02-25T19:17:54.603 回答
0

问题似乎来自“退货商店”电话,说实话我不知道附加值。如果我像下面这样重写代码,错误就消失了。有什么方法可以检查 memcache 是否真的在工作?

shops = memcache.get('shops')
    if shops is not None:
        shops = memcache.get('shops')
    else:
        allshops =  Shop.all().filter('active = ', True).order('abbrev')
        memcache.set('shops', list(allshops))
        shops = memcache.get('shops')
于 2013-02-25T20:39:58.257 回答