springboot+redis 만 료 사건 감청 실현 과정 분석
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 를 되 돌려 주지 않 았 습 니 다.
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
thymeleaf로 HTML 페이지를 동적으로 만듭니다 (spring + gradle)지난번에는 에서 화면에 HTML을 표시했습니다. 이번에는 화면을 동적으로 움직여보고 싶기 때문에 입력한 문자를 화면에 표시시키고 싶습니다. 초보자의 비망록이므로 이상한 점 등 있으면 지적 받을 수 있으면 기쁩니다! ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.