设为首页 加入收藏

TOP

SpringBoot的EnableCaching简述(一)
2023-07-25 21:35:08 】 浏览:44
Tags:SpringBoot EnableCaching 简述

Spring Boot中的EnableCaching简述

spring boot中自带有数据缓存机制,主要通过其org.springframework.cache包下的各种类来实现。

EnableCaching

@EnableCaching是启用缓存的注解,标注在任何一个可自动注入的类上即可开启。

Cacheable

@Cacheable是一个标注与类与方法上的注解,用于表示此类或此方法需要使用缓存机制。当类与方法上都有时,采用 就近原则

@Cacheable注解中,有一些常用参数可以进行配置:

  • valuecacheNames - 表示绑定的缓存名称。这里的缓存指的是单个的缓存存储器,并不是最终的键值对缓存对象。
  • key - 表示缓存对象的 key,这个才是最终的缓存键值对的 key。这里的参数需要使用 SpEL表达式。
  • keyGenerator - 表示用于生成此方法 缓存key的类。与key参数只能选择一个添加,否则会抛出IllegalStateException异常。
  • cacheManager - 指定缓存管理器。这个后面再细说。
  • condition - 缓存的条件。支持SpEL,当缓存条件满足时,才会进入缓存取值模式。
  • unless - 排除的条件。支持SpEL,当排除的条件满足时,会直接调用方法取值。
  • sync - 异步缓存模式。是否采用异步的方式,在方法取值时异步缓存。默认false,在缓存完成后才返回值。

一般情况下,可以这样使用:

@RestController
@RequestMapping("cache")
@Cacheable(value = "cache", sync = true)
public class CacheController {
    @Cacheable(value = "hello", sync = true, keyGenerator = "myKeyGenerator")
    @GetMapping("hello")
    public String hello(String name) {
        System.out.println("name - " + name);
        return "hello " + name;
    }
    @GetMapping("hello2")
    public String hello2(@RequestParam(defaultValue = "1") Integer size, @RequestParam(defaultValue = "world") String name) {
        System.out.println("name - " + name);
        return "hello " + name;
    }
}
  • 这里的CacheController被标记上了@Cacheable(value = "cache", sync = true),表示其下的方法默认使用名为cache的缓存存取器,并采用异步的方式进行缓存处理。
  • hello方法上同样添加了@Cacheable(value = "hello", sync = true, keyGenerator = "myKeyGenerator"),使得hello方法使用了独立的缓存设置,并通过myKeyGenerator的策略来生成 缓存key

CachePut

将方法返回值存入到缓存中,一般情况下是用在更新操作中,并于CacheableCacheEvict配合使用。

CacheEvict

清除缓存值,一般用在删除或更新操作中,并于CacheableCachePut配合使用。

并且在CacheEvict注解中,多了两个参数:

  • allEntries - 清除当前value下的所有缓存。
  • beforeInvocation - 在方法执行前清除缓存。

示例代码示例如下:

    @Cacheable(value = "c", key = "123")
    @GetMapping("hello")
    public String hello(String name) {
        System.out.println("name - " + name);
        return "hello " + name;
    }
    @GetMapping("/put")
    @CachePut(value = "c", key = "123")
    public String put() {
        return "hello put";
    }
    @GetMapping("/evict")
    @CacheEvict(value = "c", key = "123")
    public String evict() {
        return "hello put";
    }

上述代码中,访问hello接口时,会从c缓存存取器中取出key123的缓存数值,没有则会调用方法并进行缓存。

访问put接口时,会将c缓存存取器key123的缓存值改为hello put,没有则进行缓存。

访问evict接口时,会将c缓存存取器key123的缓存值删除,此时访问hello接口会重新调用方法并进行缓存。

CacheConfig

@CacheConfig作为类上的注解,目的是为了统一配置其下的方法缓存参数,并设定共享缓存名。

  • cacheNames - 共享缓存名数组。设定后表示此类下的方法缓存会依次从这些缓存存取器中取值,如果有,则取用缓存值;若没有则调用方法取值,并缓存值到设定的所有缓存存取器中。

CacheManager

缓存管理器接口,用来做缓存管理的类。一般我们需要自定义缓存策略时,就是从CacheManager来入手的。

直接上实例:

@Component
public class MyCacheManager implements CacheManager, InitializingBean {
    private final Map<String, Cache> cacheMap;
    public MyCacheManager() {
        cacheMap = new HashMap<>();
    }
    @Override
    public Cache getCache(String name) {
        System.out.println("正在获取缓存 - " + name);
        return cacheMap.computeIfAbsent(name, MyCache::new);
    }
    @Override
    public Collection<String> getCacheNames() {
        return cacheMap.keySet();
    }
    @Override
    public void afterPropertiesSet() throws Exception {
        System.ou
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇【电脑操作技巧】重装系统之后的.. 下一篇Seata 全局锁等待超时 问题排查

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目