添加依赖
在 pom.xml 中添加 spring-boot-starter-data-redis的依赖
org.springframework.boot
spring-boot-starter-data-redis
org.apache.commons
commons-pool2
org.springframework.boot
spring-boot-starter-test
test
属性配置
在 application.properties 文件中配置如下内容,由于Spring Boot2.x 的改动,连接池相关配置需要通过spring.redis.lettuce.pool 或者 spring.redis.jedis.pool 进行配置了。使用了Spring Cache后,能指定spring.cache.type就手动指定一下,虽然它会自动去适配已有Cache的依赖,但先后顺序会对Redis使用有影响(JCache -> EhCache -> Redis -> Guava)
spring.redis.host=localhost
spring.redis.password=battcn
# 一般来说是不用配置的,Spring Cache 会根据依赖的包自行装配
spring.cache.type=redis
# 连接超时时间(毫秒)
spring.redis.timeout=10000
# Redis默认情况下有16个分片,这里配置具体使用的分片
spring.redis.database=0
# 连接池最大连接数(使用负值表示没有限制) 默认 8
spring.redis.lettuce.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制) 默认 -1
spring.redis.lettuce.pool.max-wait=-1
# 连接池中的最大空闲连接 默认 8
spring.redis.lettuce.pool.max-idle=8
# 连接池中的最小空闲连接 默认 0
spring.redis.lettuce.pool.min-idle=0
具体编码
实体类
创建一个User类,目的是为了模拟对象存储
package com.battcn.entity;
import java.io.Serializable;
public class User implements Serializable {
private static final long serialVersionUID = 8655851615465363473L;
private Long id;
private String username;
private String password;
// TODO 省略get set
}
定义接口package com.battcn.service;
import com.battcn.entity.User;
__
public interface UserService {
__
User saveOrUpdate(User user);
__
User get(Long id);
__
void delete(Long id);
}
###实现类
为了方便演示数据库操作,直接定义了一个Map
package com.battcn.service.impl;
import com.battcn.entity.User;
import com.battcn.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
__
@Service
public class UserServiceImpl implements
《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》
【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享
UserService {
private static final Map
static {
DATAbaseS.put(1L, new User(1L, “u1”, “p1”));
DATAbaseS.put(2L, new User(2L, “u2”, “p2”));
DATAbaseS.put(3L, new User(3L, “u3”, “p3”));
}
private static final Logger log = LoggerFactory.getLogger(UserServiceImpl.class);
@Cacheable(value = “user”, key = “#id”)// 根据方法的请求参数对其结果进行缓存
@Override
public User get(Long id) {
// TODO 我们就假设它是从数据库读取出来的
log.info(“进入 get 方法”);
return DATAbaseS.get(id);
}
@CachePut(value = “user”, key = “#user.id”)// 根据方法的请求参数对其结果进行缓存
@Override
public User saveOrUpdate(User user) {
DATAbaseS.put(user.getId(), user);
log.info(“进入 saveOrUpdate 方法”);
return user;
}
@CacheEvict(value = “user”, key = “#id”)// 根据条件对缓存进行清空
@Override
public void delete(Long id) {
DATAbaseS.remove(id);
log.info(“进入 delete 方法”);
}
}
主函数@EnableCaching 必须要加,否则spring-data-cache相关注解不会生效…
package com.battcn;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
__
@SpringBootApplication
@EnableCaching
public class Chapter9Application {
public static void main(String[] args) {
SpringApplication.run(Chapter9Application.class, args);
}
}
测试完成准备事项后,编写一个junit测试类来检验代码的正确性,有很多人质疑过Redis线程安全性,故下面也提供了响应的测试案例,如有疑问欢迎指正
package com.battcn;
import com.battcn.entity.User;
import com.battcn.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
__
@RunWith(SpringRunner.class)
@SpringBootTest
public class Chapter9ApplicationTest {
private static final Logger log = LoggerFactory.getLogger(Chapter9ApplicationTest.class);
@Autowired
private UserService userService;
@Test
public void get() {
final User user = userService.saveOrUpdate(new User(5L, “u5”, “p5”));
log.info("[saveOrUpdate] - [{}]", user);
final User user1 = userService.get(5L);
log.info("[get] - [{}]", user1);
userService.delete(5L);
}



