phpmemcachedapceaccelerator

delete cache by prefix in apc / memcache / eaccelerator


Let's assume I have these variables saved in apc, memcached and eaccelerator:

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 ?


Solution

  • 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');