Reputation: 53
My new PHP application could be sped up with some caching of MySQL results. I have limited experience with memcached, but I don't think it can do what I require.
As I am working on a multi-user application I would like to be able to delete several stored values at once without removing everything.
So I might store:
account_1.value_a = foo
account_1.value_b = bar
account_2.value_a = dog
account_2.value_b = cat
Is there a caching system that would allow me to delete based on a wildcard (or similar method) such as "delete account_1.*" leaving me with:
account_1.value_a = <unset>
account_1.value_b = <unset>
account_2.value_a = dog
account_2.value_b = cat
Thanks, Jim
Upvotes: 2
Views: 2442
Reputation: 776
It may be late, but for anyone who have same issue, you can use APCu, which is so fast and lightweight. It can do what OP asked (by using regex).
Example:
apcu_store('user1Address', "some data");
apcu_store('user1Name' , "some other data");
apcu_store('user2Name' , "some other data again");;
echo "All cached data:<br>";
foreach (new APCUIterator('/^.*/') as $item) {
echo $item['key'] . " -> " . $item['value'] . "<br>";
}
//Delete all cache that thair key start with 'user1'
foreach (new APCUIterator('/^user1.*/') as $item) {
apcu_delete($item['key']);
}
echo "All cached data after delete:<br>";
foreach (new APCUIterator('/^.*/') as $item) {
echo $item['key'] . " -> " . $item['value'] . "<br>";
}
And the output would be:
All cached data:
user1Name -> some other data
user1Address -> some data
user2Name -> some other data again
All cached data after delete:
user2Name -> some other data again
Note that APCu
is not installed by default
Upvotes: 0
Reputation: 1
Memcached delete by tag can be done like this;
Searching and deleting 100,000 keys is quite fast, but performance should be monitored in much larger caches.
Before Php 8.0
$tag = "account_1";
$cached_keys = $this->memcached->getAllKeys();
foreach($cached_keys as $key){
if(substr($key, 0, strlen($tag)) === $tag){
$this->memcached->delete($key);
}
}
Php 8.0 >
$tag = "account_1";
$cached_keys = $this->memcached->getAllKeys();
foreach($cached_keys as $key){
if (str_starts_with($key, $tag)) {
$this->memcached->delete($key);
}
}
Upvotes: 0
Reputation: 1
Scache (http://scache.nanona.fi) has nested keyspaces so you could store data on subkeys and expire parent when needed.
Upvotes: 0
Reputation:
Open source module to get tags for keys in memcache, and other: http://github.com/jamm/memory/
Upvotes: 0
Reputation: 60498
Not really, but you can fake it by using version numbers in your keys.
For example, if you use keys like this:
{entitykey}.{version}.{fieldname}
So now your account_1
object keys would be:
account_1.1.value_a
account_1.1.value_b
When you want to remove account_1
from the cache, just increment the version number for that object. Now your keys will be:
account_1.2.value_a
account_1.2.value_b
You don't even need to delete the original cached values - they will fall out of the cache automatically since you'll no longer be using them.
Upvotes: 5