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);
    }
}


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

目录
相关文章
|
16天前
|
缓存 NoSQL Java
SpringBoot实现缓存预热的几种常用方案
SpringBoot实现缓存预热的几种常用方案
|
18天前
|
Java API 数据安全/隐私保护
掌握Spring Boot中的@Validated注解
【4月更文挑战第23天】在 Spring Boot 开发中,@Validated 注解是用于开启和利用 Spring 的验证框架的一种方式,特别是在处理控制层的输入验证时。本篇技术博客将详细介绍 @Validated 注解的概念和使用方法,并通过实际的应用示例来展示如何在项目中实现有效的数据验证
26 3
|
16天前
|
缓存 Java Sentinel
Springboot 中使用 Redisson+AOP+自定义注解 实现访问限流与黑名单拦截
Springboot 中使用 Redisson+AOP+自定义注解 实现访问限流与黑名单拦截
|
5天前
|
运维 Java 程序员
Spring5深入浅出篇:基于注解实现的AOP
# Spring5 AOP 深入理解:注解实现 本文介绍了基于注解的AOP编程步骤,包括原始对象、额外功能、切点和组装切面。步骤1-3旨在构建切面,与传统AOP相似。示例代码展示了如何使用`@Around`定义切面和执行逻辑。配置中,通过`@Aspect`和`@Around`注解定义切点,并在Spring配置中启用AOP自动代理。 进一步讨论了切点复用,避免重复代码以提高代码维护性。通过`@Pointcut`定义通用切点表达式,然后在多个通知中引用。此外,解释了AOP底层实现的两种动态代理方式:JDK动态代理和Cglib字节码增强,默认使用JDK,可通过配置切换到Cglib
|
1天前
|
JSON 前端开发 Java
【JAVA进阶篇教学】第七篇:Spring中常用注解
【JAVA进阶篇教学】第七篇:Spring中常用注解
|
4天前
|
JavaScript Java 开发者
Spring Boot中的@Lazy注解:概念及实战应用
【4月更文挑战第7天】在Spring Framework中,@Lazy注解是一个非常有用的特性,它允许开发者控制Spring容器的bean初始化时机。本文将详细介绍@Lazy注解的概念,并通过一个实际的例子展示如何在Spring Boot应用中使用它。
17 2
|
5天前
|
前端开发 Java
SpringBoot之自定义注解参数校验
SpringBoot之自定义注解参数校验
16 2
|
12天前
|
Java Spring
springboot自带的@Scheduled注解开启定时任务
springboot自带的@Scheduled注解开启定时任务
|
14天前
|
XML JSON Java
【SpringBoot】springboot常用注解
【SpringBoot】springboot常用注解
|
15天前
|
Java 数据库 开发者
了解Spring Boot:重要注解详解
了解Spring Boot:重要注解详解