mirror of https://github.com/jeecgboot/jeecg-boot
修改redis配置,解决字典表无法使用问题
parent
8b1611abfd
commit
e2d9a66a48
|
@ -1,34 +1,18 @@
|
||||||
package org.jeecg.config;
|
package org.jeecg.config;
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.time.Duration;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
|
||||||
import org.springframework.cache.CacheManager;
|
|
||||||
import org.springframework.cache.annotation.CachingConfigurerSupport;
|
import org.springframework.cache.annotation.CachingConfigurerSupport;
|
||||||
import org.springframework.cache.annotation.EnableCaching;
|
import org.springframework.cache.annotation.EnableCaching;
|
||||||
import org.springframework.cache.concurrent.ConcurrentMapCache;
|
|
||||||
import org.springframework.cache.interceptor.KeyGenerator;
|
import org.springframework.cache.interceptor.KeyGenerator;
|
||||||
import org.springframework.cache.support.SimpleCacheManager;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.data.redis.cache.RedisCacheConfiguration;
|
|
||||||
import org.springframework.data.redis.cache.RedisCacheManager;
|
|
||||||
import org.springframework.data.redis.cache.RedisCacheWriter;
|
|
||||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
|
||||||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
|
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
|
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
|
||||||
import org.springframework.data.redis.serializer.RedisSerializationContext;
|
|
||||||
import org.springframework.data.redis.serializer.RedisSerializer;
|
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
|
||||||
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
|
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
|
||||||
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
@ -46,7 +30,6 @@ public class RedisConfig extends CachingConfigurerSupport {
|
||||||
* 只需要讲注解上keyGenerator的值设置为keyGenerator即可</br>
|
* 只需要讲注解上keyGenerator的值设置为keyGenerator即可</br>
|
||||||
* @return 自定义策略生成的key
|
* @return 自定义策略生成的key
|
||||||
*/
|
*/
|
||||||
@Override
|
|
||||||
@Bean
|
@Bean
|
||||||
public KeyGenerator keyGenerator() {
|
public KeyGenerator keyGenerator() {
|
||||||
return new KeyGenerator() {
|
return new KeyGenerator() {
|
||||||
|
@ -63,6 +46,14 @@ public class RedisConfig extends CachingConfigurerSupport {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 这个注释不能放开,发现自定义缓存管理器,会导致实体解析失败
|
||||||
|
//TODO
|
||||||
|
// @Bean
|
||||||
|
// public CacheManager cacheManager() {
|
||||||
|
// RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(lettuceConnectionFactory);
|
||||||
|
// return builder.build();
|
||||||
|
// }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* RedisTemplate配置
|
* RedisTemplate配置
|
||||||
*/
|
*/
|
||||||
|
@ -85,61 +76,5 @@ public class RedisConfig extends CachingConfigurerSupport {
|
||||||
redisTemplate.afterPropertiesSet();
|
redisTemplate.afterPropertiesSet();
|
||||||
return redisTemplate;
|
return redisTemplate;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 此处的缓存到JAVA虚拟机内存,非存Redis
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
@Bean
|
|
||||||
public CacheManager cacheManager() {
|
|
||||||
SimpleCacheManager cacheManager = new SimpleCacheManager();
|
|
||||||
List<ConcurrentMapCache> list=new ArrayList<ConcurrentMapCache>();
|
|
||||||
list.add(new ConcurrentMapCache("dictCache"));
|
|
||||||
list.add(new ConcurrentMapCache("jeecgDemo"));
|
|
||||||
list.add(new ConcurrentMapCache("permission"));
|
|
||||||
cacheManager.setCaches(list);
|
|
||||||
cacheManager.afterPropertiesSet();
|
|
||||||
return cacheManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Redis缓存支持设置缓存过期时间
|
|
||||||
* @param redisConnectionFactory
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
@Bean
|
|
||||||
public RedisCacheManager redisCacheManager(RedisConnectionFactory redisConnectionFactory) {
|
|
||||||
return new RedisCacheManager(
|
|
||||||
RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory),
|
|
||||||
this.getRedisCacheConfigurationWithTtl(3600), //3600秒,默认策略,未配置的 key 会使用这个
|
|
||||||
this.getRedisCacheConfigurationMap() // 指定 key 策略
|
|
||||||
);
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* 指定redis缓存超时时间
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
private Map<String, RedisCacheConfiguration> getRedisCacheConfigurationMap() {
|
|
||||||
Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>();
|
|
||||||
redisCacheConfigurationMap.put("dictTableCache", this.getRedisCacheConfigurationWithTtl(600));// 600秒(表数据字典只缓存10分钟)
|
|
||||||
|
|
||||||
return redisCacheConfigurationMap;
|
|
||||||
}
|
|
||||||
private RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Integer seconds) {
|
|
||||||
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
|
|
||||||
ObjectMapper om = new ObjectMapper();
|
|
||||||
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
|
||||||
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
|
|
||||||
jackson2JsonRedisSerializer.setObjectMapper(om);
|
|
||||||
|
|
||||||
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
|
|
||||||
redisCacheConfiguration = redisCacheConfiguration.serializeValuesWith(
|
|
||||||
RedisSerializationContext
|
|
||||||
.SerializationPair
|
|
||||||
.fromSerializer(jackson2JsonRedisSerializer)
|
|
||||||
).entryTtl(Duration.ofSeconds(seconds));
|
|
||||||
|
|
||||||
return redisCacheConfiguration;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue