本文介绍了在apc/memcache/eaccelerator中按前缀删除缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我将这些变量保存在apc,memcached和eaccelerator中:

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
  • article_1_0
  • article_1_1
  • article_3_2
  • article_3_3
  • article_2_4

如何删除以article_3_开头的所有缓存变量(它们最多可以达到10000)?

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 ?

推荐答案

慢速解决方案

对于APC:

The slow solution

For APC:

$iterator = new APCIterator('user', '#^article_3_#', APC_ITER_KEY);
foreach($iterator as $entry_name) {
    apc_delete($entry_name);
}

对于加速器:

foreach(eaccelerator_list_keys() as $name => $infos) {
    if (preg_match('#^article_3_#', $name)) {
        eaccelerator_rm($name);
    }
}

对于memcached,请查看 @rik的答案

For memcached, look at @rik's answer

一次过期多个密钥的一般解决方案是为它们命名空间.要使它们过期,只需更改名称空间:

The general solution for expiring multiple keys at once is to namespace them. For expiring them, you just have to change the namespace:

假设您有一组键"article_3_1","article_3_2",....您可以这样存储它们:

Say you have a group of keys "article_3_1", "article_3_2", .... You can store them like this:

$ns = apc_fetch('article_3_namespace');
apc_store($ns."_article_3_1", $value);
apc_store($ns."_article_3_2", $value);

像这样获取它们:

$ns = apc_fetch('article_3_namespace');
apc_fetch($ns."_article_3_1");

通过增加名称空间使它们全部过期:

And expire them all by just incrementing the namespace:

apc_inc('article_3_namespace');

这篇关于在apc/memcache/eaccelerator中按前缀删除缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 14:23