每次部署站点时,如何让 Google App Engine 清除内存缓存? [英] How can I have Google App Engine clear memcache every time a site is deployed?

查看:27
本文介绍了每次部署站点时,如何让 Google App Engine 清除内存缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题说明了一切.我正在构建的站点上的内容根本不会很快发生变化,因此 Memcache 可能会将数据存储数月,除非我发布更新.有没有办法在每次部署站点时清除缓存?我正在使用 Python 运行时.

更新 1

使用 jldupontanswer 我将以下代码放在我的主要请求中处理脚本...

更新 2

我已切换到所选答案的评论中 Koen Bok 提到的方法,并为我的所有内存缓存添加前缀带有 os.environ['CURRENT_VERSION_ID']/ 的键以及 answer 的第二次更新.这个解决方案似乎比我之前发布的函数优雅得多.

解决方案

你试过 flush_all() 函数吗?此处的文档.你需要一点逻辑&状态以检测新部署或使用特殊脚本执行刷新.

更新:查看您的脚本之一的绝对路径:这在每次部署时都会发生变化.您可以使用 http://shell.appspot.com/ 进行实验:

 导入系统系统路径

<块引用>

['/base/python_dist/lib/python25.zip','/base/python_lib/versions/third_party/django-0.96','/base/python_dist/lib/python2.5/','/base/python_dist/lib/python2.5/plat-linux2','/base/python_dist/lib/python2.5/lib-tk','/base/python_dist/lib/python2.5/lib-dynload','/base/python_lib/versions/1','/base/data/home/apps/shell/1.335852500710379686/']

查看带有 /shell/1.335852500710379686/ 的行.

因此,只需保留此部署状态变量的快照(在内存缓存中;-)并进行比较以实现刷新操作.

更新 2:根据@Koen Bok 的建议,也可以使用环境变量 CURRENT_VERSION_ID(脚本文件的绝对路径的一部分).

 导入操作系统os.environ["CURRENT_VERSION_ID"]

The title asks it all. The content on the site I'm building wont change very quickly at all and so Memcache could potentially store data for months except for when I put up an update. Is there a way to make it clear the cache every time I deploy the site? I'm using the Python runtime.

Update 1

Using jldupont's answer I put the following code in my main request handling script...

Update 2

I've switched to the method mentioned by Koen Bok in the selected answer's comments and prefixed all my memcache keys with os.environ['CURRENT_VERSION_ID']/ with the helpful code in the answer's 2nd update. This solution seems to be much more elegant than the function I posted before.

解决方案

Have you tried flush_all() function? Docs here. You'll need a bit of logic & state to detect a new deployment or have a special script to perform the flushing.

Updated: look at the absolute path of one of your script: this changes on every deployment. You can use http://shell.appspot.com/ to experiment:

  import sys
  sys.path

['/base/python_dist/lib/python25.zip', '/base/python_lib/versions/third_party/django-0.96', '/base/python_dist/lib/python2.5/', '/base/python_dist/lib/python2.5/plat-linux2', '/base/python_dist/lib/python2.5/lib-tk', '/base/python_dist/lib/python2.5/lib-dynload', '/base/python_lib/versions/1', '/base/data/home/apps/shell/1.335852500710379686/']

Look at the line with /shell/1.335852500710379686/.

So, just keep a snapshot (in memcache ;-) of this deployment state variable and compare in order to effect a flushing action.

Updated 2: as suggested by @Koen Bok, the environment variable CURRENT_VERSION_ID can be used also (part of the absolute path to script files also).

 import os
 os.environ["CURRENT_VERSION_ID"]

这篇关于每次部署站点时,如何让 Google App Engine 清除内存缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆