14-spring cache 学习

Spring Cache 介绍

Spring Cache 是一个框架,实现了基于注解的缓存功能,只需要简单地加一个注解,就能实现缓存功能。
Spring Cahce 提供了一层抽象,底层可以切换不同的 cache 实现,具体就是通过 CacheManager 接口来统一不同的缓存技术。
CacheManager 是 Spring 提供的各种缓存技术抽象接口。

 

Spring Cache 注解

在 spring boot 项目中,使用缓存技术只需要在项目中导入相关缓存技术的依赖包,并在启动类上使用 @EnableCaching 开启缓存支持即可

@EnableCaching 开启缓存注解功能
@Cacheable 触发缓存,在方法执行前 spring 先查看缓存中是否有数据,如果有数据,则直接返回缓存数据;若没有数据,调用方法并将方法返回值放到缓存中
@CacheEvict 缓存清除,将一条或多条数据从缓存中删除(常用于增、删、改接口)
@CachePut 在不影响方法执行的情况下更新缓存,将方法的返回值放到缓存中

 

 

 

 

注意:使用SpringCache的缓存功能,尤其是在数据库查询时,可以将第一次查询的结果存储到Redis缓存中,避免每次调用重复数据都要查一次数据库,有效减少资源的浪费,但也要注意一些修改数据的增删改查操作,要及时清除缓存。

 

1. 启动类上使用 @EnableCaching 

使用@BladeCloudApplication注解时,可以不再添加@EnalbleCaching注解,其中包含缓存的注解

复制代码
package org.springblade.demo;

import org.springblade.common.constant.CommonConstant;
import org.springblade.core.cloud.client.BladeCloudApplication;
import org.springblade.core.launch.BladeApplication;
import org.springframework.cache.annotation.EnableCaching;

@BladeCloudApplication
@EnableCaching
public class DemoApplication {
    public static void main(String[] args) {
        BladeApplication.run(CommonConstant.BLADE_DEMO_NAME, DemoApplication.class, args);
    }
}
复制代码

 

2. @Cacheable注解

@Cacheable 注解在方法上,表示该方法的返回结果是可以缓存的。也就是说,该方法的返回结果会放在缓存中,以便于以后使用相同的参数调用该方法时,会返回缓存中的值,而不会实际执行该方法

 提供两个参数来指定缓存名:value、cacheNames,二者选其一即可 

复制代码
@GetMapping("list")
public R list(){
return R.data(this.blogService.list());
}

/**
 @Cacheable: 在方法执行前 spring 先查看缓存中是否有数据,如果有数据,则直接返回缓存数据;若没有数据,调用方法并将方法返回值放到缓存中
 cacheNames: 缓存的名称
*/
@GetMapping("listCache")
@Cacheable(cacheNames="blog")
public R listCache(){
log.info("使用缓存");
return R.data(this.blogService.list());
}

/**
 @Cacheable: 在方法执行前 spring 先查看缓存中是否有数据,如果有数据,则直接返回缓存数据;若没有数据,调用方法并将方法返回值放到缓存中
 cacheNames: 缓存的名称
 key: 缓存的 key
*/
@GetMapping("listCache")
@Cacheable(cacheNames="blog",key="'list'")
public R listCache(){
log.info("使用缓存");
return R.data(this.blogService.list());
}

/**
 @Cacheable: 在方法执行前 spring 先查看缓存中是否有数据,如果有数据,则直接返回缓存数据;若没有数据,调用方法并将方法返回值放到缓存中
 value: 缓存的名称,每个缓存名称下面可以有多个 key
 key: 缓存的 key
 condition: 条件,满足条件才缓存数据
*/
@Cacheable(value = "userCache", key = "#user.id", condition = "#user != null")
@GetMapping("/{id}")
public User getById(@PathVariable Long id){
    User user = userService.getByUd(id);
    return user;
}

/**
 @Cacheable: 在方法执行前 spring 先查看缓存中是否有数据,如果有数据,则直接返回缓存数据;若没有数据,调用方法并将方法返回值放到缓存中
 value: 缓存的名称,每个缓存名称下面可以有多个 key
 key: 缓存的 key
 condition: 条件,满足条件才缓存数据
 unless: 满足条件则不缓存
*/
@Cacheable(value = "userCache", key = "#user.id", unless= "#user == null")
@GetMapping("/{id}")
public User getById(@PathVariable Long id){
    User user = userService.getByUd(id);
    return user;
}
复制代码

 

3. @CacheEvict注解

使用了 @CacheEvict 注解的方法,会清空指定缓存。一般用在更新或者删除的方法上

allEntries 是 @CacheEvict 特有的一个属性,意为是否删除整个缓存(value 或 cacheNames 指定的),默认为 false。

复制代码
@PostMapping("update")
public R update(@RequestBody BlogEntity blog) {
return R.status(this.blogService.updateById(blog));
}


/**
 CacheEvict: 清理指定缓存
 value: 缓存的名称,每个缓存名称下面可以有多个 key
 key: 缓存的 key
*/
@PostMapping("updateCache")
@CacheEvict(cacheNames="blog",key = "#blog.isDeleted")
public R updateCache(@RequestBody BlogEntity blog) {
log.info("删除缓存");
return R.status(this.blogService.updateById(blog));
}


/**
 CacheEvict: 清理指定缓存
 value: 缓存的名称,每个缓存名称下面可以有多个 key
 key: 缓存的 key
 allEntries : 清理所有缓存属性,默认为 false
*/
@PostMapping("updateCache")
@CacheEvict(cacheNames="blog",allEntries=true)
public R updateCache(@RequestBody BlogEntity blog) {
log.info("删除缓存");
return R.status(this.blogService.updateById(blog));
}
复制代码

 

4. @CachePut注解

加了 @CachePut 注解的方法,会把方法的返回值 put 到缓存里面缓存起来,供其它地方使用。它通常用在新增方法上。

复制代码
/**
 CachePut: 将方法返回值放入缓存
 value: 缓存的名称,每个缓存名称下面可以有多个 key
 key: 缓存的 key
*/
@CachePut(value = "userCache", key = "#user.id")
@PostMapping
public User save(User user){
    userService.save(user);
    return user;
}
复制代码

 

posted @   马铃薯1  阅读(139)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2023-09-12 10-Vue核心-监视属性
点击右上角即可分享
微信分享提示