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.

还有一个方法可以判断缓存中是否存在项:cache::has(‘MyKey’);

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

例如,类似于"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.

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.

推荐答案

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

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.

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));
  }
}

Laravel相关问答推荐

如何使中继器项目计数动态Laravel Filament V3?

Laravel 10 - 没有 $append 属性的Eloquent 的热切加载

LARVAL SAVE 中的关系有

我在 laravel 中插入多个复选框值.我怎样才能做到这一点?

函数接受 Laravel 集合对象,尽管只允许使用字符串

在 laravel 项目中放置 css 文件的位置

try 发布 AJAX 请求时 POST 405(方法不允许)-Laravel 4

如何判断用户Electron邮件是否已存在

如何在 laravel 中使用 str_replace

实施 Payum/Laravel 定期付款

如何使用 Laravel 模型访问数据库视图?

PHP 5.5,什么情况下PHP会导致很高的committed memory

你如何在自定义 Laravel Nova 工具中使用确认对话?

不支持驱动Driver[] - Laravel 5.3

Laravel 5.1 重命名项目

Laravel 4 - 如何将所有字段的所有验证错误消息作为 JSON struct 返回?

Laravel 如何具体构建和判断 CSRF 令牌?

在 laravel 中安装 vue 3.0

如何以 JSON 格式发送 Laravel 错误响应

如何在不要求用户登录 Laravel 的情况下验证Electron邮件