springboot + redis expired event listeners

1 redis.conf modify the configuration file:

K Keyspace events, published with keyspace@ prefix事件
E Keyevent events, published with keyevent@ prefix
g Generic commands (non-type specific) like DEL, EXPIRE, RENAME, …
$ String commands
l List commands
s Set commands
h Hash commands
z Sorted set commands
x Expired events (events generated every time a key expires)
e Evicted events (events generated when a key is evicted for maxmemory)
A Alias for g$lshzxe, so that the “AKE” string means all the events.
The default configuration redis.conf are: the Notify -keyspace- Events ""
We need to be changed: the Notify -keyspace- Events Ex
That corresponds to the above key event expired. Play this modification restart the redis

2 client to listen expired redis event of:

@Configuration
public class RedisListenerConfig {
    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {

        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        return container;
    }
}

3. Write a listener

@ Slf4j
@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {

    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {
        String expiredKey = message.toString();
       log.info("expiredKey========="+expiredKey);
    }

4. The method of query tables just added two different id, a 30s, a 27s.

  redisUtil.set("UserId"+user.get(0).getId(),user.get(0).getId(),30);
  redisUtil.set("UserInfoId"+userInfo.get(0).getId(),userInfo.get(0).getId(),27);

Console output:

Note that:
expired listen for messages in return is that the key value of the key expired, there is no return value of

Guess you like

Origin www.cnblogs.com/red-star/p/12557965.html
Recommended