简体   繁体   English

Memcached无法按时更新

[英]Memcached doesn't update on time

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('/')

When the browser redirects to '/' the last added item isn't in the list(it is added only after reloading). 当浏览器重定向到'/'时,最后添加的项目不在列表中(仅在重新加载后才添加)。 This happens only when I am on development server(on GAE it works OK),and I wonder if it can happen on GAE and what's the problem with this code 仅当我在开发服务器上时才会发生这种情况(在GAE上可以正常工作),我想知道它是否可以在GAE上发生,并且此代码有什么问题

Any suggestions would be highly appreciated. 任何建议将不胜感激。

UPD:thx,I made keys the same,but the problem still remains UPD:thx,我做了相同的键,但是问题仍然存在

You're not hitting memcache at all here. 您根本没有在这里使用memcache。 You're using a different key format in the post and get methods: in get you use "posts_user" whereas in post you use "user:posts", so the key is never found and you fall through to the db query. 您在postget方法中使用了不同的键格式:在get使用“ posts_user”,而在post使用“ user:posts”,因此永远找不到该键,您将进入db查询。 And, of course, the query is not up to date because of eventual consistency, which is presumably the whole reason you're using memcache in the first place. 而且,当然,由于最终的一致性,查询不是最新的,这大概就是您首先使用内存缓存的全部原因。

Fix your memcache keys and this should work. 修复您的内存缓存键,这应该可以工作。

maybe the item is not in memcache when you do replace. 替换时,该项目可能不在内存缓存中。 Why do you use replace in this case? 为什么在这种情况下使用替换? Any reason not to use memcache.set? 有什么理由不使用memcache.set吗? In the get function, there is still one place where the key is posts_%s: which is different than the others. 在get函数中,仍有一个地方的键是posts_%s:这与其他地方不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM