|
1 | 1 | package com.ody.common.config;
|
2 | 2 |
|
3 |
| -import java.time.Duration; |
4 |
| -import org.redisson.Redisson; |
5 |
| -import org.redisson.api.RedissonClient; |
6 |
| -import org.redisson.config.Config; |
7 |
| -import org.redisson.spring.data.connection.RedissonConnectionFactory; |
8 | 3 | import org.springframework.beans.factory.annotation.Value;
|
9 |
| -import org.springframework.cache.CacheManager; |
10 | 4 | import org.springframework.cache.annotation.EnableCaching;
|
11 | 5 | import org.springframework.context.annotation.Bean;
|
12 | 6 | import org.springframework.context.annotation.Configuration;
|
13 | 7 | import org.springframework.context.annotation.Profile;
|
14 |
| -import org.springframework.data.redis.cache.RedisCacheConfiguration; |
15 |
| -import org.springframework.data.redis.cache.RedisCacheManager; |
16 | 8 | import org.springframework.data.redis.connection.RedisConnectionFactory;
|
17 |
| -import org.springframework.data.redis.core.RedisTemplate; |
18 |
| -import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; |
19 |
| -import org.springframework.data.redis.serializer.RedisSerializationContext; |
20 |
| -import org.springframework.data.redis.serializer.StringRedisSerializer; |
| 9 | +import org.springframework.data.redis.connection.RedisStandaloneConfiguration; |
| 10 | +import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; |
| 11 | +import org.springframework.data.redis.listener.RedisMessageListenerContainer; |
21 | 12 |
|
22 | 13 | @Profile({"local", "dev", "prod"})
|
23 | 14 | @Configuration
|
24 | 15 | @EnableCaching
|
25 | 16 | public class RedisConfig {
|
26 | 17 |
|
27 |
| - private static final String REDISSON_HOST_PREFIX = "redis://"; |
28 |
| - private static final int THREE_SECOND = 3000; |
29 |
| - |
30 | 18 | @Value("${spring.data.redis.host}")
|
31 | 19 | private String redisHost;
|
32 | 20 |
|
33 | 21 | @Value("${spring.data.redis.port}")
|
34 | 22 | private int redisPort;
|
35 | 23 |
|
36 | 24 | @Bean
|
37 |
| - public RedissonClient redissonClient() { |
38 |
| - Config config = new Config(); |
39 |
| - config.useSingleServer() |
40 |
| - .setAddress(REDISSON_HOST_PREFIX + redisHost + ":" + redisPort) |
41 |
| - .setConnectTimeout(THREE_SECOND) |
42 |
| - .setRetryAttempts(3); |
43 |
| - |
44 |
| - return Redisson.create(config); |
45 |
| - } |
46 |
| - |
47 |
| - @Bean |
48 |
| - public RedisTemplate<String, Object> redisTemplate() { |
49 |
| - RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); |
50 |
| - redisTemplate.setConnectionFactory(new RedissonConnectionFactory(redissonClient())); |
51 |
| - redisTemplate.setDefaultSerializer(new StringRedisSerializer()); |
52 |
| - return redisTemplate; |
53 |
| - } |
54 |
| - |
55 |
| - @Bean |
56 |
| - public RedisCacheConfiguration redisCacheConfiguration() { |
57 |
| - return RedisCacheConfiguration.defaultCacheConfig() |
58 |
| - .serializeKeysWith( |
59 |
| - RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()) |
60 |
| - ) |
61 |
| - .serializeValuesWith( |
62 |
| - RedisSerializationContext.SerializationPair.fromSerializer( |
63 |
| - new GenericJackson2JsonRedisSerializer() |
64 |
| - ) |
65 |
| - ) |
66 |
| - .entryTtl(Duration.ofHours(1L)); |
| 25 | + public RedisConnectionFactory redisConnectionFactory() { |
| 26 | + RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(redisHost, redisPort); |
| 27 | + return new LettuceConnectionFactory(config); |
67 | 28 | }
|
68 | 29 |
|
69 | 30 | @Bean
|
70 |
| - public CacheManager cacheManager(RedisConnectionFactory cf) { |
71 |
| - return RedisCacheManager.RedisCacheManagerBuilder |
72 |
| - .fromConnectionFactory(cf) |
73 |
| - .cacheDefaults(redisCacheConfiguration()) |
74 |
| - .build(); |
| 31 | + public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory redisConnectionFactory) { |
| 32 | + RedisMessageListenerContainer container = new RedisMessageListenerContainer(); |
| 33 | + container.setConnectionFactory(redisConnectionFactory); |
| 34 | + return container; |
75 | 35 | }
|
76 | 36 | }
|
0 commit comments