This commit is contained in:
2019-08-15 01:25:05 +08:00
parent 3c56288991
commit 7242f6d76c
74 changed files with 4375 additions and 126 deletions
@@ -0,0 +1,67 @@
package cn.odboy.zilean.cache;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* @author: Odboy
* @version: Jdk 1.8
* @desc: TODO Redis配置.自定义redis序列化
* @time: 2019/8/8 10:39
* @blog: www.odboy.cn
*/
@EnableCaching
@Configuration
@ConditionalOnClass(RedisOperations.class)
@EnableConfigurationProperties(RedisProperties.class)
public class RedisConfig extends CachingConfigurerSupport {
/**
* Redis工厂
*
* @param factory RedisConnectionFactory
* @return
*/
@Bean(name = "redisTemplate")
@SuppressWarnings("unchecked")
@ConditionalOnMissingBean(name = "redisTemplate")
public RedisTemplate<Object, Object> redisTemplate(
RedisConnectionFactory factory) {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
// 使用fastjson序列化
RedisFastJsonSerializerConfig fastJsonRedisSerializerConfig =
new RedisFastJsonSerializerConfig(Object.class);
// value值的序列化采用fastJsonRedisSerializer
template.setValueSerializer(fastJsonRedisSerializerConfig);
template.setHashValueSerializer(fastJsonRedisSerializerConfig);
// key的序列化采用StringRedisSerializer
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setConnectionFactory(factory);
return template;
}
/**
* 缓存管理器
*
* @param factory RedisConnectionFactory
* @return
*/
@Bean
public CacheManager cacheManager(RedisConnectionFactory factory) {
RedisCacheManager.RedisCacheManagerBuilder builder =
RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(factory);
return builder.build();
}
}
@@ -0,0 +1,50 @@
package cn.odboy.zilean.cache;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import java.nio.charset.Charset;
/**
* @author: Odboy
* @version: Jdk 1.8
* @desc: TODO Redis序列化器之Fastjson版
* @time: 2019/8/8 10:39
* @blog: www.odboy.cn
*/
public class RedisFastJsonSerializerConfig<T> implements RedisSerializer<T> {
private static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
private Class<T> clazz;
static {
// 解决fastJson autoType is not support错误
// ParserConfig.getGlobalInstance().addAccept("cn.odboy.zilean.base");
// ParserConfig.getGlobalInstance().addAccept("cn.odboy.zilean.bms.cn.odboy.zilean.entity");
ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
}
public RedisFastJsonSerializerConfig(Class<T> clazz) {
super();
this.clazz = clazz;
}
@Override
public byte[] serialize(T t) throws SerializationException {
if (null == t) {
return new byte[0];
}
return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET);
}
@Override
public T deserialize(byte[] bytes) throws SerializationException {
if (null == bytes || bytes.length <= 0) {
return null;
}
String str = new String(bytes, DEFAULT_CHARSET);
return JSON.parseObject(str, clazz);
}
}
@@ -0,0 +1,29 @@
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
<!--
这个是磁盘存储路径,当内存缓存满了的时候,就会往这里面放
java.io.tmdir是操作系统缓存的临时目录,不同操作系统缓存目录不一样
-->
<diskStore path="java.io.tmpdir"/>
<!-- defaultCache:echcache的默认缓存策略 -->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxElementsOnDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</defaultCache>
<cache name="userCache"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxElementsOnDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</cache>
</ehcache>