class MainHandler(BaseHandler.Handler):
def get(self):
user = users.get_current_user() or "unknown"
posts = memcache.get('posts_%s' % user.user_id())
if not posts:
q = db.Query(P.Post)
q.filter('author =', users.get_current_user()).order("-date")
posts=q.fetch(5)
memcache.set(key='posts_%s:'%user.user_id(),value=posts)
#q=P.Post.all().filter('user =',users.get_current_user())
self.render('index.html', user=user, posts=posts)
def post(self):
user = users.get_current_user() or "unknown"
author = users.get_current_user()
title = self.request.get('title')
content = self.request.get('content')
p = P.Post(author=author, title=title, content=content)
p.put()
res = memcache.get('posts_%s'%users.get_current_user().user_id())
if res:
res+=p
if len(res)>5:
res=res[1:]
else:
res=[p]
memcache.replace("posts_%s"%user.user_id(),value=res)
self.redirect('/')
当浏览器重定向到“/”时,最后添加的项目不在列表中(仅在重新加载后添加)。只有当我在development server上时才会发生这种情况(在GAE上它可以正常工作),我想知道它是否可以在GAE上发生,这段代码有什么问题
如有任何建议,我们将不胜感激。你知道吗
你知道吗UPD:泰铢,我做了相同的钥匙,但问题仍然存在
当您替换时,可能该项不在memcache中。在这种情况下,为什么使用replace?有没有不使用的理由memcache.set?在get函数中,还有一个地方的键是%s:这与其他地方不同。你知道吗
你一点也不想在这里碰到memcache。在
post
和get
方法中使用了不同的密钥格式:在get
中使用“posts\u user”,而在post
中使用“posts\u user”用户:帖子,因此该键永远找不到,您将进入db查询。当然,由于最终的一致性,查询不是最新的,这可能是您首先使用memcache的全部原因。你知道吗修复memcache密钥,这应该可以。你知道吗
相关问题 更多 >
编程相关推荐