# build.gradle
compile 'org.springframework.data:spring-data-redis'
compile 'biz.paluch.redis:lettuce:4.5.0.Final'
#RedisConfiguration
@Configuration@
EnableRedisRepositories(basePackageClasses = { MyAppDomains.class })
public class RedisConfiguration {
@Value("${spring.redis.host}")
private String redisHost;
@Value("${spring.redis.port}")
private int redisPort;
@Bean
public RedisConnectionFactory myRedisConnectionFactory() {
RedisClusterConfiguration clusterConfig = new RedisClusterConfiguration();
clusterConfig.clusterNode(redisHost, redisPort);
return new MyLettuceConnectionFactory(clusterConfig);
}
@Bean
@ConditionalOnMissingBean(name = "redisTemplate")
@Primary
public RedisTemplate<?, ?> redisTemplate(RedisConnectionFactory myRedisConnectionFactory) {
RedisTemplate<byte[], byte[]> template = new RedisTemplate<>();
template.setConnectionFactory(myRedisConnectionFactory);
return template;
}
@Bean
public CustomConversions redisCustomConversions() {
return new CustomConversions(Arrays.asList(new MyReadConverter(), new MyWriteConverter()));
}
}
# MyLettuceConnectionFactory
@Configuration
public class MyLettuceConnectionFactory extends LettuceConnectionFactory {
public MyLettuceConnectionFactory() { }
public MyLettuceConnectionFactory(RedisClusterConfiguration clusterConfig) {
super(clusterConfig);
}
@Override
public void afterPropertiesSet() {
super.afterPropertiesSet();
DirectFieldAccessor accessor = new DirectFieldAccessor(this);
AbstractRedisClient client = (AbstractRedisClient) accessor.getPropertyValue("client");
if(client instanceof RedisClusterClient){
RedisClusterClient clusterClient = (RedisClusterClient) client;
ClusterTopologyRefreshOptions topologyRefreshOptions = ClusterTopologyRefreshOptions.builder()
.enablePeriodicRefresh(10, TimeUnit.MINUTES)
.enableAllAdaptiveRefreshTriggers()
.build();
ClusterClientOptions clusterClientOptions = ClusterClientOptions.builder()
.validateClusterNodeMembership(false)
.topologyRefreshOptions(topologyRefreshOptions)
.build();
clusterClient.setOptions(clusterClientOptions);
}
}
}
# Entity
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@RedisHash("my:categories")
@ToString
public class Category {
@Id
private String id;
@Indexed
private int level;
@Indexed
private String parentId;
private Map<Integer, String> parentNameByLevel;
@Indexed
private String documentType;
}
# Converter
@Component
@WritingConverter
public class MyWriteConverter implements Converter<Category, byte[]> {
private final Jackson2JsonRedisSerializer<Category> serializer;
public MyWriteConverter() {
serializer = new Jackson2JsonRedisSerializer<>(Category.class);
serializer.setObjectMapper(new ObjectMapper());
}
@Override
public byte[] convert(Category value) {
return serializer.serialize(value);
}
}
@Component
@ReadingConverter
public class MyReadConverter implements Converter<byte[], Category> {
private final Jackson2JsonRedisSerializer<Category> serializer;
public MyReadConverter() {
serializer = new Jackson2JsonRedisSerializer<>(Category.class);
serializer.setObjectMapper(new ObjectMapper());
}
@Override
public Category convert(byte[] value) {
return serializer.deserialize(value);
}
}
# Repository
public interface CategoryRepository extends CrudRepository<Category, String> {
List<Category> findByDocumentType(String documentType);
}
댓글
댓글 쓰기