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

痞子三分冷 提交于 2019-11-29 17:07:18

问题


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?


回答1:


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.




回答2:


A quick command to search if a specific key exists (the key name can be a "grep regex")

for i in {1..40}; do (echo "stats cachedump $i 0"; sleep 1; echo "quit";) | telnet localhost 11211 | grep 'APREFIX*\|ANOTHERPREFIX*'; done
  • i is the slab number
  • in the example above we search the slabs from 1 to 40
  • don't miss the grep part 'APREFIX*\|ANOTHERPREFIX*' ;)
  • based on the discussion at https://groups.google.com/forum/#!topic/memcached/YyzonP9HUi0



回答3:


While @btilly is correct in saying that memcached does not do this natively, you can emulate it (quite efficiently) by maintaining an index of keys that share your prefix, allowing you to then fetch all entries that match a certain prefix.

Obviously this will only work for specific keys that you choose in advance and not arbitrary data, but it's quite workable if you can live with that limitation. There is a good article on this subject by one of the memcache developers.




回答4:


You can use Namespace and perform what you need. Here is a PHP library which perform the same. You can use same Memcached for multiple Applications.

https://github.com/vijayabose/n_memcached



来源:https://stackoverflow.com/questions/4889631/is-it-possible-to-get-search-memcached-keys-by-a-prefix

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!