## spring-boot-demo-cache-redis
> 此 demo 主要演示了 Spring Boot 如何整合 redis,操作redis中的数据,并使用redis缓存数据。连接池使用 Lettuce。
### 1.开发步骤
#### 1.1.添加依赖
```xml
com.xkcoding
common-tools
com.xkcoding
demo-cache-api
1.0.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-data-redis
org.apache.commons
commons-pool2
org.springframework.boot
spring-boot-starter-test
test
org.projectlombok
lombok
true
```
#### 1.2.配置文件
```yaml
spring:
redis:
host: localhost
# 连接超时时间(记得添加单位,Duration)
timeout: 10000ms
# Redis默认情况下有16个分片,这里配置具体使用的分片
# database: 0
lettuce:
pool:
# 连接池最大连接数(使用负值表示没有限制) 默认 8
max-active: 8
# 连接池最大阻塞等待时间(使用负值表示没有限制) 默认 -1
max-wait: -1ms
# 连接池中的最大空闲连接 默认 8
max-idle: 8
# 连接池中的最小空闲连接 默认 0
min-idle: 0
cache:
# 一般来说是不用配置的,Spring Cache 会根据依赖的包自行装配
type: redis
logging:
level:
com.xkcoding: debug
```
#### 1.3.自动装配Redis缓存管理
```java
@EnableCaching
@Configuration
@AutoConfigureAfter(RedisAutoConfiguration.class)
public class RedisCacheAutoConfiguration {
/**
* 默认情况下的模板只能支持RedisTemplate,也就是只能存入字符串,因此支持序列化
*/
@Bean
public RedisTemplate redisCacheTemplate(LettuceConnectionFactory redisConnectionFactory) {
RedisTemplate template = new RedisTemplate<>();
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setConnectionFactory(redisConnectionFactory);
return template;
}
/**
* 配置使用注解的时候缓存配置,默认是序列化反序列化的形式,加上此配置则为 json 形式
*/
@Bean
public CacheManager cacheManager(RedisConnectionFactory factory) {
// 配置序列化
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
RedisCacheConfiguration redisCacheConfiguration =
config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
return RedisCacheManager.builder(factory).cacheDefaults(redisCacheConfiguration).build();
}
}
```
#### 1.4.缓存通过注解实现
> 为了减少重复代码,该部分我将其抽取实现在 demo-cache-api 模块中
```java
@Slf4j
@Service
public class UserService {
/**
* 模拟数据库
*/
private static final Map DATABASES = new HashMap<>() {{
put(1L, new User(1L, "user1"));
put(2L, new User(2L, "user2"));
put(3L, new User(3L, "user3"));
}};
/**
* 保存或修改用户
*
* @param user 用户对象
* @return 操作结果
*/
@CachePut(value = "user", key = "#user.id")
public User saveOrUpdate(User user) {
DATABASES.put(user.getId(), user);
log.info("保存用户【user】= {}", user);
return user;
}
/**
* 获取用户
*
* @param id key值
* @return 返回结果
*/
@Cacheable(value = "user", key = "#id")
public User get(Long id) {
// 我们假设从数据库读取
log.info("查询用户【id】= {}", id);
return DATABASES.get(id);
}
/**
* 删除
*
* @param id key值
*/
@CacheEvict(value = "user", key = "#id")
public void delete(Long id) {
DATABASES.remove(id);
log.info("删除用户【id】= {}", id);
}
}
```
### 2.测试
#### 2.1.环境搭建
主要是 redis 环境的搭建,这里我提供了 docker-compose 文件,方便同学们一键启动测试环境
```bash
$ cd demo-cache/demo-cache-redis
$ docker compose -f docker-compose.env.yml up
```
#### 2.2.测试 Redis 基础功能
> 主要测试使用 `RedisTemplate` 操作 `Redis` 中的数据,查看是否正常序列化:
>
> - opsForValue:对应 String(字符串)
> - opsForZSet:对应 ZSet(有序集合)
> - opsForHash:对应 Hash(哈希)
> - opsForList:对应 List(列表)
> - opsForSet:对应 Set(集合)
> - opsForGeo:** 对应 GEO(地理位置)
```java
@Slf4j
@SpringBootTest
public class RedisTest {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
private RedisTemplate redisCacheTemplate;
/**
* 测试 Redis 操作
*/
@Test
public void get() throws InterruptedException {
// 测试线程安全,程序结束查看redis中count的值是否为1000
ExecutorService executorService = Executors.newFixedThreadPool(1000);
CountDownLatch wait = new CountDownLatch(1000);
IntStream.range(0, 1000).forEach(i -> executorService.execute(() -> {
stringRedisTemplate.opsForValue().increment("count", 1);
wait.countDown();
}));
wait.await();
log.debug("【count】= {}", stringRedisTemplate.opsForValue().getAndExpire("count", Duration.ofSeconds(10)));
stringRedisTemplate.opsForValue().set("k1", "v1");
String k1 = stringRedisTemplate.opsForValue().getAndExpire("k1", Duration.ofSeconds(10));
log.debug("【k1】= {}", k1);
// 以下演示整合,具体Redis命令可以参考官方文档
String key = "xkcoding:user:1";
redisCacheTemplate.opsForValue().set(key, new User(1L, "user1"));
// 对应 String(字符串)
User user = (User) redisCacheTemplate.opsForValue().get(key);
String userSerialized = stringRedisTemplate.opsForValue().getAndExpire(key, Duration.ofSeconds(10));
log.debug("【user】= {}", user);
log.debug("【userSerialized】= {}", userSerialized);
}
}
```
#### 2.3.测试Redis缓存是否生效
```java
@Slf4j
@SpringBootTest
public class UserServiceTest {
@Autowired
private UserService userService;
/**
* 获取两次,查看日志验证缓存
*/
@Test
public void getTwice() {
// 模拟查询id为1的用户
User user1 = userService.get(1L);
log.debug("【user1】= {}", user1);
// 再次查询
User user2 = userService.get(1L);
log.debug("【user2】= {}", user2);
// 查看日志,只打印一次日志,证明缓存生效
}
/**
* 先存,再查询,查看日志验证缓存
*/
@Test
public void getAfterSave() {
userService.saveOrUpdate(new User(4L, "测试中文"));
User user = userService.get(4L);
log.debug("【user】= {}", user);
// 查看日志,只打印保存用户的日志,查询是未触发查询日志,因此缓存生效
}
/**
* 测试删除,查看redis是否存在缓存数据
*/
@Test
public void deleteUser() {
// 查询一次,使redis中存在缓存数据
userService.get(1L);
// 删除,查看redis是否存在缓存数据
userService.delete(1L);
}
}
```
### 3.参考
- [Spring Boot 官方文档之连接 Redis](https://docs.spring.io/spring-boot/docs/3.0.0-M4/reference/htmlsingle/#data.nosql.redis)
- [Spring Boot 官方文档之 Redis 缓存](https://docs.spring.io/spring-boot/docs/3.0.0-M4/reference/htmlsingle/#io.caching.provider.redis)
- [spring-data-redis 官方文档](https://docs.spring.io/spring-data/redis/docs/3.0.0-M5/reference/html/)
- [Redis 官方文档](https://redis.io/docs/)