您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

关于spring redis注解实现缓存机制

2025/12/27 19:17:35发布14次查看
下面由redis教程栏目给大家介绍关于spring redis注解实现缓存机制,希望对需要的朋友有所帮助!
1、xml配置
<bean id="poolconfigtax" class="redis.clients.jedis.jedispoolconfig"> <property name="maxidle" value="${redis_tax.maxidle}" /> <property name="minidle" value="${redis_tax.minidle}" /> <property name="maxtotal" value="${redis_tax.maxtotal}" /> <property name="testonborrow" value="${redis_tax.testonborrow}" /> </bean> <!-- tax redis 数据库 --> <bean id="connectionfactorytax" class="org.springframework.data.redis.connection.jedis.jedisconnectionfactory" p:host-name="${redis_tax.host}" p:port="${redis_tax.port}" p:password="${redis_tax.pass}" p:pool-config-ref="poolconfigtax" p:database="0"/> <!--redis操作模版,使用该对象可以操作redis --> <bean id="redistemplatetax" class="org.springframework.data.redis.core.redistemplate" > <property name="connectionfactory" ref="connectionfactorytax" /> <!--如果不配置serializer,那么存储的时候缺省使用string,如果用user类型存储,那么会提示错误user can't cast to string!! --> <property name="keyserializer" > <bean class="org.springframework.data.redis.serializer.stringredisserializer" /> </property> <property name="valueserializer" > <bean class="org.springframework.data.redis.serializer.genericjackson2jsonredisserializer" /> </property> <property name="hashkeyserializer"> <bean class="org.springframework.data.redis.serializer.stringredisserializer"/> </property> <property name="hashvalueserializer"> <bean class="org.springframework.data.redis.serializer.genericjackson2jsonredisserializer"/> </property> <!--开启事务 --> <property name="enabletransactionsupport" value="false"></property> </bean> <!-- 配置rediscachemanager --> <bean id="rediscachemanager" class="org.springframework.data.redis.cache.rediscachemanager"> <constructor-arg name="redisoperations" ref="redistemplatetax" /> </bean> <cache:annotation-driven cache-manager="rediscachemanager"/>
2、缓存注解@cacheable、@cacheevict、@cacheput详解
一、@cacheable用法详解
1、用在哪里?用在方法或者类上。
2、这两种用法有什么区别?
 用在方法上表示:该方法的返回值将被缓存起来
 用在类上表示:表示该类的所有方法都支持该注解
3、使用后的结果是什么?下次使用相同方法和相同参数调用这个方法的时候将直接从缓存取值,而不需要再次执行该方法。
4、返回值在缓存中怎么存储的?以键值对的方式存储在缓存中,value就是返回值,key由两种策略生成:默认策略和自定义策略
5、默认策略和默认策略怎么用?
默认策略:在value值后双“::”拼接,形参列表,当形参是对象时,使用json格式:
@cacheconfig(cachenames="enterprise")//<!-- 声明缓存使用的缓存名称 -->public interface enterpriserepo extends jparepository<enterprise, integer>,jpaspecificationexecutor<enterprise>{ @cacheable(value="cash1") enterprise findbyid(integer id); @cacheput(value="cash1") enterprise save(enterprise enterprise);}
自定义策略:key属性是用来指定spring缓存方法的返回结果时对应的key的。该属性支持springel表达式。当我们没有指定该属性时,spring将使用默认策略生成key。
自定义策略是指我们可以通过spring的el表达式来指定我们的key。这里的el表达式可以使用方法参数及它们对应的属性。使用方法参数时我们可以直接使用“#参数名”或者“#p参数index”。下面是几个使用参数作为key的示例。
@cacheable(value="users", key="#id") public user find(integer id) { return null; } @cacheable(value="users", key="#p0") public user find(integer id) { return null; } @cacheable(value="users", key="#user.id") public user find(user user) { return null; } @cacheable(value="users", key="#p0.id") public user find(user user) { return null; }
除了上述使用方法参数作为key之外,spring还为我们提供了一个root对象可以用来生成key。通过该root对象我们可以获取到以下信息。
当我们要使用root对象的属性作为key时我们也可以将“#root”省略,因为spring默认使用的就是root对象的属性。如:
@cacheable(value={"users", "xxx"}, key="caches[1].name")public user find(user user) { return null;}
6、condition属性指定发生的条件
有的时候我们可能并不希望缓存一个方法所有的返回结果。通过condition属性可以实现这一功能。condition属性默认为空,表示将缓存所有的调用情形。其值是通过springel表达式来指定的,当为true时表示进行缓存处理;当为false时表示不进行缓存处理,即每次调用该方法时该方法都会执行一次。如下示例表示只有当user的id为偶数时才会进行缓存。
@cacheable(value={"users"}, key="#user.id", condition="#user.id%2==0")public user find(user user) { system.out.println("find user by user " + user); return user;}
二、@cacheput
在支持spring cache的环境下,对于使用@cacheable标注的方法,spring在每次执行前都会检查cache中是否存在相同key的缓存元素,如果存在就不再执行该方法,而是直接从缓存中获取结果进行返回,否则才会执行并将返回结果存入指定的缓存中。@cacheput也可以声明一个方法支持缓存功能。与@cacheable不同的是使用@cacheput标注的方法在执行前不会去检查缓存中是否存在之前执行过的结果,而是每次都会执行该方法,并将执行结果以键值对的形式存入指定的缓存中。
一般使用在保存,更新方法中。
@cacheput也可以标注在类上和方法上。使用@cacheput时我们可以指定的属性跟@cacheable是一样的。
@cacheput(“users”)//每次都会执行方法,并将结果存入指定的缓存中
public user find(integer id) { return null;}
三、@cacheevict
@cacheevict是用来标注在需要清除缓存元素的方法或类上的。当标记在一个类上时表示其中所有的方法的执行都会触发缓存的清除操作。@cacheevict可以指定的属性有value、key、condition、allentries和beforeinvocation。其中value、key和condition的语义与@cacheable对应的属性类似。即value表示清除操作是发生在哪些cache上的(对应cache的名称);key表示需要清除的是哪个key,如未指定则会使用默认策略生成的key;condition表示清除操作发生的条件。下面我们来介绍一下新出现的两个属性allentries和beforeinvocation。
1、 allentries属性
allentries是boolean类型,表示是否需要清除缓存中的所有元素。默认为false,表示不需要。当指定了allentries为true时,spring cache将忽略指定的key。有的时候我们需要cache一下清除所有的元素,这比一个一个清除元素更有效率。
@cacheevict(value="users", allentries=true)public void delete(integer id) { system.out.println("delete user by id: " + id);}
2、beforeinvocation属性
清除操作默认是在对应方法成功执行之后触发的,即方法如果因为抛出异常而未能成功返回时也不会触发清除操作。使用beforeinvocation可以改变触发清除操作的时间,当我们指定该属性值为true时,spring会在调用该方法之前清除缓存中的指定元素。
@cacheevict(value="users", beforeinvocation=true)public void delete(integer id) { system.out.println("delete user by id: " + id);}
以上就是关于spring redis注解实现缓存机制的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product