说明:本文主要说明@Cacheable缓存的使用
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <version>2.1.7.RELEASE</version> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> <version>2.4.0</version> </dependency>
spring.redis.host=127.0.0.1
spring.redis.port=6379
#指定缓存类型为redis缓存
spring.cache.type=redis
注解说明:
* @Cacheable("hehe") * 当前方法需要缓存,如果缓存中存在,方法不调用,如果缓存中没有,先调用业务方法,在放入缓存中。 * 缓存的数据名称(数据的key为 hehe::SimpleKey [],hehe为指定数据,SimpleKey自主生成) * 缓存的value值,默认使用jdk序列化机制,并将数据存储的redis中,缓存时间默认为-1 * @Cacheable({"hehe","haha"}) * 若是放数组,就在缓存中缓存key为hehe,haha两个数据 * * @Cacheable(value = {"hehe"},key = "'haha'") 放到缓存中的key为 hehe::haha,如若不是引用类型,key必须加单引号 * 缓存时间可以在配置文件中修改 * * @Cacheable(value = {"hehe"},key = "'cacheableDemo'")//如若不是引用类型,key必须加单引号 * * @Cacheable(value = {"hehe"},key = "#root.method.name")//key也可以通过表达式设置 * * @Caching(evict = { * @CacheEvict(value = {"haha"},key = "'cachingDemoOne'"), * @CacheEvict(value = {"haha"},key = "'cachingDemoTwo'") * })//用于组合,如数据更新后,需求想删除redis中的两个key * * @CacheEvict(value = {"haha"},allEntries = true)//删除haha文件夹下的全部数据
1,2,3中代码业务层引用集和代码
public static final List<String> list = new ArrayList<String>(); static { list.add("张龙"); list.add("赵虎"); list.add("王朝"); list.add("马汉"); }
1.@Cacheable使用
控制层:
@GetMapping("cacheableDemo") public List<String> cacheableDemo(){ return cacheableService.cacheableDemo(); }
接口层:
List<String> cacheableDemo();
业务层:
//@Cacheable(value = {"hehe"},key = "'cacheableDemo'")//如若不是引用类型,key必须加单引号 @Cacheable(value = {"hehe"},key = "#root.method.name")//key也可以通过表达式设置 public List<String> cacheableDemo(){ log.info("我进行了业务查询。。。。。"); return list; }
2.@CacheEvict使用
控制层
@GetMapping("cacheEvictDemo") public List<String> cacheEvictDemo(){ return cacheableService.cacheEvictDemo(); }
接口层
List<String> cacheEvictDemo();
业务层
@CacheEvict(value = {"hehe"},key = "'cacheableDemo'")//如果业务数据有变化,变化后的业务数据会在redis中删除,redis key为 hehe::cacheableDemo public List<String> cacheEvictDemo(){ list.add("包拯"); return list; }
3.@Caching使用
控制层
@GetMapping("cachingDemoOne") public List<String> cachingDemoOne(){ return cacheableService.cachingDemoOne(); } @GetMapping("cachingDemoTwo") public List<String> cachingDemoTwo(){ return cacheableService.cachingDemoTwo(); } @GetMapping("cachingDemo") public List<String> cachingDemo(){ return cacheableService.cachingDemo(); }
接口层
List<String> cachingDemoOne(); List<String> cachingDemoTwo(); List<String> cachingDemo();
业务层
@Cacheable(value = {"haha"},key = "'cachingDemoOne'")//key也可以通过表达式设置 public List<String> cachingDemoOne() { log.info("我进行了业务查询。。。。。"); return list; } @Cacheable(value = {"haha"},key = "'cachingDemoTwo'")//key也可以通过表达式设置 public List<String> cachingDemoTwo() { log.info("我进行了业务查询。。。。。"); return list; } /*@Caching(evict = {//用于组合 @CacheEvict(value = {"haha"},key = "'cachingDemoOne'"), @CacheEvict(value = {"haha"},key = "'cachingDemoTwo'") })*/ @CacheEvict(value = {"haha"},allEntries = true) public List<String> cachingDemo() { list.add("孙策"); return list; }