当前位置: 移动技术网 > IT编程>开发语言>Java > 基于spring 方法级缓存的多种实现

基于spring 方法级缓存的多种实现

2019年07月19日  | 移动技术网IT编程  | 我要评论

方案实施

1、 spring和ehcache集成

主要获取ehcache作为操作ehcache的对象。

spring.xml中注入ehcachemanager和ehcache对象,ehcachemanager是需要加载ehcache.xml配置信息,创建ehcache.xml中配置不同策略的cache。

<!-- ehcache 配置管理器 -->
<bean id="ehcachemanager" class="org.springframework.cache.ehcache.ehcachemanagerfactorybean">
<property name="configlocation" value="classpath:ehcache.xml" />
<!--true:单例,一个cachemanager对象共享;false:多个对象独立 -->
<property name="shared" value="true" />
<property name="cachemanagername" value="ehcachemanager" />
</bean> <!-- ehcache 操作对象 -->
<bean id="cachemanager" class="org.springframework.cache.ehcache.ehcachecachemanager">
<property name="cachemanager" ref="ehcachemanager"/>
</bean>
<!-- 启用缓存注解功能(请将其配置在spring主配置文件中) -->
<cache:annotation-driven cache-manager="cachemanager"/>

2、 spring和自带的缓存支持

<!-- spring自己的基于java.util.concurrent.concurrenthashmap实现的缓存管理器(该功能是从spring3.1开始提供的) -->
<bean id="cachemanager" class="org.springframework.cache.support.simplecachemanager">
<property name="caches">
<set>
<bean name="simplepagecachingfilter" class="org.springframework.cache.concurrent.concurrentmapcachefactorybean" />
</set>
</property>
</bean>

3.spring和redis集成

主要获取redistemplate作为操作redis的对象。

redis.properties配置信息

#host 写入redis服务器地址

redis.ip=127.0.0.1

#port

redis.port=6379

#passord

#redis.password=123456

#连接超时30000

redis.timeout=30

#最大分配的对象数

redis.pool.maxactive=100

#最大能够保持idel状态的对象数

redis.pool.maxidle=30

#当池内没有返回对象时,最大等待时间

redis.pool.maxwait=1000

#当调用borrow object方法时,是否进行有效性检查

redis.pool.testonborrow=true

#当调用return object方法时,是否进行有效性检查

redis.pool.testonreturn=true

spring注入jedispool、redisconnfactory、redistemplate对象

<!-- 加载redis.propertis -->

<bean class="org.springframework.beans.factory.config.propertyplaceholderconfigurer">

<property name="locations" value="classpath:redis.properties"/>

</bean>

<!-- redis 连接池 -->

<bean id="jedispool" class="redis.clients.jedis.jedispoolconfig">

<property name="maxtotal" value="${redis.pool.maxactive}" />

<property name="maxidle" value="${redis.pool.maxidle}" />

<property name="testonborrow" value="${redis.pool.testonborrow}" />

<property name="testonreturn" value="${redis.pool.testonreturn}" />

<property name="maxwaitmillis" value="${redis.pool.maxwait}" />

</bean>
<!-- redis 连接工厂 -->

<bean id="redisconnfactory" class="org.springframework.data.redis.connection.jedis.jedisconnectionfactory">

<property name="hostname" value="${redis.ip}" />

<property name="port" value="${redis.port}" />

<!-- property name="password" value="${redis.password}" -->

<property name="timeout" value="${redis.timeout}" />

<property name="poolconfig" ref="jedispool" />

</bean>
<!-- redis 操作对象 -->

<bean id="redistemplate" class="org.springframework.data.redis.core.redistemplate">

<property name="connectionfactory" ref="redisconnfactory" />

</bean>
<!-- 自定义缓存 -->

<bean id="cachemanager" class="org.springframework.cache.support.simplecachemanager">

<property name="caches">

<set>

<bean class="org.cpframework.cache.redis.rediscache">

<property name="redistemplate" ref="redistemplate" />

<property name="name" value="default"/>

</bean>

</set>

</property>

</bean>
<!-- 启用缓存注解功能(请将其配置在spring主配置文件中) -->

<cache:annotation-driven cache-manager="cachemanager"/>

4.spring 缓存注解解释

缓存注解有以下三个:

@cacheable @cacheevict @cacheput

1.

@cacheable(value=”accountcache”),这个注释的意思是,当调用这个方法的时候,会从一个名叫 accountcache 的缓存中查询,如果没有,则执行实际的方法,并将执行的结果存入缓存中,否则返回缓存中的对象。这里的缓存中的 key 就是参数 username,value 就是 account 对象。“accountcache”缓存是在 spring*.xml 中定义的名称。

例子:

@cacheable(value="accountcache")// 使用了一个缓存名叫 accountcache

public account getaccountbyname(string username) {

// 方法内部实现不考虑缓存逻辑,直接实现业务

system.out.println("real query account."+username);

return getfromdb(username);

}

condition:用来条件判断,满足条件的则进行缓存

例子2:

@cacheable(value="accountcache",condition="#username.length() <=4")// 缓存名叫 accountcache

public account getaccountbyname(string username) {

// 方法内部实现不考虑缓存逻辑,直接实现业务

return getfromdb(username);

}

2.

@cacheevict 注释来标记要清空缓存的方法,当这个方法被调用后,即会清空缓存。注意其中一个 @cacheevict(value=”accountcache”,key=”#account.getname()”),其中的 key 是用来指定缓存的 key 的,这里因为我们保存的时候用的是 account 对象的 name 字段,所以这里还需要从参数 account 对象中获取 name 的值来作为 key,前面的 # 号代表这是一个 spel 表达式,此表达式可以遍历方法的参数对象

例子3:

@cacheevict(value="accountcache",key="#account.getname()")// 清空accountcache 缓存

public void updateaccount(account account) {

updatedb(account);

}

@cacheevict(value="accountcache",allentries=true)// 清空accountcache 缓存

public void reload() {

reloadall()

}

@cacheable(value="accountcache",condition="#username.length() <=4")// 缓存名叫 accountcache

public account getaccountbyname(string username) {

// 方法内部实现不考虑缓存逻辑,直接实现业务

return getfromdb(username);

}

3.

@cacheput 注释,这个注释可以确保方法被执行,同时方法的返回值也被记录到缓存中,实现缓存与数据库的同步更新。

@cacheput(value="accountcache",key="#account.getname()")// 更新accountcache 缓存

public account updateaccount(account account) {

return updatedb(account);

}

附录:

@cacheable、@cacheput、@cacheevict 注释介绍

通过上面的例子,我们可以看到 spring cache 主要使用两个注释标签,即 @cacheable、@cacheput 和 @cacheevict,我们总结一下其作用和配置方法。

表 1. @cacheable 作用和配置方法

@cacheable 的作用 主要针对方法配置,能够根据方法的请求参数对其结果进行缓存

@cacheable 主要的参数

value

缓存的名称,在 spring 配置文件中定义,必须指定至少一个

例如: @cacheable(value=”mycache”) 或者 @cacheable(value={”cache1”,”cache2”}

key

缓存的 key,可以为空,如果指定要按照 spel 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合

例如: @cacheable(value=”testcache”,key=”#username”)

condition

缓存的条件,可以为空,使用 spel 编写,返回 true 或者 false,只有为 true 才进行缓存

例如: @cacheable(value=”testcache”,condition=”#username.length()>2”)

表 2. @cacheput 作用和配置方法

@cacheput 的作用 主要针对方法配置,能够根据方法的请求参数对其结果进行缓存,和 @cacheable 不同的是,它每次都会触发真实方法的调用

@cacheput 主要的参数

value

缓存的名称,在 spring 配置文件中定义,必须指定至少一个

例如: @cacheable(value=”mycache”) 或者 @cacheable(value={”cache1”,”cache2”}

key

缓存的 key,可以为空,如果指定要按照 spel 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合

例如: @cacheable(value=”testcache”,key=”#username”)

condition

缓存的条件,可以为空,使用 spel 编写,返回 true 或者 false,只有为 true 才进行缓存

例如: @cacheable(value=”testcache”,condition=”#username.length()>2”)

表 3. @cacheevict 作用和配置方法

@cachevict 的作用 主要针对方法配置,能够根据一定的条件对缓存进行清空

@cacheevict 主要的参数

value

缓存的名称,在 spring 配置文件中定义,必须指定至少一个

例如: @cachevict(value=”mycache”) 或者 @cachevict(value={”cache1”,”cache2”}

key

缓存的 key,可以为空,如果指定要按照 spel 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合

例如: @cachevict(value=”testcache”,key=”#username”)

condition

缓存的条件,可以为空,使用 spel 编写,返回 true 或者 false,只有为 true 才清空缓存

例如: @cachevict(value=”testcache”, condition=”#username.length()>2”)

allentries

是否清空所有缓存内容,缺省为 false,如果指定为 true,则方法调用后将立即清空所有缓存

例如: @cachevict(value=”testcache”,allentries=true)

beforeinvocation

是否在方法执行前就清空,缺省为 false,如果指定为 true,则在方法还没有执行的时候就清空缓存,缺省情况下,如果方法执行抛出异常,则不会清空缓存

例如: @cachevict(value=”testcache”,beforeinvocation=true)

以上这篇基于spring 方法级缓存的多种实现就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网