1. 修正了 redis-starter 中 cache 反序列化的问题
2. 增加了 redis-cache 实例
This commit is contained in:
31
examples/redis-cache-example/pom.xml
Normal file
31
examples/redis-cache-example/pom.xml
Normal file
@ -0,0 +1,31 @@
|
||||
<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>com.chinaunicom.ebtp</groupId>
|
||||
<artifactId>mall-ebtp-cloud-parent</artifactId>
|
||||
<version>0.0.1</version>
|
||||
</parent>
|
||||
<groupId>com.chinaunicom.mall.ebtp.cloud</groupId>
|
||||
<artifactId>redis-cache-example</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>redis-cache-example</name>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.chinaunicom.ebtp</groupId>
|
||||
<artifactId>mall-ebtp-cloud-redis-starter</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
@ -0,0 +1,13 @@
|
||||
package com.chinaunicom.mall.ebtp.cloud.redis.cache.example;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableCaching
|
||||
public class RedisCacheExampleApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(RedisCacheExampleApplication.class, args);
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package com.chinaunicom.mall.ebtp.cloud.redis.cache.example.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class User implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private Integer id;
|
||||
private String username;
|
||||
private Integer age;
|
||||
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.chinaunicom.mall.ebtp.cloud.redis.cache.example.service;
|
||||
|
||||
import org.springframework.cache.annotation.CacheConfig;
|
||||
import org.springframework.cache.annotation.CacheEvict;
|
||||
import org.springframework.cache.annotation.CachePut;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
|
||||
import com.chinaunicom.mall.ebtp.cloud.redis.cache.example.model.User;
|
||||
|
||||
@CacheConfig(cacheNames = "user")
|
||||
public interface ExampleService {
|
||||
|
||||
@CachePut(key = "#user.id")
|
||||
User update(User user);
|
||||
|
||||
@CacheEvict(key = "#id", allEntries = true)
|
||||
void deleteById(Integer id);
|
||||
|
||||
@Cacheable(key = "#id")
|
||||
User queryById(Integer id);
|
||||
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
package com.chinaunicom.mall.ebtp.cloud.redis.cache.example.service.impl;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.chinaunicom.mall.ebtp.cloud.redis.cache.example.model.User;
|
||||
import com.chinaunicom.mall.ebtp.cloud.redis.cache.example.service.ExampleService;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class ExampleServiceImpl implements ExampleService {
|
||||
private ConcurrentHashMap<Integer, User> userRepo = new ConcurrentHashMap<>();
|
||||
|
||||
ExampleServiceImpl() {
|
||||
log.info("Example database initial");
|
||||
|
||||
for (int i = 1; i <= 10; i++) {
|
||||
userRepo.put(i, new User(i, "test" + i, i + 20));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public User update(User user) {
|
||||
log.info(String.format("Find user by id [%s]", user.getId()));
|
||||
User target = userRepo.get(user.getId());
|
||||
if (!Objects.isNull(target)) {
|
||||
BeanUtils.copyProperties(user, target);
|
||||
}
|
||||
return target;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteById(Integer id) {
|
||||
log.info(String.format("Delete user by id [%s]", id));
|
||||
userRepo.remove(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public User queryById(Integer id) {
|
||||
log.info(String.format("Query user by id [%s]", id));
|
||||
// List<User> list = new ArrayList<User>();
|
||||
//
|
||||
// for(int i = 1; i < id; i++) {
|
||||
// list.add(userRepo.get(i));
|
||||
// }
|
||||
// return list;
|
||||
return userRepo.get(id);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
spring:
|
||||
redis:
|
||||
database: 0
|
||||
host: 125.32.114.204
|
||||
password: redis@CC1234
|
||||
port: 16379
|
||||
timeout: 6000
|
||||
ssl: false
|
||||
lettuce:
|
||||
pool:
|
||||
max-wait: -1ms
|
||||
max-active: 8
|
||||
max-idle: 8
|
||||
min-idle: 0
|
||||
cache:
|
||||
cacheNames: demo
|
@ -0,0 +1,10 @@
|
||||
spring:
|
||||
profiles:
|
||||
active: redis
|
||||
application:
|
||||
name: mall-ebtp-cloud-demo
|
||||
|
||||
logging:
|
||||
level:
|
||||
root: info
|
||||
|
@ -0,0 +1,41 @@
|
||||
package com.chinaunicom.mall.ebtp.cloud.redis.cache.example;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.chinaunicom.mall.ebtp.cloud.redis.cache.example.service.ExampleService;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class RedisCacheExampleTest {
|
||||
|
||||
private @Autowired ExampleService service;
|
||||
|
||||
@Test
|
||||
public void redisCacheTest() {
|
||||
System.out.println(Objects.toString(service.queryById(1)));
|
||||
|
||||
// // 相同的主键再次查询, 查看输入是否走了缓存
|
||||
System.out.println(Objects.toString(service.queryById(1)));
|
||||
//
|
||||
// // 使用其它id进行查询
|
||||
// System.out.println(Objects.toString(service.queryById(2)));
|
||||
//
|
||||
// // 修改数据, 检查缓存是否更新
|
||||
// System.out.println(Objects.toString(service.update(new User(2, "r42wer", 25))));
|
||||
//
|
||||
// // 查询最新的数据
|
||||
// System.out.println(Objects.toString(service.queryById(2)));
|
||||
//
|
||||
// service.deleteById(2);
|
||||
//
|
||||
// // 测试数据删除时是否同时刷新了缓存
|
||||
// System.out.println(Objects.toString(service.queryById(2)));
|
||||
}
|
||||
|
||||
}
|
@ -1,7 +1,3 @@
|
||||
server:
|
||||
port: 8762
|
||||
max-http-header-size: 1000000
|
||||
|
||||
spring:
|
||||
redis:
|
||||
database: 0
|
||||
|
@ -9,6 +9,7 @@ import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* redis序列化器
|
||||
*
|
||||
* @author xsx
|
||||
* @date 2019/7/5
|
||||
* @since 1.8
|
||||
@ -17,6 +18,7 @@ public class JsonRedisSerializer implements RedisSerializer<Object> {
|
||||
|
||||
/**
|
||||
* 序列化
|
||||
*
|
||||
* @param t 对象
|
||||
* @return 返回字节数组
|
||||
* @throws SerializationException 序列化异常
|
||||
@ -29,7 +31,7 @@ public class JsonRedisSerializer implements RedisSerializer<Object> {
|
||||
byte[] bytes;
|
||||
if (this.isSimpleType(t)) {
|
||||
bytes = t.toString().getBytes(StandardCharsets.UTF_8);
|
||||
}else {
|
||||
} else {
|
||||
try {
|
||||
bytes = JSONUtil.toJsonStr(t).getBytes(StandardCharsets.UTF_8);
|
||||
} catch (Exception ex) {
|
||||
@ -41,6 +43,7 @@ public class JsonRedisSerializer implements RedisSerializer<Object> {
|
||||
|
||||
/**
|
||||
* 反序列化
|
||||
*
|
||||
* @param bytes 字节数组
|
||||
* @return 返回对象
|
||||
* @throws SerializationException 序列化异常
|
||||
@ -52,6 +55,7 @@ public class JsonRedisSerializer implements RedisSerializer<Object> {
|
||||
|
||||
/**
|
||||
* 尝试反序列化
|
||||
*
|
||||
* @param bytes 字节数组
|
||||
* @return 返回对象
|
||||
* @throws SerializationException 序列化异常
|
||||
@ -63,28 +67,22 @@ public class JsonRedisSerializer implements RedisSerializer<Object> {
|
||||
String str = new String(bytes, StandardCharsets.UTF_8);
|
||||
if (JSONUtil.isJsonArray(str)) {
|
||||
return JSONUtil.parseArray(str);
|
||||
}else if (JSONUtil.isJsonObj(str)) {
|
||||
} else if (JSONUtil.isJsonObj(str)) {
|
||||
return JSONUtil.parseObj(str);
|
||||
}else {
|
||||
return str;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否简单类型
|
||||
*
|
||||
* @param t 实体
|
||||
* @return 返回布尔值
|
||||
*/
|
||||
private boolean isSimpleType(Object t) {
|
||||
return SimpleType.BYTE.isValue(t) ||
|
||||
SimpleType.CHARACTER.isValue(t) ||
|
||||
SimpleType.SHORT.isValue(t) ||
|
||||
SimpleType.INTEGER.isValue(t) ||
|
||||
SimpleType.LONG.isValue(t) ||
|
||||
SimpleType.FLOAT.isValue(t) ||
|
||||
SimpleType.DOUBLE.isValue(t) ||
|
||||
SimpleType.BOOLEAN.isValue(t) ||
|
||||
SimpleType.BIGINTEGER.isValue(t) ||
|
||||
SimpleType.BIGDECIMAL.isValue(t);
|
||||
return SimpleType.BYTE.isValue(t) || SimpleType.CHARACTER.isValue(t) || SimpleType.SHORT.isValue(t)
|
||||
|| SimpleType.INTEGER.isValue(t) || SimpleType.LONG.isValue(t) || SimpleType.FLOAT.isValue(t)
|
||||
|| SimpleType.DOUBLE.isValue(t) || SimpleType.BOOLEAN.isValue(t) || SimpleType.BIGINTEGER.isValue(t)
|
||||
|| SimpleType.BIGDECIMAL.isValue(t);
|
||||
}
|
||||
}
|
||||
|
@ -15,6 +15,7 @@ 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.serializer.JdkSerializationRedisSerializer;
|
||||
import org.springframework.data.redis.serializer.RedisSerializationContext;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
@ -33,7 +34,6 @@ import com.chinaunicom.mall.ebtp.cloud.redis.starter.config.JsonRedisSerializer;
|
||||
@ConditionalOnMissingBean(CacheManager.class)
|
||||
@EnableConfigurationProperties({ CacheProperties.class })
|
||||
public class RedisCacheConfiguration {
|
||||
|
||||
@Bean
|
||||
RedisCacheManager cacheManager(CacheProperties cacheProperties,
|
||||
ObjectProvider<org.springframework.data.redis.cache.RedisCacheConfiguration> redisCacheConfiguration,
|
||||
@ -41,13 +41,15 @@ public class RedisCacheConfiguration {
|
||||
RedisConnectionFactory redisConnectionFactory) {
|
||||
RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager.builder(redisConnectionFactory)
|
||||
.cacheDefaults(determineConfiguration(cacheProperties, redisCacheConfiguration));
|
||||
// 缓存名称
|
||||
// 应用指定的缓存名称
|
||||
List<String> cacheNames = cacheProperties.getCacheNames();
|
||||
// 初始化缓存名称
|
||||
if (!cacheNames.isEmpty()) {
|
||||
builder.initialCacheNames(new LinkedHashSet<>(cacheNames));
|
||||
}
|
||||
// 以链模式自定义化builder
|
||||
redisCacheManagerBuilderCustomizers.orderedStream().forEach(customizer -> customizer.customize(builder));
|
||||
|
||||
return new CacheManagerCustomizers(null).customize(builder.build());
|
||||
}
|
||||
|
||||
@ -79,7 +81,7 @@ public class RedisCacheConfiguration {
|
||||
.defaultCacheConfig();
|
||||
// 替换默认的序列化工具
|
||||
config = config.serializeValuesWith(
|
||||
RedisSerializationContext.SerializationPair.fromSerializer(new JsonRedisSerializer()));
|
||||
RedisSerializationContext.SerializationPair.fromSerializer(new JdkSerializationRedisSerializer(getClass().getClassLoader())));
|
||||
if (redisProperties.getTimeToLive() != null) {
|
||||
config = config.entryTtl(redisProperties.getTimeToLive());// 缓存有效时间
|
||||
}
|
||||
|
Reference in New Issue
Block a user