Commit 29686df481e2a80f76fe9e85412388fa309ee0ab
1 parent
6d8696f4
修改redis配置,解决字典表无法使用问题
Showing
1 changed file
with
8 additions
and
73 deletions
jeecg-boot/src/main/java/org/jeecg/config/RedisConfig.java
1 | package org.jeecg.config; | 1 | package org.jeecg.config; |
2 | 2 | ||
3 | import java.lang.reflect.Method; | 3 | import java.lang.reflect.Method; |
4 | -import java.time.Duration; | ||
5 | -import java.util.ArrayList; | ||
6 | -import java.util.HashMap; | ||
7 | -import java.util.List; | ||
8 | -import java.util.Map; | ||
9 | 4 | ||
10 | import javax.annotation.Resource; | 5 | import javax.annotation.Resource; |
11 | - | ||
12 | -import org.springframework.cache.CacheManager; | ||
13 | import org.springframework.cache.annotation.CachingConfigurerSupport; | 6 | import org.springframework.cache.annotation.CachingConfigurerSupport; |
14 | import org.springframework.cache.annotation.EnableCaching; | 7 | import org.springframework.cache.annotation.EnableCaching; |
15 | -import org.springframework.cache.concurrent.ConcurrentMapCache; | ||
16 | import org.springframework.cache.interceptor.KeyGenerator; | 8 | import org.springframework.cache.interceptor.KeyGenerator; |
17 | -import org.springframework.cache.support.SimpleCacheManager; | ||
18 | import org.springframework.context.annotation.Bean; | 9 | import org.springframework.context.annotation.Bean; |
19 | import org.springframework.context.annotation.Configuration; | 10 | import org.springframework.context.annotation.Configuration; |
20 | -import org.springframework.data.redis.cache.RedisCacheConfiguration; | ||
21 | -import org.springframework.data.redis.cache.RedisCacheManager; | ||
22 | -import org.springframework.data.redis.cache.RedisCacheWriter; | ||
23 | -import org.springframework.data.redis.connection.RedisConnectionFactory; | ||
24 | import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; | 11 | import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; |
25 | import org.springframework.data.redis.core.RedisTemplate; | 12 | import org.springframework.data.redis.core.RedisTemplate; |
26 | import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; | 13 | import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; |
27 | -import org.springframework.data.redis.serializer.RedisSerializationContext; | ||
28 | import org.springframework.data.redis.serializer.RedisSerializer; | 14 | import org.springframework.data.redis.serializer.RedisSerializer; |
29 | import org.springframework.data.redis.serializer.StringRedisSerializer; | 15 | import org.springframework.data.redis.serializer.StringRedisSerializer; |
30 | - | ||
31 | -import com.fasterxml.jackson.annotation.JsonAutoDetect; | ||
32 | import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; | 16 | import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; |
33 | import com.fasterxml.jackson.annotation.PropertyAccessor; | 17 | import com.fasterxml.jackson.annotation.PropertyAccessor; |
34 | import com.fasterxml.jackson.databind.ObjectMapper; | 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
@@ -46,7 +30,6 @@ public class RedisConfig extends CachingConfigurerSupport { | @@ -46,7 +30,6 @@ public class RedisConfig extends CachingConfigurerSupport { | ||
46 | * 只需要讲注解上keyGenerator的值设置为keyGenerator即可</br> | 30 | * 只需要讲注解上keyGenerator的值设置为keyGenerator即可</br> |
47 | * @return 自定义策略生成的key | 31 | * @return 自定义策略生成的key |
48 | */ | 32 | */ |
49 | - @Override | ||
50 | @Bean | 33 | @Bean |
51 | public KeyGenerator keyGenerator() { | 34 | public KeyGenerator keyGenerator() { |
52 | return new KeyGenerator() { | 35 | return new KeyGenerator() { |
@@ -63,6 +46,14 @@ public class RedisConfig extends CachingConfigurerSupport { | @@ -63,6 +46,14 @@ public class RedisConfig extends CachingConfigurerSupport { | ||
63 | }; | 46 | }; |
64 | } | 47 | } |
65 | 48 | ||
49 | + // 这个注释不能放开,发现自定义缓存管理器,会导致实体解析失败 | ||
50 | + //TODO | ||
51 | +// @Bean | ||
52 | +// public CacheManager cacheManager() { | ||
53 | +// RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(lettuceConnectionFactory); | ||
54 | +// return builder.build(); | ||
55 | +// } | ||
56 | + | ||
66 | /** | 57 | /** |
67 | * RedisTemplate配置 | 58 | * RedisTemplate配置 |
68 | */ | 59 | */ |
@@ -85,61 +76,5 @@ public class RedisConfig extends CachingConfigurerSupport { | @@ -85,61 +76,5 @@ public class RedisConfig extends CachingConfigurerSupport { | ||
85 | redisTemplate.afterPropertiesSet(); | 76 | redisTemplate.afterPropertiesSet(); |
86 | return redisTemplate; | 77 | return redisTemplate; |
87 | } | 78 | } |
88 | - | ||
89 | - /** | ||
90 | - * 此处的缓存到JAVA虚拟机内存,非存Redis | ||
91 | - */ | ||
92 | - @Override | ||
93 | - @Bean | ||
94 | - public CacheManager cacheManager() { | ||
95 | - SimpleCacheManager cacheManager = new SimpleCacheManager(); | ||
96 | - List<ConcurrentMapCache> list=new ArrayList<ConcurrentMapCache>(); | ||
97 | - list.add(new ConcurrentMapCache("dictCache")); | ||
98 | - list.add(new ConcurrentMapCache("jeecgDemo")); | ||
99 | - list.add(new ConcurrentMapCache("permission")); | ||
100 | - cacheManager.setCaches(list); | ||
101 | - cacheManager.afterPropertiesSet(); | ||
102 | - return cacheManager; | ||
103 | - } | ||
104 | - | ||
105 | - | ||
106 | - /** | ||
107 | - * Redis缓存支持设置缓存过期时间 | ||
108 | - * @param redisConnectionFactory | ||
109 | - * @return | ||
110 | - */ | ||
111 | - @Bean | ||
112 | - public RedisCacheManager redisCacheManager(RedisConnectionFactory redisConnectionFactory) { | ||
113 | - return new RedisCacheManager( | ||
114 | - RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory), | ||
115 | - this.getRedisCacheConfigurationWithTtl(3600), //3600秒,默认策略,未配置的 key 会使用这个 | ||
116 | - this.getRedisCacheConfigurationMap() // 指定 key 策略 | ||
117 | - ); | ||
118 | - } | ||
119 | - /** | ||
120 | - * 指定redis缓存超时时间 | ||
121 | - * @return | ||
122 | - */ | ||
123 | - private Map<String, RedisCacheConfiguration> getRedisCacheConfigurationMap() { | ||
124 | - Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>(); | ||
125 | - redisCacheConfigurationMap.put("dictTableCache", this.getRedisCacheConfigurationWithTtl(600));// 600秒(表数据字典只缓存10分钟) | ||
126 | - | ||
127 | - return redisCacheConfigurationMap; | ||
128 | - } | ||
129 | - private RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Integer seconds) { | ||
130 | - Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class); | ||
131 | - ObjectMapper om = new ObjectMapper(); | ||
132 | - om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); | ||
133 | - om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); | ||
134 | - jackson2JsonRedisSerializer.setObjectMapper(om); | ||
135 | - | ||
136 | - RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig(); | ||
137 | - redisCacheConfiguration = redisCacheConfiguration.serializeValuesWith( | ||
138 | - RedisSerializationContext | ||
139 | - .SerializationPair | ||
140 | - .fromSerializer(jackson2JsonRedisSerializer) | ||
141 | - ).entryTtl(Duration.ofSeconds(seconds)); | ||
142 | 79 | ||
143 | - return redisCacheConfiguration; | ||
144 | - } | ||
145 | } | 80 | } |