سؤال

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

Any suggestions would be highly appreciated.

UPD:thx,I made keys the same,but the problem still remains

هل كانت مفيدة؟

المحلول

You're not hitting memcache at all here. 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. 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? In the get function, there is still one place where the key is posts_%s: which is different than the others.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top