maven+spring+ehcache缓存注解 学习总结

1 篇文章 0 订阅
0 篇文章 0 订阅

前提条件

1.此文适合spring3.1版本以上(包括3.1)

MAVEN配置pom.xml

<!-- 引入EHCACHE缓存 -->
<dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache-core</artifactId>
    <version>2.1.0</version>
</dependency>
Spring配置文件spring-ehcache.xml

<?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:context="http://www.springframework.org/schema/context"
	xmlns:oxm="http://www.springframework.org/schema/oxm" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:ehcache="http://www.springframework.org/schema/cache"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.0.xsd  
       http://www.springframework.org/schema/oxm 
       http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd  
       http://www.springframework.org/schema/mvc 
       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  
       http://www.springframework.org/schema/cache
       http://www.springframework.org/schema/cache/spring-cache.xsd">

	<!-- 支持缓存注解 cache-manager默认为cacheManager,如果定义bean的id为cacheManeger,此处可以省略 -->  
	<ehcache:annotation-driven cache-manager="cacheManager" />

	<!--  缓存工厂,指定ehcache.xml位置-->
	<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>

Ehcache配置文件ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
    <defaultCache eternal="true" maxElementsInMemory="100" overflowToDisk="false" />
    <!-- 缓存配置-->
    <cache name="queryCache" maxElementsInMemory="2000" eternal="false"
<span style="white-space:pre">		</span>timeToIdleSeconds="900" timeToLiveSeconds="1800" overflowToDisk="true" />
<span style="white-space:pre">		</span>
<span style="white-space:pre">		</span><cache name="deleteCache" maxElementsInMemory="2000" eternal="false"
<span style="white-space:pre">		</span>timeToIdleSeconds="900" timeToLiveSeconds="1800" overflowToDisk="true" />
</ehcache>

Cache配置中的几个属性: 


name:Cache的名称,必须是唯一的(ehcache会把这个cache放到HashMap里)。 
maxElementsInMemory:内存中保持的对象数量。 
maxElementsOnDisk:DiskStore中保持的对象数量,默认值为0,表示不限制。 
eternal:是否是永恒数据,如果是,则它的超时设置会被忽略。 
overflowToDisk:如果内存中数据数量超过maxElementsInMemory限制,是否要缓存到磁盘上。 
timeToIdleSeconds:对象空闲时间,指对象在多长时间没有被访问就会失效。只对eternal为false的有效。默认值0,表示一直可以访问。 
timeToLiveSeconds:对象存活时间,指对象从创建到失效所需要的时间。只对eternal为false的有效。默认值0,表示一直可以访问。 
diskPersistent:是否在磁盘上持久化。指重启jvm后,数据是否有效。默认为false。 
diskExpiryThreadIntervalSeconds:对象检测线程运行时间间隔。标识对象状态的线程多长时间运行一次。 
diskSpoolBufferSizeMB:DiskStore使用的磁盘大小,默认值30MB。每个cache使用各自的DiskStore。 
memoryStoreEvictionPolicy:如果内存中数据超过内存限制,向磁盘缓存时的策略。默认值LRU,可选FIFO、LFU。 


java代码

1.测试类

public class EhcacheTest {
	
	//测试添加缓存
	public void addEhcacheTest() {
		E<span style="font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px; background-color: rgb(248, 248, 248);">hcacheServiceImpl <span style="font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px; background-color: rgb(248, 248, 248);">ehcacheServiceImpl= new E<span style="font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px; background-color: rgb(248, 248, 248);">hcacheServiceImpl();</span></span></span>
		System.out.println(ehcacheServiceImpl.checkIdCardOrghCard("111"));// 第一次走数据库
		System.out.println(ehcacheServiceImpl.checkIdCardOrghCard("111"));// 第二次不走数据库,走缓存
	}
	//测试清除缓存
	public void deleteEhcacheTest(){
		ehcacheServiceImpl.checkIdCardOrghCard1("111");
	}

}



2.service类

public class EhcacheServiceImpl {
	@Cacheable(value="queryCache",key="#idCode")
	public String checkIdCardOrghCard(<span style="font-family: Arial, Helvetica, sans-serif;">String </span><span style="font-family: Arial, Helvetica, sans-serif;">idCode</span><span style="font-family: Arial, Helvetica, sans-serif;">) {</span>
		// TODO Auto-generated method stub
		System.out.println("测试");
		return idCode;
	}
	@CacheEvict(value="queryCache",key="#idCode")
	public void checkIdCardOrghCard1(String <span style="font-family: Arial, Helvetica, sans-serif;">idCode</span><span style="font-family: Arial, Helvetica, sans-serif;">) {</span>
		// TODO Auto-generated method stub
		System.out.println("清除缓存"+idCode);
	}
}

3.Spring原生的缓存的注解共有四个:

  •     @Cacheable  :应用到读取数据的方法上,即可缓存的方法,如查找方法:先从缓存中读取,如果没有再调用方法获取数据,然后把数据添加到缓存中
  •     @CacheEvict :即应用到移除数据的方法上,如删除方法,调用方法时会从缓存中移除相应的数据
  •     @CachePut   :应用到写数据的方法上,如新增/修改方法,调用方法时会自动把相应的数据放入缓存
  •     @Caching    :上面三种注解配置方法时,一个方法只能使用三者之一。如果要使用多个,则需要使用@Caching
4.注解属性介绍

  • key="#idCode"  对象缓存的key值,需要保证唯一,用的是Spring的 SpEL表达式, 取值为idCode值,默认为类名+方法名+参数值
  • value="queryCache":指的是ehcache.xml里的缓存名字
  • 支持条件condition,例如:属性 id<10
  • CacheEvict特有两个属性allEntries 表示调用之后,清空缓存,默认false,  还有个beforeInvocation 属性,表示先清空缓存,再进行查询  

测试类执行结果

执行三次方法,顺序addEhcacheTest()-->deleteEhcacheTest()-->addEhcacheTest()

测试
111
111
清除缓存111
测试
111
111


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值