是否可以通过前缀获取/搜索 Memcached 键?

发布于 2024-10-16 01:27:33 字数 118 浏览 5 评论 0原文

我正在向 memcached 写入很多键/值 -> PREFIX_KEY1, PREFIX_KEY2, PREFIX_KEY3

我需要获取所有以 PREFIX_ 开头的键,

这可能吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

苹果你个爱泡泡 2024-10-23 01:27:33

抱歉,但是没有。 Memcached 使用哈希算法将密钥分布在明显随机的位置,因此这些密钥分散在各处。你必须扫描所有内容才能找到它们。

您还应该意识到,根据设计,memcached 可以出于任何原因随时删除任何键。如果你要放入东西,你应该意识到你不能指望它会回来。这对于其原始用例来说绝对没问题,即用于减少数据库命中的缓存。但如果你想用它做一些更复杂的事情,这可能是一个严重的问题。

如果这些限制是一个问题,我建议您改用 Redis。它的行为很像 memcached,只不过它会持久保存数据并且允许您存储复杂的数据结构。因此,对于您的用例,您可以在 Redis 中存储哈希,然后稍后取出整个哈希。

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.

梦毁影碎の 2024-10-23 01:27:33

搜索特定键是否存在的快速命令(键名称可以是“grep regex”)

for i in {1..40}; do (echo "stats cachedump $i 0"; sleep 1; echo "quit";) | telnet localhost 11211 | grep 'APREFIX*\|ANOTHERPREFIX*'; done

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
把昨日还给我 2024-10-23 01:27:33

虽然 @btilly 正确地说 memcached 本身不会执行此操作,但您可以通过维护共享前缀的键索引来模拟它(非常有效),从而允许您获取与特定前缀匹配的所有条目。

显然,这仅适用于您提前选择的特定键,而不适用于任意数据,但如果您可以忍受这种限制,那么它是相当可行的。其中一位 memcache 人员有一篇关于此主题的好文章开发商。

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.

孤檠 2024-10-23 01:27:33

您可以使用命名空间并执行您需要的操作。这是一个执行相同功能的 PHP 库。您可以将同一个 Memcached 用于多个应用程序。

https://github.com/vijayabose/n_memcached

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文