1. 介绍

介绍如何使用cache提供缓存服务,插件包装了org.springframework.boot:spring-boot-starter-cache.

2. 版本历史

2.1. 1.4.0.0.M1

  • 升级到springboot 2.7.17

2.2. 1.3.1.1

  • 修复生成key的list未作排序的问题

3. 使用

3.1. 使用gradle 插件

3.1.1. 添加插件

gradle中

implementation('org.yunchen.gb:gb-plugin-cache:1.4.0.0.M1')

4. 配置

配置application.yml文件:

spring:
  cache:
    type: simple

5. 使用

cache使用简便,主要是@EnableCaching,@Cacheable,@CacheEvict,@CachePut四个注解

5.1. 配置Application

在项目的Application类上添加@EnableCaching注解,从而允许程序扫描所有的缓存注解

@ComponentScan(basePackages = ["org.yunchen.gb"])
@SpringBootApplication
@EnableCaching
class DemoApplication extends SpringBootServletInitializer implements WebApplicationInitializer {
    //.......
}

5.2. 使用@Cacheable注解

在方法上添加@Cacheable,将结果放入缓存

5.2.1. 标准用法

使用默认的SimpleKeyGenerator

    @Cacheable(value="baseUserJson")
    @ResponseBody
    public Map json(PageParams pageParams, String search, String role) {
        //......
    }

5.2.2. 配合内置keyGenerator的用法

插件提供了内置的keyGenerator:GbKeyGenerator用法如下:

    @Cacheable(value="baseUserJson",keyGenerator = "GbKeyGenerator")
    @ResponseBody
    public Map json(PageParams pageParams, String search, String role) {
        //......
    }
在生成key时,GbKeyGenerator会比SimpleKeyGenerator多增加一个class名称. 但为了应付更复杂的缓存要求, 建议使用配合插件内置的@TokenGeneratorHelper进行高级设置
高级设置
    @TokenGeneratorHelper(withHeaderkeys = "token,Content-Type",withDateFormat = "yyyy-MM-dd hh",withRemoteUser = true)
    @Cacheable(value="baseUserJson",keyGenerator = "GbKeyGenerator")
    @ResponseBody
    public Map json(PageParams pageParams, String search, String role) {
        //........
    }

TokenGeneratorHelper注解的参数:

参数名 默认值 描述

withClassName

false

key中是否携带类名

withMethodName

false

key中是否携带方法名

withParams

true

key中是否携带全部调用参数

withSessionId

false

key中是否携带sessionId

withRemoteUser

false

key中是否携带reqeust.remoteUser的值

withHeaderKeys

key中是否携带header中的值(使用逗号隔开的header关键词的字符串)

withAttributesKeys

key中是否携带request attributes中的值(使用逗号隔开的request attributes关键词的字符串)

withAllRequestParameter

true

key中是否携带request的parameter的各个参数值

withRequestParameterKeys

key中是否携带request的parameter的指定参数值(使用逗号隔开的parameter关键词的字符串)

withPrincipal

false

key中是否携带principal(需要确保项目已引用gb-springsecurity插件)

withDateFormat

false

key中是否携带当前日期的固定格式(如"yyyy-MM-dd hh"表示一小时内的值相同)

5.3. 使用@CacheEvict注解

在方法上添加@CacheEvict,清除特定名称的缓存.添加allEntries=true,会清除掉value名称下的全部缓存.

    @CacheEvict(value = 'userJson', allEntries=true)
    public void update(long id) {
         ..........
     }

5.4. 使用@CachePut注解

在方法上添加@CachePut,将结果放入到缓存中.也可以配合内置keyGenerator使用

    @CachePut(value="somevalue",keyGenerator = "GbKeyGenerator")
    public Map someMethod(String arg0, String arg1) {
        ..........
    }
不建议使用@CachePut来更新缓存,因为具体使用@Cacheable的方法逻辑与当前的方法逻辑不一定相同, 未来可能独立演变,甚至可能由不同团队人员开发,依靠开发约定保证两者唯一太危险. 建议采用@CacheEvict注解触发缓存清除,而由@Cacheable的方法逻辑来更新缓存.

6. 配合redis进行缓存处理