php 如何在 Laravel 5 中按键获取所有缓存项目的列表?

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

How to get list of all cached items by key in Laravel 5?

phplaravelcachinglaravel-5key

提问by kohloth

The Cache class in laravel has methods such as get('itemKey') to retrieve items from the cache, and remember('itemKey', ['myData1', 'myData2']) to save items in the cache.

laravel 中的 Cache 类有 get('itemKey') 等方法从缓存中检索项目,remember('itemKey', ['myData1', 'myData2']) 将项目保存在缓存中。

There is also a method to check if an item exists in the cache: Cache::has('myKey');

还有一种方法可以检查缓存中是否存在某个项目:Cache::has('myKey');

Is there any way, (when using the file-based cache driver), to get a list of all items in the cache?

有什么方法可以(在使用基于文件的缓存驱动程序时)获取缓存中所有项目的列表?

For instance, something that might be named something like "Cache::all()" that would return:

例如,可能命名为“Cache::all()”之类的东西会返回:

[
    'itemKey' => [
        'myData1',
        'myData2'
   ],
   'myKey' => 'foo'
]

The only way I can think of doing this is to loop through all possible key names using the Cache::has() method. i.e. aaa, aab, aac, aad... but of course, this is not a solution.

我能想到的唯一方法是使用 Cache::has() 方法遍历所有可能的键名。即 aaa、aab、aac、aad...但当然,这不是解决方案。

I can't see anything in the documentation or the API that describes a function like this, but I don't think its unreasonable to believe that one must exist.

我在文档或 API 中看不到任何描述这样的函数的内容,但我认为相信一个函数必须存在并不是没有道理的。

采纳答案by jedrzej.kurylo

There is no way to do that using Cachefacade. Its interface represents the functionality that allunderlying storages offer and some of the stores do not allow listing all keys.

使用Cache外观无法做到这一点。它的界面代表了所有底层存储提供的功能,并且一些存储不允许列出所有密钥。

If you're using the FileCache, you could try to achieve that by interacting with the underlying storage directly. It doesn't offer the method you need, so you'll need to iterate through the cache directory. It won't be too efficient due to a lot of disk I/O that might need to happen.

如果您正在使用FileCache,您可以尝试通过直接与底层存储交互来实现这一点。它不提供您需要的方法,因此您需要遍历缓存目录。由于可能需要进行大量磁盘 I/O,因此效率不会太高。

In order to access the storage, you need to do

为了访问存储,你需要做

$storage = Cache::getStore(); // will return instance of FileStore
$filesystem = $storage->getFilesystem(); // will return instance of Filesystem

$keys = [];
foreach ($filesystem->allFiles('') as $file1) {
  foreach ($filesystem->allFiles($file1) as $file2) {
    $keys = array_merge($keys, $filesystem->allFiles($file1 . '/' . $file2));
  }
}

回答by mangas

^This above dont work in LV 5.2

^以上在 LV 5.2 中不起作用

Try this solution:

试试这个解决方案:

    $storage = \Cache::getStore(); // will return instance of FileStore
    $filesystem = $storage->getFilesystem(); // will return instance of Filesystem
    $dir = (\Cache::getDirectory());
    $keys = [];
    foreach ($filesystem->allFiles($dir) as $file1) {

        if (is_dir($file1->getPath())) {

            foreach ($filesystem->allFiles($file1->getPath()) as $file2) {
                $keys = array_merge($keys, [$file2->getRealpath() => unserialize(substr(\File::get($file2->getRealpath()), 10))]);
            }
        }
        else {

        }
    }

回答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*");
 foreach ($a_keys as $key){
    //Your Action ...
    //For example forget key
    $redis->del($key);
 }

回答by repat

For Memcached, you can do this:

对于 Memcached,您可以这样做:

cache()->getMemcached()->getAllKeys()
  1. get Illuminate\Cache\CacheManager
  2. Get Memcached: http://php.net/manual/de/class.memcached.php
  3. getAllKeys(): http://php.net/manual/de/memcached.getallkeys.php
  1. 得到 Illuminate\Cache\CacheManager
  2. 获取Memcachedhttp: //php.net/manual/de/class.memcached.php
  3. getAllKeys()http: //php.net/manual/de/memcached.getallkeys.php

This gives you an array of keys you can go through.

这为您提供了一系列可以通过的键。

回答by Yevgeniy Afanasyev

in \config\database.phpmake a redis store for the cache

\config\database.php赚了Redis的商店缓存

   // store cache in their own redis store ...
    'cache-connection' => [
        'host'               => ...,
        'password'           => ...,
        'port'               => env('REDIS_PORT', 6379),
        'database'           => 2,
        'read_write_timeout' => 60,
        'timeout'            => 6.0,
    ],

in \config\cache.phpuse this redis database

正在\config\cache.php使用这个 redis 数据库

'stores' => [
   ...
   'redis' => [
        'driver'     => 'redis',
        'connection' => 'cache-connection',
    ],
],

now you can use Redis class to check what is in your cache

现在您可以使用 Redis 类来检查缓存中的内容

$a = Redis::connection('cache-connection')->keys('*');
\Log::debug($a);