Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 936
kohloth Avatar asked Aug 03 '15 15:08

kohloth


People also ask

How to retrieve items from the cache in Laravel?

Show activity on this post. 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 it possible to retrieve all caches of a tag?

The underlaying Cache Drive is not supported retrieve all caches of a certain tag. If u really need this kind of feature, u should looking for Redis, using Redis' hash instead of Cache's tags.

How to speed up your Laravel application?

The cached data is usually stored in a very fast data store such as Memcached or Redis. Thankfully, Laravel provides an expressive, unified API for various cache backends, allowing you to take advantage of their blazing fast data retrieval and speed up your web application. Your application's cache configuration file is located at config/cache.php.

How to see all keys registered in Redis in Laravel?

If you’ve set up your Laravel application with Redis and you want to see all the keys registered in your Redis, simply open Laravel Tinker using the following command: And then run the following code snippet to get all the keys available in your redis:


1 Answers

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 {

        }
    }
like image 182
mangas Avatar answered Oct 10 '22 21:10

mangas