Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete stored apc keys with a regular expression

Tags:

php

caching

apc

Is it possible to use a method to enable you to delete stored apc entries with regular expressions?

For example some users queries I want to delete from the cache when new data is entered into the database so that the new data is shown the next time the query is run.

Say you have a friend list query which is cached, but when a new friend is added all cached friend queries for that user would be deleted ...

If I have keys like this for the users friendlist:

$sql = "SELECT * FROM friends WHERE userId = :userId";

$sqlKey = str_replace(":userId", $userId, $sql);    
$key = $userId."-friend".md5('query'.$sqlKey);

$data = friendsArray;
apc_add($key, $data, 60 * 10);

Then the desired outcome would be to delete all entries that began with the current userId after running the add new friend query to ensure the friend list displays the new user on the next viewing:

apc_delete("~$userId-friend([a-f0-9]+)~");

As the friends list sql and the add friend sql are in different documents this seems the simplest way to go about doing it without rewriting and keying the sql but I don't think regular expressions are supported?

like image 805
Dan Avatar asked Oct 23 '22 16:10

Dan


1 Answers

As J Fox wrote on php.net, apc_delete also accepts and array of keys or APCIterator object. And APCIterator accepts regex. So your code should look about like that:

// delete all keys beginning with a regex match
$toDelete = new APCIterator('user', '/^' . $userId . '-friend([a-f0-9]+)/', APC_ITER_VALUE);

var_dump( apc_delete($toDelete) );
// returns boolean true|false on success or failure
like image 183
Mołot Avatar answered Oct 27 '22 06:10

Mołot