当redis中key失效时进行回调操作
KeyExpirationEventMessageListener
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.time.DateFormatUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.connection.Message; import org.springframework.data.redis.connection.MessageListener; import org.springframework.data.redis.listener.KeyExpirationEventMessageListener; import org.springframework.data.redis.listener.RedisMessageListenerContainer; import java.util.Date; /** * redis数据过期监听器*/ public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener { private Logger logger = LoggerFactory.getLogger(this.getClass()); /** * Creates new {@link MessageListener} for {@code __keyevent@*__:expired} messages. * * @param listenerContainer must not be {@literal null}. */ public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) { super(listenerContainer); } /** * 针对redis数据失效事件,进行数据处理 * @param message * @param pattern */ @Override public void onMessage(Message message, byte[] pattern) {
// 获取失效数据的key
String expiredKey = message.toString();
}
}