Laravel 中的 Cache 类具有 get('itemKey') 等方法来从缓存中检索项目,以及 Remember('itemKey', ['myData1', 'myData2']) 来将项目保存在缓存中。
还有一个方法可以检查缓存中是否存在某个项目:Cache::has('myKey');
有什么方法(当使用基于文件的缓存驱动程序时)获取缓存中所有项目的列表?
例如,可能被命名为“Cache::all()”之类的东西,它将返回:
[
'itemKey' => [
'myData1',
'myData2'
],
'myKey' => 'foo'
]
我能想到的唯一方法是使用 Cache::has() 方法循环遍历所有可能的键名称。即 aaa、aab、aac、aad...但当然,这不是解决方案。
我在文档或 API 中看不到任何描述此类函数的内容,但我认为相信这样的函数一定存在是没有道理的。
旧的答案在 Laravel 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 {
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)