Reputation: 914
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.
There is also a method to check if an item exists in the cache: 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:
[
'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.
Upvotes: 23
Views: 69057
Reputation: 474
Older answers didn't work for me in Laravel 5.2 so I used 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 {
}
}
Upvotes: 16
Reputation: 101
Step 1
// Add Namespace
use Illuminate\Support\Facades\Redis;
Step 2
// Get All Available Keys
$cacheKeys = Redis::connection('cache')->keys('*');
Step 3
// Laravel DDD
ddd($cacheKeys);
OR
// PHP Variable Dump
var_dump($cacheKeys);
Upvotes: 5
Reputation: 39
I know this is an old question but I ran into the issue the other day and couldn't find a solution anywhere for the file storage system.
My use case was that I wanted to be able to remove based on the naming convention of full stops seperating groups. For example cache()->forget('foo')
wouldn't remove the key foo.bar
.
The way it works is it keeps a json encoded array of all the keys you add to the file store, then when you want to remove it loops through and if it matches it is removed. This may be useful to you as well but if not your use case could utilize the cache()->getKeys()
method which now works too.
Steps to follow:
In your AppServiceProvider.php
register
method add the following:
use Illuminate\Support\Facades\Cache;
use App\Extensions\FileStore;
...
$this->app->booting(function () {
Cache::extend('file', function ($app) {
return Cache::repository(new FileStore($app['files'], config('cache.stores.file.path'), null));
});
});
Then create a new directory in app
called Extensions
. Add a new file in the new Extensions
directory called FileStore.php
with the following contents:
<?php
namespace App\Extensions;
class FileStore extends \Illuminate\Cache\FileStore
{
/**
* Get path for our keys store
* @return string
*/
private function keysPath()
{
return storage_path(implode(DIRECTORY_SEPARATOR, ['framework','cache','keys.json']));
}
/**
* Get all keys from our store
* @return array
*/
public function getKeys()
{
if (!file_exists($this->keysPath())) {
return [];
}
return json_decode(file_get_contents($this->keysPath()), true) ?? [];
}
/**
* Save all keys to file
* @param array $keys
* @return bool
*/
private function saveKeys($keys)
{
return file_put_contents($this->keysPath(), json_encode($keys)) !== false;
}
/**
* Store a key in our store
* @param string $key [description]
*/
private function addKey($key)
{
$keys = $this->getKeys();
// Don't add duplicate keys into our store
if (!in_array($key, $keys)) {
$keys[] = $key;
}
$this->saveKeys($keys);
}
// -------------------------------------------------------------------------
// LARAVEL METHODS
// -------------------------------------------------------------------------
/**
* Store an item in the cache for a given number of seconds.
*
* @param string $key
* @param mixed $value
* @param int $seconds
* @return bool
*/
public function put($key, $value, $seconds)
{
$this->addKey($key);
return parent::put($key, $value, $seconds);
}
/**
* Remove an item from the cache.
*
* @param string $key
* @return bool
*/
public function forget($forgetKey, $seperator = '.')
{
// Get all stored keys
$storedKeys = $this->getKeys();
// This value will be returned as true if we match at least 1 key
$keyFound = false;
foreach ($storedKeys as $i => $storedKey) {
// Only proceed if stored key starts with OR matches forget key
if (!str_starts_with($storedKey, $forgetKey.$seperator) && $storedKey != $forgetKey) {
continue;
}
// Set to return true after all processing
$keyFound = true;
// Remove key from our records
unset($storedKeys[$i]);
// Remove key from the framework
parent::forget($storedKey);
}
// Update our key list
$this->saveKeys($storedKeys);
// Return true if at least 1 key was found
return $keyFound;
}
/**
* Remove all items from the cache.
*
* @return bool
*/
public function flush()
{
$this->saveKeys([]);
return parent::flush();
}
}
Upvotes: 1
Reputation: 41360
in \config\database.php
make a redis store for the cache
// 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.php
use this redis database
'stores' => [
...
'redis' => [
'driver' => 'redis',
'connection' => 'cache-connection',
],
],
now you can use Redis class to check what is in your cache
$a = Redis::connection('cache-connection')->keys('*');
\Log::debug($a);
Upvotes: 5
Reputation: 350
For Memcached, you can do this:
cache()->getMemcached()->getAllKeys()
Illuminate\Cache\CacheManager
Memcached
: http://php.net/manual/de/class.memcached.phpgetAllKeys()
: http://php.net/manual/de/memcached.getallkeys.phpThis gives you an array of keys you can go through.
Upvotes: 9
Reputation: 323
In 'yourKeyGoesHere' you can insert a string used as same as a like with a * or insert directly the exactly key.
$redis = Cache::getRedis();
$a_keys = $redis->keys("*yourKeyGoesHere*");
foreach ($a_keys as $key){
//Your Action ...
//For example forget key
$redis->del($key);
}
Upvotes: 12
Reputation: 40909
There is no way to do that using Cache facade. Its interface represents the functionality that all underlying storages offer and some of the stores do not allow listing all keys.
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));
}
}
Upvotes: 12