@Cacheable整合及简单使用

4 篇文章 0 订阅

说明:本文主要说明@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;
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值