当前位置: 代码网 > it编程>编程语言>Java > Spring Cache注解@Cacheable的九个属性详解

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

2025年05月22日 Java 我要评论
1.value/cachenames 属性如下图所示,这两个属性代表的意义相同,根据@aliasfor注解就能看出来了。这两个属性都是用来指定缓存组件的名称,即将方法的返回结果放在哪个缓存中,属性定义

1.value/cachenames 属性

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

//这两种配置等价
@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
            public object generate(object target, method method, object... params) {
                return method.getname()+ arrays.aslist(params).tostring();
            }
        };
    }
    /**
     * 支持 lambda 表达式编写
     */
    /*@bean("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 时,方法的返回值才不会被缓存。可以在获取到结果后进行判断。

@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注解属性内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com