【发布时间】:2016-02-05 13:55:13
【问题描述】:
我将我的员工存储在 appengine ndb 中,并且我正在通过 taskque 运行一项 cron 作业以生成包含每个员工的电子邮件地址的字典列表。结果列表如下所示:
[{"text":"john@mycompany.com"},{"text":"mary@mycompany.com"},{"text":"paul@mycompany.com"}]
该列表用作各种角度组件的源数据,例如 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 组件用于从 memcache 获取数据的函数示例:
@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 不能在包含三个字典的列表中保持几分钟以上?
任何想法都值得赞赏。谢谢
【问题讨论】:
-
您是否尝试过使用专用的 memcached 而不是共享的?
标签: python google-app-engine cron memcached task-queue