初始化提交
This commit is contained in:
32
cooperop-base-cache/pom.xml
Normal file
32
cooperop-base-cache/pom.xml
Normal file
@ -0,0 +1,32 @@
|
||||
<?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>
|
||||
|
||||
<parent>
|
||||
<groupId>business.chaoran</groupId>
|
||||
<artifactId>cooperop-demo-base</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<groupId>business.chaoran</groupId>
|
||||
<artifactId>cooperop-base-cache</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!--redis序列化用fastjson,业务返回Json还是用gson更加灵活-->
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@ -0,0 +1,46 @@
|
||||
package business.cooperop.base.cache.redis;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.serializer.SerializerFeature;
|
||||
import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
|
||||
import org.springframework.data.redis.serializer.SerializationException;
|
||||
|
||||
/*
|
||||
**********************************************
|
||||
* DATE PERSON REASON
|
||||
* 2020-09-27 FXY Created
|
||||
**********************************************
|
||||
*/
|
||||
|
||||
/**
|
||||
* Fastjson集成 spring-data-redis 扩展
|
||||
*
|
||||
* @author FXY
|
||||
* <p>
|
||||
* 2018年1月23日
|
||||
*/
|
||||
|
||||
public class ExtGenericFastJsonRedisSerializer extends GenericFastJsonRedisSerializer {
|
||||
|
||||
private static int NO_SkipTransientFieldFEATURE;
|
||||
|
||||
{
|
||||
int features = SerializerFeature.config(JSON.DEFAULT_GENERATE_FEATURE, SerializerFeature.SkipTransientField, false);
|
||||
NO_SkipTransientFieldFEATURE = SerializerFeature.config(features, SerializerFeature.WriteClassName, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] serialize(Object object) throws SerializationException {
|
||||
|
||||
if (object == null) {
|
||||
return new byte[0];
|
||||
}
|
||||
try {
|
||||
return JSON.toJSONBytes(object, NO_SkipTransientFieldFEATURE);
|
||||
} catch (Exception ex) {
|
||||
throw new SerializationException("Could not serialize: " + ex.getMessage(), ex);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
37
cooperop-base-cache/src/main/java/business/cooperop/base/cache/redis/RedisConfig.java
vendored
Normal file
37
cooperop-base-cache/src/main/java/business/cooperop/base/cache/redis/RedisConfig.java
vendored
Normal file
@ -0,0 +1,37 @@
|
||||
package business.cooperop.base.cache.redis;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
|
||||
@Configuration
|
||||
public class RedisConfig {
|
||||
@Bean
|
||||
public RedisManager redisManager(RedisTemplate redisTemplate) {
|
||||
RedisManager redisManager = new RedisManager();
|
||||
redisManager.setRedisTemplate(redisTemplate);
|
||||
return redisManager;
|
||||
}
|
||||
|
||||
@ConditionalOnMissingBean(RedisTemplate.class)
|
||||
@Bean("redisTemplate")
|
||||
public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
|
||||
|
||||
RedisTemplate redisTemplate = new RedisTemplate();
|
||||
redisTemplate.setConnectionFactory(factory);
|
||||
|
||||
StringRedisSerializer ss = new StringRedisSerializer();
|
||||
|
||||
//默认使用JdkSerializationRedisSerializer对value进行序列化
|
||||
//如果想要保证redis中数据的可读性,可以自定义序列化方式
|
||||
ExtGenericFastJsonRedisSerializer redisSerializer = new ExtGenericFastJsonRedisSerializer();
|
||||
redisTemplate.setKeySerializer(ss);
|
||||
redisTemplate.setValueSerializer(redisSerializer);
|
||||
redisTemplate.setHashKeySerializer(ss);
|
||||
redisTemplate.setHashValueSerializer(redisSerializer);
|
||||
return redisTemplate;
|
||||
}
|
||||
}
|
||||
68
cooperop-base-cache/src/main/java/business/cooperop/base/cache/redis/RedisManager.java
vendored
Normal file
68
cooperop-base-cache/src/main/java/business/cooperop/base/cache/redis/RedisManager.java
vendored
Normal file
@ -0,0 +1,68 @@
|
||||
package business.cooperop.base.cache.redis;
|
||||
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.ValueOperations;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* RedisManager
|
||||
*
|
||||
* @author FXY
|
||||
* <p>
|
||||
* 2018年1月23日
|
||||
*/
|
||||
public class RedisManager {
|
||||
|
||||
/**
|
||||
* 默认过期时长,单位:秒
|
||||
*/
|
||||
public final static long DEFAULT_EXPIRE = 60 * 30 * 1;
|
||||
/**
|
||||
* 不设置过期时长
|
||||
*/
|
||||
public final static long NOT_EXPIRE = -1;
|
||||
|
||||
private RedisTemplate redisTemplate;
|
||||
|
||||
|
||||
public void set(String key, Object value, long expire) {
|
||||
try {
|
||||
if (expire == NOT_EXPIRE) {
|
||||
redisTemplate.opsForValue().set(key, value);
|
||||
} else {
|
||||
redisTemplate.opsForValue().set(key, value, expire, TimeUnit.SECONDS);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void set(String key, Object value) {
|
||||
set(key, value, DEFAULT_EXPIRE);
|
||||
}
|
||||
|
||||
public <T> T get(String key, Class<T> clazz) {
|
||||
ValueOperations<String, T> operations = redisTemplate.opsForValue();
|
||||
return operations.get(key);
|
||||
|
||||
}
|
||||
|
||||
public Object get(String key) {
|
||||
return redisTemplate.opsForValue().get(key);
|
||||
}
|
||||
|
||||
public void delete(String key) {
|
||||
redisTemplate.delete(key);
|
||||
}
|
||||
|
||||
public RedisTemplate<String, Object> getRedisTemplate() {
|
||||
return redisTemplate;
|
||||
}
|
||||
|
||||
public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
|
||||
this.redisTemplate = redisTemplate;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user