Laravel 缓存获取所有带标签的项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26340011/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:16:02  来源:igfitidea点击:

Laravel cache get all items with tag

phpcachinglaravelmemcached

提问by Matthijn

In Laravel you can place items in the Cache with a tag like:

在 Laravel 中,您可以使用如下标签将项目放入缓存中:

Cache::tags('bans')->put($result->ip, $result->reason);

But I can't seem to find a way to get all items with a certain tag. Is it possible to retrieve all items with a certain tag?

但我似乎无法找到一种方法来获取具有特定标签的所有项目。是否可以检索具有特定标签的所有项目?

Like:

喜欢:

Cache::tags('bans')->all(); 

Or something like that

或类似的东西

采纳答案by Marcin Nabia?ek

I think you use it wrong. As first argument you pass key of cache, as second value and as third expire time in minutes.

我觉得你用错了。作为第一个参数,您传递缓存的键,作为第二个值和第三个以分钟为单位的过期时间。

If you want to cache some bans with reason, for example assume you have in PHP some group of users who spammed, you could use something like that:

如果你想有理由地缓存一些禁令,例如假设你在 PHP 中有一些发送垃圾邮件的用户组,你可以使用这样的东西:

$bans = [
   [
       'ip' => 'test ip',
       'reason' => "spam: test reason",
   ],
   [
       'ip' => 'test ip2 ',
       'reason' => "spam: test reason 2",
   ]

];

Cache::tags('bans')->put('spam', $bans, 100);

$spams = Cache::tags('bans')->get('spam');
foreach ($spams as $spam) {
    echo $spam['ip'].' '.$spam['reason']."<br />";
}

So here you put into cache the whole array and now you can access items using standard foreachloop.

所以在这里你把整个数组放入缓存中,现在你可以使用标准foreach循环访问项目。

回答by CharlieJade

The underlaying Cache Drive is not supported retrieve all caches of a certain tag.

不支持底层缓存驱动器检索某个标签的所有缓存。

If u really need this kind of feature, u should looking for Redis, using Redis' hashinstead of Cache's tags.

如果你真的需要这种功能,你应该寻找Redis,使用Redis 的哈希而不是缓存的标签。

Here is some example code:

下面是一些示例代码:

// Delete hash table - `bans`
Redis::del('bans');
// Setting hash table filed
Redis::hSet('bans', 'ip1', 'spam: test reason');
Redis::hSet('bans', 'ip2', 'spam: test reason 2');
// Get all filed form hash table - `bans`
dd(Redis::hGetAll('bans'));

Debug output will be:

调试输出将是:

array:2 [▼
  "ip1" => "spam: test reason"
  "ip2" => "spam: test reason 2"
]

回答by nat_jea

In 'yourKeyGoesHere'you can insert a string used as same as a like with a * or insert directly the exactly key.

'yourKeyGoesHere' 中,您可以插入一个与 like 一样使用的字符串,并带有 * 或直接插入确切的键。

$redis = Cache::getRedis();
$a_keys = $redis->keys("*yourKeyGoesHere*");