Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get/search Memcached keys by a prefix?

Tags:

I'm writing to memcached a lot of key/value -> PREFIX_KEY1, PREFIX_KEY2, PREFIX_KEY3

I need to get all the keys that starts with PREFIX_

Is it possible?

like image 893
Zanoni Avatar asked Feb 03 '11 17:02

Zanoni


People also ask

How can I see all keys in memcached?

Conclusion. In short, to list all keys in Memcached, we obtain the slab ID then list the keys using stats cachedump command.

How many key value pairs are stored on the memcached server?

Memcached statistics hold a lot of valuable information. For example, the total number of items (i.e. key-value pairs) stored in Memcached, can be useful information to move forward. The total number of items stored in Memcached is 10.

Is Memcached free?

Memcached's website describes Memcached as a 'Free and open source, high-performance, distributed memory object caching system'. Like Redis, Memcached is an open source way to store key value pairs in memory, meaning that data is very quickly retrieved.


1 Answers

Sorry, but no. Memcached uses a hashing algorithm that distributes keys at apparently random places, and so those keys are scattered all over. You'd have to scan everything to find them.

Also you should be aware that, by design, memcached can drop any any key at any time for any reason. If you're putting stuff in, you should be aware that you can't depend on it coming back out. This is absolutely fine for its original use case, a cache to reduce hits on a database. But it can be a severe problem if you want to do something more complicated with it.

If these limitations are a problem, I would suggest that you use Redis instead. It behaves a lot like memcached, except that it will persist data and it lets you store complex data structures. So for your use case you can store a hash in Redis, then pull the whole hash out later.

like image 86
btilly Avatar answered Nov 07 '22 16:11

btilly