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

前端 未结 5 814
旧时难觅i
旧时难觅i 2021-02-19 10:01

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 cach

相关标签:
5条回答
  • 2021-02-19 10:33

    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);
    
    0 讨论(0)
  • 2021-02-19 10:35

    ^This above dont work in LV 5.2

    Try 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 {
    
            }
        }
    
    0 讨论(0)
  • 2021-02-19 10:35

    For Memcached, you can do this:

    cache()->getMemcached()->getAllKeys()
    
    1. get Illuminate\Cache\CacheManager
    2. Get Memcached: http://php.net/manual/de/class.memcached.php
    3. getAllKeys(): http://php.net/manual/de/memcached.getallkeys.php

    This gives you an array of keys you can go through.

    0 讨论(0)
  • 2021-02-19 10:50

    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));
      }
    }
    
    0 讨论(0)
  • 2021-02-19 10:55

    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);
     }
    
    0 讨论(0)
提交回复
热议问题