Springboot中基于注解使用缓存

简介: Springboot中基于注解使用缓存

一、概述



Spring定义了org.springframework.cache.Cache 和org.springframework.cache.CacheManager接口来统一不同的缓存技术,并支持使用JCache(JSR-107)注解简化我们开发;


  1. Cache接口为缓存的组件规范了定义,包含缓存的各种操作集合;
  2. Cache接口下Spring提供了各种xxxCache的实现;如RedisCache,EhCacheCache , ConcurrentMapCache等;
  3. 每次调用需要缓存功能的方法时,Spring会检查指定参数所指定的目标方法是否 已经被调用过;如果有就直接从缓存中获取方法调用后的结果,如果没有就调用方法;并缓存结果后返回给用户。下次调用直接从缓存中获取。
  4. 使用Spring缓存抽象时我们需要关注以下两点;
  • [x] 确定方法需要被缓存以及他们的缓存策略
  • [x] 从缓存中读取之前缓存存储的数据


二、常用注解及其参数



  1. 注解简介


注解 功能
@Cacheable 主要针对方法配置,能够根据方法的请求参数对其结果进行缓存
@CacheEvict 清空缓存
@CachePut 对存入缓存的数据进行更新
@EnableCaching 开启基于注解的缓存
@Caching 定义复杂的缓存规则


  1. @Cacheable/@CachePut/@CacheEvict主要的参数


常用参数 功能介绍 使用
value/cacheNames 缓存的名称,在spring配置文件中定义,必须指定至少一个 @Cacheable(value ="user")或者 @Cacheable(value={"user","mycache"};
key 缓存的key,可以为空,如果指定要按照SpEL表达式编写,如果不指定,则缺省按照方法的所有参数进行组合 @Cacheable(value = {"user"},key = "#id")
condition 缓存的条件,可以为空,使用SpEL编写,返回true或者false,只有为true才进行缓存/清除缓存,在调用方法之前之后都能判断 @Cacheable(value="user",condition="#id>2")
allEntries(@CacheEvict) 是否清空所有缓存内容,缺省为false,如果指定为true,则方法调用后将立即清空所有缓存 @CachEvict(value="user",allEntries=true)
beforeInvocation(@CacheEvict) 是否在方法执行前就清空,缺省为false,如果指定为true,则在方法还没有执行的时候就清空缓存,缺省情况下,如果方法执行抛出异常,则不会清空缓存 @CachEvict(value="user",beforeInvocation=true)
unless(@CachePut)(@Cacheable) 用于否决缓存的,不像condition,该表达式只方法执行之后判断,此时可以拿到返回值result进行判断。条件为true不会缓存,fasle才缓存 @Cacheable(value="user",unless="#result == null")
sync 是否支持异步 @Cacheable(value = {"user"},key = "#id",sync = true)
keyGenerator 指定key的生成策略,不能和key同时使用 @Cacheable(value ={"user"},keyGenerator = "myKeyGenerator")
cacheManager 用来指定缓存管理器


  1. Cache SpEL available metadata(可用的缓存元数据)


名字 位置 描述 示例
methodName root object 当前被调用的方法名 #root.methodName
method root object 当前被调用的方法 #root.method.name
target root object 当前被调用的目标对象 #root.target
targetClass root object 当前被调用的目标对象类 #root.targetClass
args root object 当前被调用的方法的参数列表 #root.args[0]
caches root object 当前方法调用使用的缓存列表(如@Cacheable(value={"cache1","cache2"})),则有两个cache #root.caches[0]
argumentname evaluation context 方法参数的名字.可以直接#参数名,也可以使用#p0或#a0的形式,0代表参数的索引; #iban、#a0、#p0
result evaluation context 方法执行后的返回值(仅当方法执行之后的判断有效,如"unless" cache put’的表达式’cache evict’的表达式,beforeInvocation=false) #result


三、简单的使用



<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>
package com.example.jpa.controller;
import com.example.jpa.entity.User;
import com.example.jpa.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.*;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @Cacheable(value = {"user"},key = "#id") //调用方法之前调用
    @GetMapping("/user")
    public User getUserById(@RequestParam Integer id){
        return userService.getUserById(id);
    }
    @GetMapping("/users")
    public List<User> getAll(){
        return userService.getAll();
    }
    @Cacheable(value = "user",key = "#pageOffset")
    @GetMapping("/usersByPage")
    public Page<User> getAllByPage(@RequestParam Integer pageOffset,@RequestParam Integer pageSize){
        return userService.getAll(PageRequest.of(pageOffset,pageSize));
    }
    @CachePut(value = "user",key = "#result.id") //即调用方法又更新缓存数据
    @PostMapping("/user")
    public User saveUser(@RequestBody User user){
        return userService.saveUser(user);
    }
    //@CachePut(value = "user",key = "#result.id") //即调用方法又更新缓存数据
    @Caching
    @PutMapping("/user")
    public User updateUser(@RequestBody User user){
        return userService.updateUser(user);
    }
    @CacheEvict(value = "user",key = "#id")
    @DeleteMapping("/user")
    public void deleteUser(Integer id){
        userService.deleteUserById(id);
    }
}


在这里碰到了一个问题,在更新数据的时候怎么确保分页里面的缓存数据也随之更新而不是返回更新前的数据?

目录
相关文章
|
关系型数据库 MySQL
mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file
mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file
1631 0
|
Linux C语言
成功解决 在Linux CentOS 7 中安装gcc
这篇文章介绍了如何在Linux CentOS 7系统中安装gcc (g++) 8工具集。由于CentOS 7默认的gcc版本是4.8,而这个版本与Qt 5.14、Qt 5.15或更高版本不兼容,可能会导致编译时出现系统头文件错误。文章中提到,即使在项目配置中添加了`CONFIG+=c++11`,如果仍然报错,那么很可能是gcc版本的问题。为了解决这个问题,文章提供了使用CentOS的Software Collections (scl)来安装更新版本的gcc的步骤。
成功解决 在Linux CentOS 7 中安装gcc
|
前端开发 JavaScript 应用服务中间件
linux安装nginx和前端部署vue项目(实际测试react项目也可以)
本文是一篇详细的教程,介绍了如何在Linux系统上安装和配置nginx,以及如何将打包好的前端项目(如Vue或React)上传和部署到服务器上,包括了常见的错误处理方法。
4971 0
linux安装nginx和前端部署vue项目(实际测试react项目也可以)
|
监控 Java API
如何从 Java 程序中查找内存使用情况
【8月更文挑战第22天】
1607 0
|
缓存 Java 关系型数据库
Spring Boot与Spring中的数据缓存Cache支持与实战(附源码)
Spring Boot与Spring中的数据缓存Cache支持与实战(附源码)
894 0
Spring Boot与Spring中的数据缓存Cache支持与实战(附源码)
|
监控 关系型数据库 MySQL
mysql启动时报错:Starting MySQL... ERROR! The server quit without updating PID file (/opt/mysql/data/mysql.pid) 的解决方法
出现问题的可能性 1、可能是/opt/mysql/data/数据目录mysql用户没有权限(修改数据目录的权限)    解决方法 :给予权限,执行  "chown -R mysql.mysql /opt/mysql/data"  然后重新启动mysqld   2、可能进程里已经存在mysql进程    解决方法:用命令“ps -ef|grep mysqld”查看是否有mysqld进程,如果有使用“kill -9  进程号”杀死,然后重新启动mysqld!   3、可能是第二次在机器上安装mysql,有残余数据影响了服务的启动。
4531 0
|
1天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8050 2
|
10天前
|
域名解析 人工智能 Linux
|
20天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
18146 63
|
18天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置