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
Using jldupont's answer I put the following code in my main request handling script...
使用jldupont的答案我将以下代码放在我的主要请求处理脚本中......
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.
我已经在所选答案的注释中切换到Koen Bok提到的方法,并使用os.environ ['CURRENT_VERSION_ID'] /将所有我的memcache键作为答案的第二次更新中的有用代码。这个解决方案似乎比我之前发布的功能更优雅。
4 个解决方案
#1
21
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_ID can 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"]
#2
3
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编码键值适用于memcached。
#3
2
I have not tested this but perhaps if you insert into memcache a key with version # on instance start.
我没有测试过这个,但是如果你在memcache中插入一个带有版本#的实例启动的密钥。
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.
然后当下一个实例启动时,也就是在部署之后,它将检查memcache及其本地版本,如果它们不同,则刷新所有并重新启动密钥。
Only flaw is what if the key is evicted, could replace memcache to datastore but then your making datastore calls for every instance start.
如果密钥被逐出,只有缺陷是可以将memcache替换为数据存储区,然后你的数据存储区调用每个实例启动。
=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"])
#4
0
You could just create an admin-only path that would flush the cache when it's accessed.
您可以创建一个仅限管理员的路径,以便在访问缓存时刷新缓存。
#1
21
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_ID can 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"]
#2
3
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编码键值适用于memcached。
#3
2
I have not tested this but perhaps if you insert into memcache a key with version # on instance start.
我没有测试过这个,但是如果你在memcache中插入一个带有版本#的实例启动的密钥。
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.
然后当下一个实例启动时,也就是在部署之后,它将检查memcache及其本地版本,如果它们不同,则刷新所有并重新启动密钥。
Only flaw is what if the key is evicted, could replace memcache to datastore but then your making datastore calls for every instance start.
如果密钥被逐出,只有缺陷是可以将memcache替换为数据存储区,然后你的数据存储区调用每个实例启动。
=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"])
#4
0
You could just create an admin-only path that would flush the cache when it's accessed.
您可以创建一个仅限管理员的路径,以便在访问缓存时刷新缓存。