Delete cache using prefix in apc / memcache / eaccelerator

Suppose I have these variables stored in apc, memcached and eaccelerator:

  • article_1_0
  • article_1_1
  • article_3_2
  • article_3_3
  • article_2_4

How to remove all cached variables starting with article_3_(they can reach 10,000)?

is there any way to list cached variables?

+4
source share
5 answers

Slow decision

For APC:

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

For the accelerator:

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

For memcached, check out @rik's answer

Correct solution

A common solution for expiring multiple keys at once is a namespace. To complete them, you just need to change the namespace:

, "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');
+11

, 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 et. al - , APCIterator.

+2
+1

memcached, scache . :

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

and ultimately destroy the data by deleting the parent node

scache_shunset($conn, 'article/3');
+1
source

There is an APCIterator that helps you search for keys in APC. Create an instance of APCIterator.

APCIterator :: valid () means that there are keys before the iteration. APCIterator :: key () returns you the apc key. APCIterator :: next () moves the iterator position to the next element.

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

// 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);
    }
}  
+1
source

All Articles