Deleting from Laravel 4 cache using pattern for key? Deleting from Laravel 4 cache using pattern for key? laravel laravel

Deleting from Laravel 4 cache using pattern for key?


Another solution: as long as you are not using file or database cache you can make use of Cache Tags.

Just tag every cache entry with your package name:

Cache::tags('myPackage')->put('config', $config, $minutes);Cache::tags('myPackage')->put('md5ofafilename', $md5, $minutes);

(You can also use the tags method with remember, forever, and rememberForever)

When it's time to flush the cache of your package's entries just do

Cache::tags('myPackage')->flush();

Note:When you need to access the cache entries you still need to reference the tags. E.g.

$myConfig = Cache::tags('myPackage')->get('config');

That way, another cache entry with key config having a different tag (e.g. hisPackage) will not conflict with yours.


Easy - use Cache::getMemory()

foreach (Cache::getMemory() as $cacheKey => $cacheValue){    if (strpos($cacheKey, 'mypackage') !== false)    {        Cache::forget($cacheKey);    }}

p.s. dont ever unlink 'cache' files manually. Laravel cache keeps a record of all cache records in an array, so it will be expecting the file to be there, even if you 'unlink' it.


Here is the same solution as in the accepted answer, but rewritten specifically for Redis.

Using KEYS

$redis = Cache::getRedis();$keys = $redis->keys("*");foreach ($keys as $key) {  if (strpos($key, 'mypackage') !== false)  {    $redis->del($key);  }}

Using SCAN (Redis >= 2.8.0)

$redis = Cache::getRedis();$cursor = 0;while($data = $redis->scan($cursor)){  $cursor = $data[0];  foreach($data[1] as $key)  {    if (strpos($key, 'mypackage') !== false)      {        $redis->del($key);      }    }  }  if ($cursor == 0) break;}