为什么 appengine memcache 没有在请求的时间段内存储我的数据?

Why is appengine memcache not storing my data for the requested period of time?

我将我的员工存储在 appengine ndb 中,我正在 运行 通过 taskque 执行 cron 作业以生成包含每个员工电子邮件地址的字典列表。结果列表看起来像这样:

[{"text":"john@mycompany.com"},{"text":"mary@mycompany.com"},{"text":"paul@mycompany.com"}]

该列表用作 varing angular 组件(例如 ngTags ngAutocomplete 等)的源数据。我想将列表存储在内存缓存中,这样 Angular http 调用会 运行 更快.

我遇到的问题是存储在 memcache 中的值永远不会持续超过几分钟,即使我已将其设置为持续 26 小时。我知道存储的实际值不能超过 1mb,因此作为实验,我将员工列表硬编码为仅包含三个值,但问题仍然存在。

appengine 控制台告诉我作业 运行 成功,如果我手动 运行 作业,它会将值加载到内存缓存中,但它们只会在那里停留几分钟。我以前用更多的数据做过很多次,所以我不明白出了什么问题。我启用了计费功能并且没有超过配额。

以下是用于将数据加载到内存缓存中的函数示例:

def update_employee_list():
try:
    # Get all 3000+ employees and generate a list of dictionaries
    fresh_emp_list = [{"text":"john@mycompany.com"},{"text":"mary@mycompany.com"},{"text":"paul@mycompany.com"}]

    the_cache_key = 'my_emp_list'
    emp_data = memcache.get(the_cache_key)

    # Kill the memcache packet so we can rebuild it.
    if emp_data is not None:
        memcache.delete(the_cache_key)

    # Rebuild the memcache packet      
    memcache.add(the_cache_key, fresh_emp_list, 93600) # this should last for 26 hours  

except Exception as e:
    logging.info('ERROR!!!...A failure occured while trying to setup the memcache packet: %s'%e.message)
    raise deferred.PermanentTaskFailure() 

这里是 angular 组件用来从内存缓存中获取数据的函数示例:

@route
def get_emails(self):
    self.meta.change_view('json')
    emp_emails = memcache.get('my_emp_list')
    if emp_emails is not None:
        self.context['data'] = emp_emails
    else:
        self.context['data'] = [] 

这是 cron.yaml 中的 cron 设置示例:

- url: /cron/lookups/update_employee_list
  description: Daily rebuild of Employee Data
  schedule: every day 06:00
  timezone: America/New_York 

为什么 appengine memcache 不能保存三个字典的列表超过几分钟?

欢迎任何想法。谢谢

除非您使用专用的内存缓存(付费服务),否则缓存的值可以并且将随时被逐出。

您通过指定生命周期告诉 memcache 什么时候您的值变得无效,因此可以从 memcache 中删除。然而,这并不能保证您的值会在内存缓存中保留那么久,它只是对缓存值最长生命周期的限制。

注意: 放入 memcache 的越多,其他值被丢弃的可能性就越大。因此,您应该仔细考虑将哪些数据放入缓存中。你绝对不应该把你遇到的每一个值都放在内存缓存中。

旁注: 在我最近工作的项目中,我们有一个 - 大约一天的 - 最长缓存生命周期。没有任何缓存值比它持续时间更长,即使所需的生命周期要长得多。有趣的是,缓存每天大约在同一时间被清除,甚至包括非常新的值。

因此:永远不要依赖内存缓存。始终使用持久存储和内存缓存来提高高流量的性能。