2

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

AMadmanTriumphs
  • 4,888
  • 3
  • 28
  • 44

5 Answers5

5

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.

Eric Petroelje
  • 59,820
  • 9
  • 127
  • 177
0

This might help: memcache and wildcards

Community
  • 1
  • 1
Parris Varney
  • 11,320
  • 12
  • 47
  • 76
0

Open source module to get tags for keys in memcache, and other: http://github.com/jamm/memory/

OZ_
  • 12,492
  • 7
  • 50
  • 68
0

Scache (http://scache.nanona.fi) has nested keyspaces so you could store data on subkeys and expire parent when needed.

Stefan
  • 1
  • 1
0

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);
    }
}