开发者

Spring Cache注解@Cacheable的九个属性详解

开发者 https://www.devze.com 2025-05-23 10:35 出处:网络 作者: 扛麻袋的少年
目录1.value/cacheNames 属性2.key属性3.keyGeneratjavascriptor 属性4.cacheManager 属性5.cacheResolver 属性6.condition 属性7.unless 属性8.sync 属性1.value/cacheNames 属性
目录
  • 1.value/cacheNames 属性
  • 2.key属性
  • 3.keyGeneratjavascriptor 属性
  • 4.cacheManager 属性
  • 5.cacheResolver 属性
  • 6.condition 属性
  • 7.unless 属性
  • 8.sync 属性

1.value/cacheNames 属性

如下图所示,这两个属性代表的意义相同,根据@AliasFor注解就能看出来了。这两个属性都是用来指定缓存组件的名称,即将方法的返回结果放在哪个缓存中,属性定义为数组,可以指定多个缓存;

Spring Cache注解@Cacheable的九个属性详解

//这两种配置等价
@Cacheable(value = "user") //@Cacheable(cacheNames = "user")
User getUser(Integer id);

2.key属性

可以通过 key 属性来指定缓存数据所使用的的 key,默认使用的是方法调用传过来的参数作为 key。最终缓存中存储的内容格式为:Entry<key,value> 形式。

  • 如果请求没有参数:key=new SimpleKey();
  • 如果请求有一个参数:key=参数的值
  • 如果请求有多个参数:key=newSimpleKey(params);(你只要知道 key不会为空就行了)

key值的编写,可以使用 SpEL 表达式的方式来编写;除此之外,我们同样可以使用 keyGenerator 生成器的方式来指定 key,我们只需要编写一个 keyGenerator ,将该生成器注册到 IOC 容器即可。(keyGenerator的使用,继续往下看)

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

使用示例如下:

@Cacheable(value = "user",key = "#root.method.name")
User getUser(Integer id);

3.keyGenerator 属性

key 的生成器。如果觉得通过参数的方式来指定比较麻烦,我们可以自己指定 key 的生成器的组件 id。key/keyGenerator属性:二选一使用。我们可以通过自定义配置类方式,将 keyGenerator 注册到 IOC 容器来使用。

import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import Java.lang.reflect.Method;
import java.util.Arrays;
@Configuration
public class MyCacheConfig {
    @Bean("myKeyGenerator")
    public KeyGenerator keyGenerator(){
        return new KeyGenerator(){
            @Override
           python public Object generate(Object target, Method method, Object... params) {
                return method.getName()+ Arrays.asList(params).toString();
            }
        };
    }
    /**
     * 支持 lambda 表达式编写
     */
    /*@jsBean("myKeyGenerator")
    public KeyGenerator keyGenerator(){
        return ( target,  method, params)-> method.getName()+ Arrays.asList(params).toString();
    }*/
}

4.cacheManager 属性

该属性,用来指定缓存管理器。针对不同的缓存技术,需要实现不同的 cacheManager,Spring 也为我们定义了如下的一些 cacheManger 实现()

CacheManger描述
SimpleCacheManager使用简单的Collection来存储缓存,主要用于测试
ConcurrentMapCacheManager使用ConcurrentMap作为缓存技术(默认)
NoOpCacheManager测试用
EhCacheCacheManager使用EhCache作为缓存技术,以前在hibernate的时候经常用
GuavaCacheManager使用google guava的GuavaCache作为缓存技术
HazelcastCacheManager使用Hazelcast作为缓存技术
JCacheCacheManager使用JCache标准的实现作为缓存技术,如Apache Commons JCS
RedisCacheManager使用Redis作为缓存技术

具体使用介绍,可参考:SpringBoot整合Redis实现数据缓存

5.cacheResolver 属性

该属性,用来指定缓存管理器。使用配置同 cacheManager 类似,可自行百度。(cacheManager指定管理器/cacheResolver指定解析器 它俩也是二选一使用)

6.condition 属性

条件判断属性,用来指定符合指定的条件下才可以缓存。也可以通过 SpEL 表达式进行设置。这个配置规则和上面表格中的配置规则是相同的。

@Cacheable(value = "user",condition = "#id>0")//传入的 id 参数值>0才进行缓存
User getUser(Integer id);
@Cacheable(value = "user",condition = "#a0>1")//传入的第一个参数的值>1的时候才进行缓存
User getUser(Integer id);
@Cacheable(value = "user",condition = "#a0>1 and #root.methodName编程 eq 'getUser'")//传入的第一个参数的值>1 且 方法名为 getUser 的时候才进行缓存
User getUser(Integer id);

7.unless 属性

unless属性,意为"除非"的意思。即只有 unless 指定的条件为 true 时,方法的返回值才不会被缓存。php可以在获取到结果后进行判断。

@Cacheable(value = "user",unless = "#result == null")//当方法返回值为 null 时,就不缓存
User getUser(Integer id);
@Cacheable(value = "user",unless = "#a0 == 1")//如果第一个参数的值是1,结果不缓存
User getUser(Integer id);

8.sync 属性

该属性用来指定是否使用异步模式,该属性默认值为 false,默认为同步模式。异步模式指定 sync = true 即可,异步模式下 unless 属性不可用。

到此这篇关于Spring Cache注解@Cacheable的九个属性详解的文章就介绍到这了,更多相关@Cacheable注解属性内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号