【问题标题】:Why is appengine memcache not storing my data for the requested period of time?为什么 appengine memcache 没有在请求的时间段内存储我的数据?
【发布时间】: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 不能在包含三个字典的列表中保持几分钟以上?

任何想法都值得赞赏。谢谢

【问题讨论】:

标签: python google-app-engine cron memcached task-queue


【解决方案1】:

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

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

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

附带说明: 在我最近参与的项目中,我们的缓存寿命最长约为一天。即使期望的生命周期要高得多,也没有任何缓存值能持续更长时间。有趣的是,尽管缓存每天大约在同一时间被清除,甚至包括非常新的值。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-07
    • 1970-01-01
    • 2015-06-22
    • 1970-01-01
    • 2021-08-14
    • 2019-08-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多