您的位置:首页 > 数据库 > Redis

Redis之声明式缓存

2020-04-01 18:49 791 查看

Redis之声明式缓存

导入依赖

<!--redis依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--对象转为json字符串保存在redis中,提高可读性-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.2</version>
</dependency>

在application.yml中配置redis的IP、端口、密码、连接池配置等信息

spring:
redis:
password: 123456
host: 192.168.31.14
port: 6666
jedis:
pool:
max-active: 100
max-wait: 1000ms
max-idle: 20
min-idle: 10

启动类使用注解开启缓存并配置缓存配置

@SpringBootApplication
@EnableCaching //开启缓存
public class SpringcachedemoApplication {

public static void main(String[] args) {
SpringApplication.run(SpringcachedemoApplication.class, args);
}

@Bean
public RedisCacheConfiguration redisCacheConfiguration(){
RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig();
//value采用json的方式进行序列化
configuration = configuration.serializeValuesWith(RedisSerializationContext
.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())).
serializeKeysWith(RedisSerializationContext.
SerializationPair.fromSerializer(new StringRedisSerializer()));
return configuration;
}
}

创建测试用的service类:
@Cacheable 缓存注解,执行查询的过程中使用
@CachePut 更新、添加数据的过程中使用
@CacheEvict 删除的过程中用来清楚缓存

@Service
public class EmpService {

//value是key的名称,key就是key的后缀, #empno是SPEL 获取的参数empno的值
//@Cacheable(value = "emp",condition = "#empno>2000")  //key --> 所有参数列表的组合
//@Cacheable(value = "emp",key="#empno",condition = "#empno>2000")  //key --> emp::1001
//@Cacheable(value = "emp",key = "#root.methodName")
@Cacheable(value = "emp",key = "#empno")
public Emp findEmpById(Integer empno,String ename){
//默认的情况采用的jdk的序列化方式进行序列化
Emp e = new Emp(empno,"hello",new Date());
System.out.println("查询数据库:"+e.getEname());
return e;
}

@Cacheable(value = "emps",key = "#currentPage+':'+#pageSize")
public List<Emp> queryEmps(Integer currentPage,Integer pageSize){
List list = new ArrayList();
for (int i = 0; i < 100; i++) {
list.add(new Emp(i,"hello"+i,new Date()));
}
List result = new ArrayList();
for (int i =(currentPage-1)*pageSize; i <currentPage*pageSize ; i++) {
result.add(list.get(i));//模拟分页查询
}
System.out.println("查询数据库");
return result;
}

//同步更新缓存 :  key --> emp::1001
@CachePut(value = "emp",key = "#emp.empno")
public Emp updateEmp(Emp emp){
emp.setEname("world");
System.out.println("更新员工");
return emp;
}

@CachePut(value = "emp",key = "#emp.empno")
public Emp saveEmp(Emp emp){
System.out.println("执行saveEmp:"+emp.getEmpno());
return emp;
}

//删除数据时,同步清除缓存中的数据
@CacheEvict(value = "emp",key="#empno")
public void deleteEmp(Integer empno){
System.out.println("删除数据库中的数据:"+empno);
}

//同步清空缓存中的所有数据
@CacheEvict(value = "emps",allEntries = true,beforeInvocation = true) //--> emps::1001  emps::1:10
public void removeEmp(Integer empno){
System.out.println("执行数据库的删除操作:"+empno);
System.out.println(2/0);
}
}

新建测试类

@SpringBootTest
class SpringcachedemoApplicationTests {

@Autowired
private EmpService empService;

@Test
void findEmp() {
Emp emp = empService.findEmpById(1001,"smith");
System.out.println(emp.getEname());
}

@Test
public void findEmps(){
List<Emp> list = empService.queryEmps(2,10);
System.out.println(list);
}

//更新数据库的同时,同步更新缓存
@Test
public void  updateEmp(){
Emp emp = new Emp();
emp.setEmpno(1001);
empService.updateEmp(emp);
}

//添加数据,同时同步缓存
@Test
public void testSaveEmp(){
Emp emp = new Emp(1008,"张三",new Date());
empService.saveEmp(emp);
}

@Test
public void deleteEmp(){
empService.deleteEmp(1001);
}

@Test
public void removeEmp(){
empService.removeEmp(10);
}
}

声明式缓存使用SpringAOP的方式对源代码进行增强,通过注解对不同的方法进行不同的处理,减少了对代码的入侵,而且SpringCache组件对主流缓存组件进行一致性集成,通过暴露统一接口,可以轻松进行组件之间的切换,提高了使用缓存的灵活性。

  • 点赞
  • 收藏
  • 分享
  • 文章举报
zifan_ 发布了11 篇原创文章 · 获赞 0 · 访问量 184 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: