配置一个spring默认实现的缓存(cache)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
<!--启用缓存 -->
<cache:annotation-driven />
<!-- 声明缓存管理器 -->
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean name="myCache"
class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"></bean>
</set>
</property>
</bean>
</beans>
如上图所式,
第一步 启用缓存
第二步 声明缓存管理器。
这里的缓存管理器
SimpleCacheManager
这是一个spring内置缓存管理器的实现,使用属性注入注入一个
ConcurrentMapCacheFactoryBean。
这是一个spring的基于HashMap的实现的缓存。个人理解就是利用java的map集合做的一个在内存中的缓存。
第三部 开始使用缓存,这里对于缓存的使用是基于spring的注解。如
@Cacheable(value = "myCache", key = "'employee.'+#p0")
@CacheEvict(value = "myCache", key = "'employee.'+#p0")
@CachePut(value = "myCache", key = "'employee.'+#p0")
不再赘述各个注解的具体使用方法。
spring整合配置ehcache缓存
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
<!--启用缓存 -->
<cache:annotation-driven />
<!-- 声明缓存管理器 -->
<bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml"/>
</bean>
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="cacheManagerFactory"/>
</bean>
</beans>
第一步:在spring项目中,添加ehcache的相关jar包。
第二步:上图的xml配置文件,和第一个很类似,不过这里的cacheFactory换成了ehcache的了。
第三步:既然是基于ehcache的缓存,自然需要使用ehcache的配置文件。这个在第二步里面也有引用。我的配置如下。
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<diskStore path="c:/ehcache"/>
<defaultCache
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false"/>
<cache name="myCache"
maxElementsOnDisk="20000"
maxElementsInMemory="1000"
eternal="true"
overflowToDisk="true"
diskPersistent="true"/>
</ehcache>
配置完上面这些,关于缓存的使用我这里都是基于注解的,所以代码里未做任何改变。依然ok。
配置redis缓存
配置redis缓存前,需要导入相关的jar包并且配置一些reids的配置
jar包 spring-data-redis
jedis
commons-pool2(jedis的连接池)
接下来,我将把所有的reids和使用redis做缓存的配置写在一个配置文件中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
<!--启用缓存 -->
<cache:annotation-driven />
<!-- 配置jedis连接池 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="50" />
<property name="maxTotal" value="100" />
<property name="maxWaitMillis" value="20000" />
</bean>
<!--配置jedis的连接工厂 -->
<bean id="connectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="127.0.0.1"></property>
<property name="port" value="6379" />
<property name="usePool" value="true" />
<property name="poolConfig" ref="poolConfig" />
</bean>
<!--主键的序列化方式 -->
<bean id="stringRedisSerializer"
class="org.springframework.data.redis.serializer.StringRedisSerializer" />
<!--对象的序列化方式 -->
<bean id="jdkSerializationRedisSerializer"
class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"></bean>
<!-- 配置redisTemplate -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="defaultSerializer" ref="stringRedisSerializer" />
<property name="keySerializer" ref="stringRedisSerializer" />
<property name="valueSerializer" ref="jdkSerializationRedisSerializer" />
</bean>
<!-- 声明reids缓存管理器 -->
<bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
<constructor-arg index="0" ref="redisTemplate"></constructor-arg>
</bean>
</beans>
如上面的配置文件
第一步:启用缓存
第二步: 配置jedis连接池
第三步:配置jedis的连接工厂
第四部:主键的序列化方式
第五步:对象的序列化方式
第六步:配置redisTemplate
第七步:声明reids缓存管理器
做好了上面的配置之后,同样的在程序中使用注解来使用缓存,效果如下图。缓存需要缓存的对象被放入了redis中。