spring项目中通过xml配置缓存(cache)记录贴

配置一个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中。


  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot,可以使用application.yml或application.properties文件来配置Spring Cache。以下是一个在yml文件配置Spring Cache的示例: ```yaml spring: cache: type: caffeine ``` 在以上示例配置Spring Cache的类型为Caffeine,也可以改为其他类型,如Ehcache,Redis等。例如,配置Ehcache作为缓存类型的示例: ```yaml spring: cache: type: ehcache ehcache: config: classpath:ehcache.xml ``` 在以上示例配置Spring Cache的类型为Ehcache,并指定了Ehcache配置文件的位置为classpath:ehcache.xml。如果不想使用外部的Ehcache配置文件,也可以直接在yml文件配置Ehcache的参数,如下所示: ```yaml spring: cache: type: ehcache ehcache: config: # Ehcache配置参数 maxEntriesLocalHeap: 1000 timeToLiveSeconds: 3600 ``` 在以上示例配置Ehcache的maxEntriesLocalHeap参数为1000,timeToLiveSeconds参数为3600秒。这些参数可以根据具体的需求进行调整。 除了缓存类型的配置,还可以在yml文件配置缓存的具体实现,如Caffeine的缓存大小和过期时间等。例如: ```yaml spring: cache: caffeine: spec: maximumSize=500,expireAfterAccess=5m ``` 在以上示例,指定了Caffeine的缓存大小为500,过期时间为5分钟。 需要注意的是,配置不同的缓存类型需要引入不同的依赖包,并且需要在代码使用对应的注解来使用缓存。更多关于Spring Cache配置和使用可以参考Spring官方文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值