springboot+redis 만 료 사건 감청 실현 과정 분석

1 redis.conf 설정 파일 수정:
K 키 스페이스 이벤트,keyspace@접두사 이벤트 로 게 시 됨
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.
redis.conf 의 기본 설정 은:notify-keyspace-events"입 니 다.
notify-keyspace-events Ex 로 변경 해 야 합 니 다.
위 키 의 만 료 이벤트 에 대응 하 는 것 입 니 다.이거 수정 하고 리 디 스 다시 시작 할 게 요.
2 클 라 이언 트 가 redis 의 만 료 사건 을 감청 합 니 다.

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

    RedisMessageListenerContainer container = new RedisMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    return container;
  }
}
3.모니터 를 작성 한다.

@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.조회 방법 에 표 두 개 중 다른 id,하 나 는 30s,하 나 는 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);
콘 솔 출력:

주의해 야 할 것 은:
만 료 된 감청 메시지 에서 돌아 오 는 것 은 만 료 된 키 의 key 값 으로 value 를 되 돌려 주지 않 았 습 니 다.
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기