通过 apc / memcache / eaccelerator 中的前缀删除缓存

发布于 2024-10-13 16:26:52 字数 324 浏览 4 评论 0原文

假设我将这些变量保存在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

甚是思念 2024-10-20 16:26:52

对于APC

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

对于 eaccelerator:

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

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

正确的解决方案

一次使多个密钥过期的一般解决方案是对它们进行命名。要使它们过期,您只需更改命名空间:

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

$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");

并通过仅递增来使它们全部过期命名空间:

apc_inc('article_3_namespace');

The slow solution

For APC:

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

For eaccelerator:

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

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:

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

Fetch them like this:

$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');
耳钉梦 2024-10-20 16:26:52

尽管文档说 APCIterator 在 apc >= 3.1.1 中可用,但我在几个声称拥有 apc 3.1.9 的系统上,但是没有 APCIterator 存在。如果您没有可用的 APCIterator,请尝试一下类似的操作:

$aCacheInfo = apc_cache_info('user');

foreach($aCacheInfo['cache_list'] as $_aCacheInfo)
    if(strpos($_aCacheInfo['info'], 'key_prefix:') === 0)
        apc_delete($_aCacheInfo['info']);

在本示例中,我们将检查密钥中的前缀,但您可以使用 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:

$aCacheInfo = apc_cache_info('user');

foreach($aCacheInfo['cache_list'] as $_aCacheInfo)
    if(strpos($_aCacheInfo['info'], 'key_prefix:') === 0)
        apc_delete($_aCacheInfo['info']);

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.

满地尘埃落定 2024-10-20 16:26:52

有一种方法可以从内存缓存中检索所有密钥,但它非常昂贵的。

There is a way to retrieve all keys from memcache but it's very expensive.

橘亓 2024-10-20 16:26:52

如果可以使用 memcached 的替代品,scache 支持结构化键空间。有了它,您可以将数据存储到嵌套路径:

scache_shset($conn, 'article/1/0', $data10);
scache_shset($conn, 'article/3/0', $data30);
scache_shset($conn, 'article/3/1', $data31);

并最终通过删除父节点来销毁数据

scache_shunset($conn, 'article/3');

If there is possibility to use alternatives for memcached, scache supports structured keyspaces. With it you could store data to nested paths :

scache_shset($conn, 'article/1/0', $data10);
scache_shset($conn, 'article/3/0', $data30);
scache_shset($conn, 'article/3/1', $data31);

and eventually destroy data by deleting the parent node

scache_shunset($conn, 'article/3');
倥絔 2024-10-20 16:26:52

有一个 APCIterator 可以帮助您搜索 APC 中的键。
实例化 APCIterator。

APCIterator::valid() 意味着仍有键需要迭代。 APCIterator::key() 返回 apc 密钥。 APCIterator::next() 将迭代器位置移动到下一项。

// APC
$iterator = new APCIterator('user', '/^article_3_/');

while($iterator->valid()) {
     apc_delete($iterator->key());
     // You can view the info for this APC cache value and so on by using 
     // $iterator->current() which is array
     $iterator->next();
}

对于 memcache,您可以使用 Memcached 并使用 getAllKeys 方法

// Memcached 
$m = new Memcached();
$m->addServer('mem1.domain.com', 11211);

$items = $m->getAllKeys();

foreach($items as $item) {
    if(preg_match('#^article_3_#', $item)) {
        $m->delete($item);
    }
}  

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.

// APC
$iterator = new APCIterator('user', '/^article_3_/');

while($iterator->valid()) {
     apc_delete($iterator->key());
     // You can view the info for this APC cache value and so on by using 
     // $iterator->current() which is array
     $iterator->next();
}

For memcache you can use Memcached and use getAllKeys method

// Memcached 
$m = new Memcached();
$m->addServer('mem1.domain.com', 11211);

$items = $m->getAllKeys();

foreach($items as $item) {
    if(preg_match('#^article_3_#', $item)) {
        $m->delete($item);
    }
}  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文