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组件对主流缓存组件进行一致性集成,通过暴露统一接口,可以轻松进行组件之间的切换,提高了使用缓存的灵活性。
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- 分布式缓存技术redis学习系列(五)——redis实战(redis与spring整合,分布式锁实现)
- Redis 缓存 + Spring 的集成示例
- 初学Redis(二)用Redis作为MySQL数据库的缓存
- redis代替kafka做缓存队列
- 网站缓存技术总结( ehcache、memcache、redis对比)
- 网站缓存技术总结( ehcache、memcache、redis对比)
- c#对第三方缓存redis的操作(-)
- hibernate redis 二级缓存
- redis做带分页的列表缓存
- T5清理redis或file缓中带tag的指定缓存
- redis与ehcache结合实现高性能缓存方案
- 缓存子系统如何设计(Cachable tag, Memcache/redis support, xml config support, LRU/LFU/本地缓存命中率)
- SpringBoot集成Redis来实现缓存技术方案
- Redis在SSM框架中最为缓存
- redis数据库缓存部署
- 利用Redis进行全页面缓存的简单Demo
- Shiro使用redis作为缓存(解决shiro频繁访问Redis)思路总结
- JavaWeb项目缓存从 ehcache 切换到 redis
- simple-cache说明文档-注解化自动缓存,redis、memcached
- 分布式缓存技术redis学习系列(五)——spring-data-redis与JedisPool的区别、使用ShardedJedisPool与spring集成的实现及一致性哈希分析