Redis Cache with spring
1. Redis Connection Factory
@Component @Configuration public class RedisConnectionConfig { @Value("${redis.host}") private String redisHost; @Value("${redis.port}") private int redisPort; @Bean public RedisConnectionFactory XXXRedisConnectionFactory() { JedisPoolConfig poolConfig = new JedisPoolConfig(); poolConfig.setMaxTotal(5); poolConfig.setJmxNamePrefix("redispool"); poolConfig.setJmxNameBase("redis.client.jedis:type=XXXSentinelPool,name="); JedisConnectionFactory connectionFactory = new JedisConnectionFactory(poolConfig); connectionFactory.setUsePool(true); connectionFactory.setHostName(redisHost); connectionFactory.setPort(redisPort); return connectionFactory; } } |
2. RedisTemplate extends
@Getter @Setter public class XXXRedisTemplate<K, V> extends RedisTemplate { private String cacheName; } |
3. RedisSerializer impletemt
@Slf4j public class XXXRedisSingleSerializer<T> implements RedisSerializer<Object> { private Class<?> clazz; private ObjectMapper objectMapper = new ObjectMapper(); public XXXRedisSingleSerializer(Class<?> clazz) { this.clazz = clazz; } @Override public byte[] serialize(Object o) throws SerializationException { try { return objectMapper.writeValueAsBytes(o); } catch (IOException e) { log.warn("serialize exception list : {}, message : {}", o, e.getMessage(), e); } return null; } @Override public T deserialize(byte[] bytes) throws SerializationException { try { Object o = objectMapper.readValue(bytes, objectMapper.getTypeFactory().constructType(clazz)); return (T) o; } catch (IOException e) { log.warn("deserialize exception data : {}, message : {}", bytes, e.getMessage(), e); } return null; } } |
@Slf4j public class XXXRedisSerializer implements RedisSerializer<List> { private Class<?> clazz; private ObjectMapper objectMapper = new ObjectMapper(); public XXXRedisSerializer(Class<?> clazz) { this.clazz = clazz; } @Override public byte[] serialize(List ts) throws SerializationException { try { return objectMapper.writeValueAsBytes(ts); } catch (IOException e) { log.warn("serialize exception list : {}, message : {}", ts, e.getMessage(), e); } return null; } @Override public List deserialize(byte[] bytes) throws SerializationException { try { List<?> list = objectMapper.readValue( bytes, objectMapper.getTypeFactory().constructCollectionType(List.class, clazz)); return list; } catch (IOException e) { log.warn("deserialize exception data : {}, message : {}", bytes, e.getMessage(), e); } return Lists.newArrayList(); } } |
4. RedisTemplate Bean 등록
@Configuration public class XXXRedisTemplateConfig { @Autowired @Qualifier("XXXRedisConnectionFactory") private RedisConnectionFactory XXXPortalRedisConnectionFactory; @Bean public XXXRedisTemplate<String, XXXdto1> xxx1RedisTemplate() { return redisTemplate(XXXdto1.class, XXXCacheConst.XXX1); }
@Bean public XXXRedisTemplate<String, xxx2Dto> xxx2Template() { XXXRedisTemplate<String, xxx2Dto> redisTemplate = new XXXRedisTemplate<>(); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer( new XXXRedisSingleSerializer<xxx2Dto>(xxx2Dto)); redisTemplate.setConnectionFactory(XXXRedisConnectionFactory); redisTemplate.setCacheName(XXXCacheConst.xxx2); return redisTemplate; } @NotNull private XXXRedisTemplate redisTemplate(Class<?> clazz, String cacheName) { XXXRedisTemplate redisTemplate = new XXXRedisTemplate<>(); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new XXXRedisSerializer(clazz)); redisTemplate.setConnectionFactory(XXXRedisConnectionFactory); redisTemplate.setCacheName(cacheName); return redisTemplate; } } |
5. CacheManager에 등록
@Autowired private List<XXXRedisTemplate> XXXRedisTemplates; @Bean public CacheManager cacheManager() { List<RedisCacheManager> cacheManagers = XXXRedisTemplates .stream() .map( redisTemplate -> { RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate); redisCacheManager.setCacheNames(Lists.newArrayList(redisTemplate.getCacheName())); redisCacheManager.setDefaultExpiration(300); redisCacheManager.afterPropertiesSet(); return redisCacheManager; }) .collect(Collectors.toList()); CompositeCacheManager compositeCacheManager = new CompositeCacheManager(); compositeCacheManager.setCacheManagers(Lists.newArrayList(cacheManagers)); return compositeCacheManager; } } |