Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

memcache and wildcards

Tags:

memcached

I'm just wondering if there is a way to clear memcache using wildcards for key values.

So say I have a cache with the key "1234~foo" and another "1234~foo~bar".

Is there any way I can say clear the cache by using something like clear("1234*") and have it clear both from above?

I hope that makes sense.

Thanks.

like image 260
GivP Avatar asked Oct 20 '09 16:10

GivP


People also ask

What is memcache used for?

Memcached can serve cached items in less than a millisecond, and enables you to easily and cost effectively scale for higher loads. Memcached is popular for database query results caching, session caching, web page caching, API caching, and caching of objects such as images, files, and metadata.

How does Memcached work internally?

Clients use a hashing algorithm to determine which Memcached storage server to use - this helps distribute the load. The server then computes a second hash of the key in order to determine where it should store the corresponding value in an internal hash table.

Is Memcached transactional?

Memcached does not support transactions in this sense, although its operations are atomic.


1 Answers

No, there isn't a direct easy way to do this. The FAQ addresses this, and provides a kind of workaround:

Deleting by Namespace

While memcached does not support any type of wildcard deleting or deletion by namespace (since there are not namespaces), there are some tricks that can be used to simulate this. They do require extra trips to the memcached servers however.

Example, in PHP, for using a namespace called foo:

$ns_key = $memcache->get("foo_namespace_key"); // if not set, initialize it if($ns_key===false) {     $ns_key=rand(1, 10000);     $memcache->set("foo_namespace_key", $ns_key); } // cleverly use the ns_key $my_key = "foo_".$ns_key."_12345"; $my_val = $memcache->get($my_key);  //To clear the namespace do: $memcache->increment("foo_namespace_key"); 
like image 66
Eric Petroelje Avatar answered Sep 19 '22 10:09

Eric Petroelje