Skip to content

Commit

Permalink
[FEAT] expired eventListener 추가 (#1307)
Browse files Browse the repository at this point in the history
  • Loading branch information
yubinquitous committed Aug 17, 2023
1 parent 6d55b2b commit 824f681
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,32 @@

import lombok.Getter;
import lombok.RequiredArgsConstructor;

import org.ftclub.cabinet.redis.ExpirationListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisKeyValueAdapter;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Getter
@Configuration
@RequiredArgsConstructor
@EnableRedisRepositories // Redis Repository 활성화
@EnableRedisRepositories()
public class RedisRepositoryConfig {

@Bean
public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory redisConnectionFactory) {
RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();
redisMessageListenerContainer.setConnectionFactory(redisConnectionFactory);
return redisMessageListenerContainer;
}

@Value("${spring.redis.host}")
private String host;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.ftclub.cabinet.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

import java.util.Map;


@Component
public class ExpirationListener extends KeyExpirationEventMessageListener {

@Autowired
private RedisTemplate<String, Object> redisTemplate;

/**
* Creates new {@link MessageListener} for {@code __keyevent@*__:expired} messages.
*
* @param listenerContainer must not be {@literal null}.
*/
public ExpirationListener(
@Qualifier("redisMessageListenerContainer")
RedisMessageListenerContainer listenerContainer) {
super(listenerContainer);
}

/**
* @param message redis key
* @param pattern __keyevent@*__:expired
*/
@Override
public void onMessage(Message message, byte[] pattern) {

System.out.println("########## onMessage pattern " + new String(pattern) + " | " + message.toString());

printExpiredKeyValue(message.toString());
}

// 삭제된 키의 value를 출력하려고 해서 출력이 제대로 안 나옴
public void printExpiredKeyValue(String key) {
// System.out.println("########## printExpiredKeyValue " + key);
final Map<Object, Object> entries =
redisTemplate.opsForHash().entries(key);
System.out.println(entries);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ void test() {

// search all entries by prefix
// redisTemplate.opsForHash().keys(cabinetId).forEach(System.out::println);

// sleep 10 sec
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
System.out.println("interrupted");
}
}

/**
Expand All @@ -70,14 +77,15 @@ void test() {
* @param wrongPasswordCount: ${wrongPasswordCount} 또는 ${userCount}
*/
public void saveWithCustomPrefix(String cabinetId, String hashKey, int wrongPasswordCount) {
// 해당 키가 존재하는지 확인
boolean hasKey = Boolean.TRUE.equals(redisTemplate.hasKey(cabinetId));
redisTemplate.opsForHash().put(cabinetId, hashKey, wrongPasswordCount);
// redisTemplate.opsForHash().putAll(cabinetId, convertToMap(tc)); // hashKey를 설정하기 위해 putAll이 아닌 put을 사용했습니다.
// 해당 키가 처음 생성된 것이라면 timeToLive 설정
if (!hasKey) {
System.out.println("set expire time");
// 10초 후에 삭제
redisTemplate.expire(cabinetId, 30, TimeUnit.SECONDS);
// 30초 후에 삭제
redisTemplate.expire(cabinetId, 2, TimeUnit.SECONDS);
}
}

Expand Down

0 comments on commit 824f681

Please sign in to comment.