1. 修正了 redis-starter 中 cache 反序列化的问题

2. 增加了 redis-cache 实例
This commit is contained in:
Administrator
2020-11-03 16:02:24 +08:00
parent 101e1a87ce
commit b8b47bf595
11 changed files with 279 additions and 76 deletions

View 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>

View File

@ -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);
}
}

View File

@ -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;
}

View File

@ -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);
}

View File

@ -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);
}
}

View File

@ -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

View File

@ -0,0 +1,10 @@
spring:
profiles:
active: redis
application:
name: mall-ebtp-cloud-demo
logging:
level:
root: info

View File

@ -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)));
}
}