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

koh*_*oth 12 php caching key laravel laravel-5

laravel中的Cache类具有诸如get('itemKey')之类的方法来从缓存中检索项目,并记住('itemKey',['myData1','myData2'])以将项目保存在缓存中.

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

有没有办法(使用基于文件的缓存驱动程序时)获取缓存中所有项目的列表?

例如,某些可能被命名为"Cache :: all()"的东西会返回:

[
    'itemKey' => [
        'myData1',
        'myData2'
   ],
   'myKey' => 'foo'
]
Run Code Online (Sandbox Code Playgroud)

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

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

小智 10

^上面这个在LV 5.2中不起作用

尝试此解决方案:

    $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 {

        }
    }
Run Code Online (Sandbox Code Playgroud)


jed*_*ylo 9

使用Cache facade 无法做到这一点.它的界面代表所有底层存储提供的功能,而某些商店不允许列出所有密钥.

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

要访问存储,您需要这样做

$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));
  }
}
Run Code Online (Sandbox Code Playgroud)


nat*_*jea 8

'yourKeyGoesHere'中,您可以插入一个与*相同的字符串,或者直接插入精确的键.

 $redis = Cache::getRedis();
 $a_keys = $redis->keys("*yourKeyGoesHere*");
 foreach ($a_keys as $key){
    //Your Action ...
    //For example forget key
    $redis->del($key);
 }
Run Code Online (Sandbox Code Playgroud)

  • 虽然此代码片段可以解决问题,但[包括解释](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)确实有助于提高帖子的质量。请记住,您是在为将来的读者回答问题,而那些人可能不知道您建议代码的原因。 (2认同)

小智 5

对于 Memcached,您可以这样做:

cache()->getMemcached()->getAllKeys()
Run Code Online (Sandbox Code Playgroud)
  1. 得到 Illuminate\Cache\CacheManager
  2. 获取Memcachedhttp : //php.net/manual/de/class.memcached.php
  3. getAllKeys()http : //php.net/manual/de/memcached.getallkeys.php

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


Yev*_*yev 5

\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,
    ],
Run Code Online (Sandbox Code Playgroud)

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

'stores' => [
   ...
   'redis' => [
        'driver'     => 'redis',
        'connection' => 'cache-connection',
    ],
],
Run Code Online (Sandbox Code Playgroud)

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

$a = Redis::connection('cache-connection')->keys('*');
\Log::debug($a);
Run Code Online (Sandbox Code Playgroud)