开发者

Memcache alternatives, more control

My new PHP application could be sped up with some caching of MySQL results. I have limited experience with memcached,开发者_如何学Python 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


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.


This might help: memcache and wildcards


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


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜