framework icon indicating copy to clipboard operation
framework copied to clipboard

Unable to subscribe to Redis global key expiration events due to Laravel prefix handling

Open Lukasss93 opened this issue 2 months ago • 5 comments

Laravel Version

12.30.1

PHP Version

8.4.2

Database Driver & Version

Redis 8.0.1

Description

I have a use case where I need to subscribe to a Redis event when a key expires, in order to trigger specific logic.

After enabling keyspace notifications in Redis with:

# redis-cli >
CONFIG SET notify-keyspace-events Ex

and subscribing with:

Redis::subscribe(['__keyevent@0__:expired'], function ($message) {
    // handle logic
});

I don’t receive the global event.

This happens because Laravel only listens to channels with the prefix defined in config/database.php under redis.options.prefix. If I replace the default prefix with an empty string, the subscription works correctly.

It looks like currently there is no way to listen to global Redis events without modifying the prefix.

Question

Is it possible to implement a way to subscribe to global events without having to remove/override the Redis prefix? And also without having to set an empty prefix at runtime, since I might want to subscribe to both Redis global events and other prefixed channels at the same time.

This seems related to this issue: https://github.com/laravel/framework/issues/29449

Steps To Reproduce

  1. Enable Redis key expiration notifications:

    CONFIG SET notify-keyspace-events Ex
    
  2. Use Laravel to subscribe to the expiration channel:

    Redis::subscribe(['__keyevent@0__:expired'], function ($message) {
        // handle logic
    });
    
  3. Let a key expire.

  4. Notice that no event is received unless you remove the prefix by setting redis.options.prefix to an empty string in config/database.php.

Lukasss93 avatar Sep 24 '25 14:09 Lukasss93