728x90
반응형
SMALL
Gradle
implementation('org.springframework.boot:spring-boot-starter-data-redis')
implementation('redis.clients:jedis:2.9.0')
https://spring.io/projects/spring-data-redis
mvnrepository.com/artifact/org.springframework.data/spring-data-redis
Config
@Configuraton
@EnableRedisRepositories
@PropertySource("classpath:/redis-test-${spring.profiles.active}.properties")
public class RedisConfig {
@Value("${redis.cluster.nodes}")
private List<String> nodes;
public List<String> getNodes() {
return nodes;
}
public void setNodes(List<String> nodes) {
this.nodes = nodes;
}
@Bean
public JedisPoolConfig jedisPoolConfig() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(30);
jedisPoolConfig.setMinIdle(10);
jedisPoolConfig.setTestOnBorrow(true);
jedisPoolConfig.setTestOnReturn(true);
return jedisPoolConfig;
}
@Bean
public JedisConnectionFactory redisConnectionFactory() {
RedisClusterConfiguration clusterConfiguration = new RedisClusterConfiguration(getNodes());
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(clusterConfiguration, jedisPoolConfig());
jedisConnectionFactory.setUsePool(true);
return jedisConnectionFactory;
}
@Bean
public RedisTemplate<String, String> redisTemplate() {
RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory());
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new StringRedisSerializer());
redisTemplate.setEnableDefaultSerializer(false);
redisTemplate.setEnableTransactionSupport(true);
return redisTemplate;
}
}
CRUD Interface
public interface RedisCrudProvider<T> {
String get(String key);
T get(String key, Type type);
void save(String key, Object value);
...
boolean hasKey(String key);
void delete(String key);
}
CRUD Interface 구현체
@Slf4j
@Component
@RequiredArgsConstructor
public class RedisCrudProviderImpl<T> implements RedisCrudProvider<T> {
private final RedisTemplate redisTemplate;
private final Gson gson;
@Override
public String get(String key) {
return getOperator().opsForValue().get(key);
}
@Override
public T get(String key, Type type) {
return gson.fromJson(getOperator().opsForValue().get(key), type);
}
@Override
public void save(String key, Object value) {
getOperator().opsForValue().set(key, gson.toJson(value));
}
...
@Override
public boolean hasKey(String key) {
return getOperator().hasKey(key);
}
@Override
public void delete(String key) {
getOperator().delete(key);
}
private RedisOperations<String, String> getOperator() {
try {
ValueOperations<String, String> vop = redisTemplate.opsForValue();
return vop.getOperations();
} catch (Exception e) {
throw new GatewayRedisOperationException(e.getMessage(), e);
}
}
}
Service
@Component
@RequiredArgsConstructor
public class RedisCrud {
private final RedisCrudProvider redisCrudProvider;
public String get(String key) {
return ofNullable(redisCrudProvider.get(key)).orElse("default");
}
public void set(String key, Object value) {
redisCrudProvider.save(key, value);
}
}
Test
@RunWith(MockitoJUnitRunner.class)
public class RedisVerifierTest {
@InjectMocks
private RedisCrud sut;
@Mock
private RedisCrudProvider redisCrudProvider;
@Test
public void get() {
//given
String redisKey = "testKey";
//then
assertThat(sut.get(redisKey)).isEqualTo("testValue");
}
}
정리
의존성 충돌을 조심해야 한다.... monog-reactive쪽과 충돌....일반 mongo로 변경했더니 의존성 충돌이 나지 않았다.
Redis에는 Map, List 다 들어가서 편리하다.
MVC 했으니 WebFlux...도 조만간..ㅎ
728x90
반응형
LIST
'Develope > Cache' 카테고리의 다른 글
[Redis] Tuning - 간단한 튜닝을 해보자! (0) | 2021.01.17 |
---|---|
[Redis] Redis Monitoring - 모니터링 툴 소개 (0) | 2021.01.17 |
[Redis] Cluster 구축 튜토리얼 - Step By Step Run Book (0) | 2021.01.17 |
[Redis] redis-cli 명령어 모음 (0) | 2021.01.17 |
[Redis] AOF vs RDB - Redis 메모리 관리 방식 비교 (0) | 2021.01.17 |