使用密钥模式从 Laravel 4 缓存中删除?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14873506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Deleting from Laravel 4 cache using pattern for key?
提问by Hailwood
For my package we make use of the Laravel cache,
对于我的包,我们使用 Laravel 缓存,
Every cache key we create is prefixed, so we get mypackage-config
, mypackage-md5ofafilename
At times I need to flush all cache files that my package has created, the issue? I only know the pattern of the cache keys, I don't know the entire key!
我们创建的每个缓存键都有前缀,所以我们得到mypackage-config
,mypackage-md5ofafilename
有时我需要刷新我的包创建的所有缓存文件,问题是?我只知道缓存键的模式,我不知道整个键!
So, I need a way to go Cache::forget('mypackage-*')
or similar, is this possible?
所以,我需要一条路Cache::forget('mypackage-*')
或类似的路,这可能吗?
If it was just for my system I know I am using the file cache, so I could manually unlink the files, but because it is a generic package I don't know what cache method the end user is using, I just know the interface (aka the Laravel cache interface).
如果只是为了我的系统,我知道我正在使用文件缓存,所以我可以手动取消链接文件,但是因为它是一个通用包,我不知道最终用户使用的是什么缓存方法,我只知道接口(又名 Laravel 缓存接口)。
采纳答案by Laurence
Easy - use Cache::getMemory()
易于使用 Cache::getMemory()
foreach (Cache::getMemory() as $cacheKey => $cacheValue)
{
if (strpos($cacheKey, 'mypackage') !== false)
{
Cache::forget($cacheKey);
}
}
p.s. dont ever unlink 'cache' files manually. Laravel cache keeps a record of all cache records in an array, so it will be expecting the file to be there, even if you 'unlink' it.
ps 永远不要手动取消链接“缓存”文件。Laravel 缓存将所有缓存记录的记录保存在一个数组中,因此即使您“取消链接”它,它也会期望文件在那里。
回答by harryg
Another solution: as long as you are notusing file or database cache you can make use of Cache Tags.
另一种解决方案:只要您不使用文件或数据库缓存,您就可以使用Cache Tags。
Just tag every cache entry with your package name:
只需使用您的包名称标记每个缓存条目:
Cache::tags('myPackage')->put('config', $config, $minutes);
Cache::tags('myPackage')->put('md5ofafilename', $md5, $minutes);
(You can also use the tags
method with remember
, forever
, and rememberForever
)
(您也可以将tags
方法与remember
、forever
、 和 一起使用rememberForever
)
When it's time to flush the cache of your package's entries just do
当需要刷新包条目的缓存时,只需执行
Cache::tags('myPackage')->flush();
Note:When you need to access the cache entries you still need to reference the tags. E.g.
注意:当您需要访问缓存条目时,您仍然需要引用标签。例如
$myConfig = Cache::tags('myPackage')->get('config');
That way, another cache entry with key config
having a different tag (e.g. hisPackage
) will not conflict with yours.
这样,另一个config
具有不同标签(例如hisPackage
)的键的缓存条目将不会与您的发生冲突。
回答by Kazik
Here is the same solution as in the accepted answer, but rewritten specifically for Redis.
这是与接受的答案相同的解决方案,但专门为 Redis 重写。
Using KEYS
使用密钥
$redis = Cache::getRedis();
$keys = $redis->keys("*");
foreach ($keys as $key) {
if (strpos($key, 'mypackage') !== false)
{
$redis->del($key);
}
}
Using SCAN (Redis >= 2.8.0)
使用扫描(Redis >= 2.8.0)
$redis = Cache::getRedis();
$cursor = 0;
while($data = $redis->scan($cursor))
{
$cursor = $data[0];
foreach($data[1] as $key)
{
if (strpos($key, 'mypackage') !== false)
{
$redis->del($key);
}
}
}
if ($cursor == 0) break;
}
回答by Zachary Canann
Here is a Redis specific example to clear all keys based on a given prefix. This is based on Kazik's answer, with some restructuring and added safety.
这是一个 Redis 特定示例,用于根据给定的前缀清除所有键。这是基于 Kazik 的回答,进行了一些重组并增加了安全性。
Note that RedisStore is under the namespace Illuminate\Cache\RedisStore
注意RedisStore在namespace下 Illuminate\Cache\RedisStore
$cacheDriver = Cache::driver();
if ($cacheDriver instanceof(RedisStore)) {
$cursor = 0;
do {
$data = $cacheDriver->scan($cursor);
$cursor = $data[0];
$cacheEntries = $data[1];
foreach ($cacheEntries as $key) {
// This clears based on prefix. Change according to your use case.
if (starts_with($key, Cache::getPrefix())) {
$redis->del($key);
}
}
} while ($cursor != 0);
}