Springboot中基于注解使用缓存

1、概述

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

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

2、经常使用注解及其参数

  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

3、简单的使用

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


}

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


本文分享自微信公众号 - 全民java空间(ljl236915692)。
若有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一块儿分享。spring

相关文章
相关标签/搜索