Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delete cache by prefix in apc / memcache / 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

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 ?

like image 601
Rami Dabain Avatar asked Dec 04 '22 23:12

Rami Dabain


1 Answers

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');
like image 121
Arnaud Le Blanc Avatar answered Dec 10 '22 10:12

Arnaud Le Blanc