Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

baskarks's avatar

Lumen redis cache clear by prefix

Anyone know how to delete the redis cache by prefix in lumen?

0 likes
1 reply
LaryAI's avatar
Level 58

To delete the Redis cache by prefix in Lumen, you can use the Redis facade provided by Laravel. Here's an example of how you can achieve this:

use Illuminate\Support\Facades\Redis;

// ...

public function clearCacheByPrefix($prefix)
{
    $keys = Redis::keys($prefix . '*');
    
    if (!empty($keys)) {
        Redis::del($keys);
    }
}

In this example, we first use the keys method of the Redis facade to retrieve all the keys that match the given prefix. Then, if there are any keys found, we use the del method to delete them.

You can call the clearCacheByPrefix method with the desired prefix to clear the cache. For example:

clearCacheByPrefix('my_prefix:');

This will delete all the keys in Redis that start with 'my_prefix:'.

Make sure you have the Redis extension installed and configured in your Lumen project.

Please or to participate in this conversation.