0

我在 Google App Engine (Python) 中使用 Memcache 已经有一段时间了,通常它工作得很好。在过去的几天里,虽然我注意到使用下面示例中的一些代码,当我更新数据库条目后立即更新它时,它没有及时得到它。这是由于将条目存储在数据库中所需的时间长度吗?有什么解决办法吗?

# I store the comment here 
c = Comment(content = content, submitter = submitter, group_id=int(group_id), user_id = user_id)
c.put()
# Right after I store the comment I refresh the cache
comment_cache(int(group_id), True)

通常最新的评论不在缓存中。

4

2 回答 2

2

由于最终的一致性,如果comment_cache()运行查询(即,不通过键获取),那么您所描述的就是预期的。

一些解决方案:

  1. 更改comment_cache()c作为参数,以便它明确知道它:comment_cache(int(group_id), True, c).
  2. comment_cache()在任务队列中运行。仍然不能保证它会接收新评论,但由于它会在一段时间后运行,它可能会。
于 2012-11-06T06:09:18.797 回答
1

我和你有同样的问题。

当我在我的数据库中添加一个值时,我更新了我的缓存,但由于查询需要很长时间才能运行,我最后插入的值没有插入到缓存中。

我的解决方案:我有一个更新缓存的函数,现在我添加了我想放入数据库中的值作为参数:

def get_values_from_cache_or_database(particular_value = None, update = True):
  key = 'my_key'

  values = memcache.get(key)
    if values is None or update:
      values = db.GqlQuery("SELECT * FROM Table")
      values = list(values)
      if update:
        if particular_value not in values:
          # if we are here, particular_value isn't in your data base (because time 
          # request is  long) but we want the particular_value in the cache so we add 
          # it manually  
          values.append(particular_value)
  memcache.set(key, values)
  return values

因此,例如,我们像这样输入一个值“value1”: value1.put() 我们调用此函数以“value1”作为参数刷新缓存: get_values_from_cache_or_database(value1, True) 然后我们将获取最新添加的缓存!

于 2013-05-06T05:29:02.893 回答