实现Spring配置流程
I. 编写服务类,在方法上使用缓存注解
II. 将Service类纳入Spring容器进行管理
A. 通过类配置
B. 通过XML文件配置
1. 包扫描标签+注解
2. <bean>标签
III. 开启缓存注解代理,并指定缓存器
IV. 配置缓存管理器和实际缓存技术
附件:
缓存注解:
@Cacheable常用属性
如果有缓存值,@Cacheable标记的方法不在执行,主要用于get方法
I. value属性:指定缓存器
A. @Cacheable(value=”mycache”) 或者@Cacheable(value={”cache1”,”cache2”}
II. key属性:指定缓存的key
A. @Cacheable(value=”testcache”,key=”#userName”)
III. condition属性:根据条件决定是否缓存返回值
A. @Cacheable(value=”testcache”,condition=”#userName.length()>2”)
@CachePut
@CachePut标记得方法直接执行,并将返回值更新到缓存中,主要用于update方法
I. value属性:指定缓存器
A. @Cacheable(value=”mycache”) 或者@Cacheable(value={”cache1”,”cache2”}
II. key属性:指定缓存的key
A. @Cacheable(value=”testcache”,key=”#userName”)
III. condition属性:根据条件决定是否缓存返回值
A. @Cacheable(value=”testcache”,condition=”#userName.length()>2”)
@CacheEvict
@CacheEvict标记的方法执行前后会清除指定缓存
I. value属性:指定缓存器
A. @Cacheable(value=”mycache”) 或者@Cacheable(value={”cache1”,”cache2”}
II. key属性:指定需要清除缓存的key
A. @Cacheable(value=”testcache”,key=”#userName”)
III. condition属性:根据条件决定是否清除缓存
A. @Cacheable(value=”testcache”,condition=”#userName.length()>2”)
IV. allEntries属性:确定是否清除所有缓存
A. @CachEvict(value=”testcache”,allEntries=true)
V. beforeInvocation属性:指定执行位置
A. @CachEvict(value=”testcache”,beforeInvocation=true)
配置缓存管理器和实际缓存技术
使用Spring提供的缓存技术
<bean id="cacheManager"
class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean
class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"
p:name="default" />
<bean
class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"
p:name="accountCache" />
</set>
</property>
</bean>
整合Ehcache提供的缓存技术
I. 提供Ehcache缓存技术的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<ehcachexmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
<!-- 磁盘缓存位置 -->
<diskStorepath="java.io.tmpdir/ehcache"/>
<!-- 默认缓存 -->
<defaultCache
maxEntriesLocalHeap="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxEntriesLocalDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistencestrategy="localTempSwap"/>
</defaultCache>
<cachename="userCache"
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="3"
timeToLiveSeconds="3"
maxEntriesLocalDisk="10000000"
overflowToDisk="false"
memoryStoreEvictionPolicy="LRU"/>
</ehcache>
II. 将Ehcache缓存技术配置到Spring中
<cache:annotation-driven cache-manager="cacheManager"/>
<bean id="cacheManager"class="org.springframework.cache.ehcache.EhCacheCacheManager">
<propertyname="cacheManager" ref="ehcache"></property>
</bean>
<bean id="ehcache"class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<propertyname="configLocation"value="classpath:ehcache-setting.xml"></property>
</bean>
整合Redis提供的缓存技术
<bean id="poolConfig"class="redis.clients.jedis.JedisPoolConfig">
<propertyname="maxIdle" value="${redis.maxIdle}" />
<propertyname="maxWaitMillis" value="${redis.maxWait}" />
<propertyname="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
<!-- 连接工厂 -->
<bean id="JedisConnectionFactory"class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.host}" p:port="${redis.port}"p:password="${redis.pass}"p:pool-config-ref="poolConfig"/>
<!-- redis模板 -->
<bean id="redisTemplate"class="org.springframework.data.redis.core.RedisTemplate">
<propertyname="connectionFactory" ref="JedisConnectionFactory"/>
</bean>
<bean id="cacheManager"class="org.springframework.cache.support.SimpleCacheManager">
<propertyname="caches">
<set>
<!--这里可以配置多个redis -->
<beanclass="com.cky.rest.utils.RedisCache">
<propertyname="redisTemplate" ref="redisTemplate" />
<propertyname="name" value="content"/>
<!--name对应的名称要在类或方法的注解中使用 -->
</bean>
</set>
</property>
</bean>