内存缓存和通配符 [英] memcache and wildcards
问题描述
我只是想知道是否有一种方法可以使用通配符清除键值来清除内存缓存.
I'm just wondering if there is a way to clear memcache using wildcards for key values.
所以说我有一个缓存,其中包含键"1234〜foo"和另一个"1234〜foo〜bar".
So say I have a cache with the key "1234~foo" and another "1234~foo~bar".
有什么办法可以说我使用clear("1234 *")之类的东西清除缓存并从上方清除它们吗?
Is there any way I can say clear the cache by using something like clear("1234*") and have it clear both from above?
我希望这是有道理的.
谢谢.
推荐答案
不,没有直接简单的方法可以做到这一点. 常见问题解答解决了此问题,并提供了一种解决方法:
No, there isn't a direct easy way to do this. The FAQ addresses this, and provides a kind of workaround:
按命名空间删除
尽管memcached不支持任何类型的通配符删除或按名称空间删除(由于没有名称空间),但可以使用一些技巧来模拟此.但是,它们确实需要额外访问Memcached服务器.
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.
在PHP中,使用名为foo的命名空间的示例:
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");
这篇关于内存缓存和通配符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!