python 每次部署站点时,如何让 Google App Engine 清除内存缓存?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1983556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:29:23  来源:igfitidea点击:

How can I have Google App Engine clear memcache every time a site is deployed?

pythongoogle-app-enginedeploymentcachingmemcached

提问by donut

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.

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

Update 1

更新 1

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

使用jldupont回答,我将以下代码放入我的主请求处理脚本中...

Update 2

更新 2

I've switched to the method mentioned by Koen Bokin 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.

我已经切换到Koen Bok在所选答案的评论中提到的方法,os.environ['CURRENT_VERSION_ID']/并在我的所有内存缓存键前加上了答案的第二次更新中的有用代码。这个解决方案似乎比我之前发布的函数优雅得多。

采纳答案by jldupont

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.

你试过flush_all()函数吗?文档在这里。您需要一些逻辑和状态来检测新部署或使用特殊脚本来执行刷新。

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:

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

  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/']

['/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/版本/1', '/base/data/home/apps/shell/1.335852500710379686/']

Look at the line with /shell/1.335852500710379686/.

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

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

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

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

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

 import os
 os.environ["CURRENT_VERSION_ID"]

回答by Matt

When creating keys for your cached values, include the version of the file that is doing the cache gets/sets in the key. That way, when a new version of the file exists, it will no longer reference the old versions in the cache - they will be left to expire out on their own.

为缓存值创建键时,请在键中包含执行缓存获取/设置的文件版本。这样,当文件的新版本存在时,它将不再引用缓存中的旧版本——它们将自行过期。

We use CVS and java, so we declare this variable in each file that will do caching:

我们使用 CVS 和 java,所以我们在每个将进行缓存的文件中声明这个变量:

private static final String CVS_REVISION = "$Revision $";

When you check that file out, you'll get something like this:

当您检出该文件时,您将得到如下信息:

private static final String CVS_REVISION = "$Revision: 1.15 $";

You can adapt for your language and version control system if not CVS. Remember to encode special characters out of your keys. We've found that URL Encoding key values works well for memcached.

如果不是 CVS,您可以适应您的语言和版本控制系统。请记住从您的密钥中编码特殊字符。我们发现 URL Encoding 键值适用于 memcached。

回答by bdavenport

I have not tested this but perhaps if you insert into memcache a key with version # on instance start.

我还没有测试过这个,但也许如果你在实例启动时将一个版本号为 # 的密钥插入到内存缓存中。

Then when the next instance is started, aka after a deployment, it would check memcache and its local version, if they differ, flush all and re-initalize the key.

然后当下一个实例启动时,也就是在部署之后,它会检查内存缓存及其本地版本,如果它们不同,则刷新所有并重新初始化密钥。

Only flaw is what if the key is evicted, could replace memcache to datastore but then your making datastore calls for every instance start.

唯一的缺陷是,如果键被驱逐,可以将内存缓存替换为数据存储,但随后您会为每个实例启动调用数据存储。

=edit=

=编辑=

Add to the top of your called python files from app.yaml

从 app.yaml 添加到您调用的 python 文件的顶部

# Check if the version is updated
if memcache.get("static-version") == os.environ["CURRENT_VERSION_ID"]:
    pass
else:
    memcache.flush_all()
    memcache.set(key="static-version", value=os.environ["CURRENT_VERSION_ID"])

回答by donut

You could just create an admin-only path that would flush the cache when it's accessed.

您可以创建一个仅限管理员的路径,在访问时刷新缓存。