init
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<!-- maven可以继承父项目中的参数,所以可以删除子模块中相同定义 -->
|
||||
<artifactId>zilean-cache</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<name>zilean-cache</name>
|
||||
<description>缓存模块</description>
|
||||
|
||||
<parent>
|
||||
<groupId>cn.odboy</groupId>
|
||||
<artifactId>zilean</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<!-- lookup parent from repository -->
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<!-- maven可以继承父项目中的依赖,所以可以删除子模块中相同定义 -->
|
||||
<!--lombok 依赖-->
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>${lombok.version}</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<!-- FastJson -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
<version>${fastjson.version}</version>
|
||||
</dependency>
|
||||
<!-- HuTool工具箱-->
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-all</artifactId>
|
||||
<version>${hutool.version}</version>
|
||||
</dependency>
|
||||
<!--redis 依赖-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
<version>${springboot.version}</version>
|
||||
</dependency>
|
||||
<!--redis-client 依赖-->
|
||||
<dependency>
|
||||
<groupId>redis.clients</groupId>
|
||||
<artifactId>jedis</artifactId>
|
||||
<version>${redis.client.version}</version>
|
||||
</dependency>
|
||||
<!-- Spring Boot 缓存支持启动器 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-cache</artifactId>
|
||||
<version>${springboot.version}</version>
|
||||
</dependency>
|
||||
<!-- Ehcache 坐标 -->
|
||||
<dependency>
|
||||
<groupId>net.sf.ehcache</groupId>
|
||||
<artifactId>ehcache</artifactId>
|
||||
<version>2.10.6</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<!-- 如果其他包需要依赖这个模块那么就不要加这个 -->
|
||||
<!--<build>-->
|
||||
<!--<plugins>-->
|
||||
<!--<plugin>-->
|
||||
<!--<groupId>org.springframework.boot</groupId>-->
|
||||
<!--<artifactId>spring-boot-maven-plugin</artifactId>-->
|
||||
<!--</plugin>-->
|
||||
<!--</plugins>-->
|
||||
<!--</build>-->
|
||||
</project>
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
+50
@@ -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>
|
||||
Reference in New Issue
Block a user