0

我正在尝试学习如何使用 memcache 来减少数据库的负载。所以我有一个cacheData函数,当给定一个键时缓存博客文章,在这种情况下是“条目”。问题是,当我在博客上发布新帖子并进行数据库查询时,尽管新帖子已添加到数据库中,但它没有返回任何值。为什么我的数据库查询后没有返回条目?

缓存数据函数:

def cacheData(key, update = False):
     value = memcache.get(key)
     if value is None or update:
         logging.error("DATABASE HIT")
         value = db.GqlQuery("select * from Entry order by date desc")
         value = list(value) #returns 0 when entry is added...
         logging.error("value has: " + str(len(value)) + " items")
         memcache.set(key,value)
     return value

这是在数据库中输入数据的方法:

def post(self):

    error = "You are missing a title or blog entry!"
    title = self.request.get("subject")
    blog = self.request.get("content")

    if (title and blog):
        blogEntry = Entry(title = title, blogText = blog)
        blogEntry.put()
        cacheData("entries", update = True)
        self.redirect("/blog")

    else:
        self.renderPostPage(title = title, blog = blog, error = error)
4

1 回答 1

0

这可能是最终的一致性问题。我很好奇,您的 cacheData 只尝试从 memcache 中获取一个条目,而您确实选择 * (获取所有实体)。我相信如果您尝试通过密钥获取,它将是强一致的。另一种解决方法是在调用 .put() 以将数据持久保存在数据存储区之后,同时更新内存缓存。

于 2013-04-21T21:44:45.700 回答