通过 apc / memcache / eaccelerator 中的前缀删除缓存
假设我将这些变量保存在 apc、memcached 和 eaccelerator 中:
article_1_0
article_1_1
article_3_2
article_3_3
article_2_4
如何删除以 article_3_
开头的所有缓存变量(最多可达 10000 个)?
有没有办法列出缓存的变量?
Let's assume I have these variables saved in apc, memcached and eaccelerator:
article_1_0
article_1_1
article_3_2
article_3_3
article_2_4
How can I delete all cached variables that starts with article_3_
(they can reach up to 10000) ?
is there any way to list the cached variables ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对于APC
:
对于 eaccelerator:
对于 memcached,请查看 @rik 的回答
正确的解决方案
一次使多个密钥过期的一般解决方案是对它们进行命名。要使它们过期,您只需更改命名空间:
假设您有一组键“article_3_1”,“article_3_2”,...。您可以像这样存储它们:
像这样获取它们:
并通过仅递增来使它们全部过期命名空间:
The slow solution
For APC:
For eaccelerator:
For memcached, look at @rik's answer
The proper solution
The general solution for expiring multiple keys at once is to namespace them. For expiring them, you just have to change the namespace:
Say you have a group of keys "article_3_1", "article_3_2", .... You can store them like this:
Fetch them like this:
And expire them all by just incrementing the namespace:
尽管文档说 APCIterator 在 apc >= 3.1.1 中可用,但我在几个声称拥有 apc 3.1.9 的系统上,但是没有 APCIterator 存在。如果您没有可用的 APCIterator,请尝试一下类似的操作:
在本示例中,我们将检查密钥中的前缀,但您可以使用 preg_match 等。 al 并实现更接近 APCIterator 提供的功能。
Although the docs say APCIterator is available in apc >= 3.1.1, I'm on several systems that claim to have apc 3.1.9, however there is no APCIterator present. If you don't have APCIterator at your disposal, give something like this a whirl:
In this example we're checking for a prefix in the key, but you could use preg_match et. al and achieve something closer to what APCIterator provides.
有一种方法可以从内存缓存中检索所有密钥,但它非常昂贵的。
There is a way to retrieve all keys from memcache but it's very expensive.
如果可以使用 memcached 的替代品,scache 支持结构化键空间。有了它,您可以将数据存储到嵌套路径:
并最终通过删除父节点来销毁数据
If there is possibility to use alternatives for memcached, scache supports structured keyspaces. With it you could store data to nested paths :
and eventually destroy data by deleting the parent node
有一个 APCIterator 可以帮助您搜索 APC 中的键。
实例化 APCIterator。
APCIterator::valid() 意味着仍有键需要迭代。 APCIterator::key() 返回 apc 密钥。 APCIterator::next() 将迭代器位置移动到下一项。
对于 memcache,您可以使用 Memcached 并使用 getAllKeys 方法
There is an APCIterator which helps you search through the keys in APC.
Instantiate the APCIterator.
APCIterator::valid() means that there are keys still to iterate trough. APCIterator::key() returns you the apc key. APCIterator::next() moves the iterator position to the next item.
For memcache you can use Memcached and use getAllKeys method