ehcache缓存配置(注解方式)

spring cache

Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,香港虚拟主机,它本质上不是一个具体的缓存实现方案(例如 EHCache 或者 OSCache),而是一个对缓存使用的抽象,通过在既有代码中添加少量它定义的各种 annotation,即能够达到缓存方法的返回对象的效果。

Spring 的缓存技术还具备相当的灵活性,不仅能够使用 SpEL(Spring Expression Language)来定义缓存的 key 和各种 condition,还提供开箱即用的缓存临时存储方案,也支持和主流的专业缓存例如 EHCache 集成。

其特点总结如下: 通过少量的配置 annotation 注释即可使得既有代码支持缓存 支持开箱即用 Out-Of-The-Box,即不用安装和部署额外第三方组件即可使用缓存 支持 Spring Express Language,能使用对象的任何属性或者方法来定义缓存的 key 和 condition 支持 AspectJ,并通过其实现任何方法的缓存支持 支持自定义 key 和自定义缓存管理者,具有相当的灵活性和扩展性

本人主要讲解spring与ehcache整合方法(基于maven项目),香港服务器租用,至于spring cache 详细介绍请参考《注解驱动的spring cache 缓存介绍》,为了能更好的理解,建议在看本文前先把这篇的文章的例子做一遍。

maven jar包准备工作

主要需要如下几个

slf4j-log4j12 (log4j日志)

ehcache-core (ehcache核心包)

spring-webmvc (spring webmvc)

spring-context-support(spring 上下文扩展包)

测试需要

junit

spring-test

好了以上这些足够,美国空间,不用再添加其他的内容。当然maven项目会自动将依赖报加载进来。附一张jar包图

再附上pom 文件配置:

org.slf4jslf4j-log4j121.7.5net.sf.ehcacheehcache-core2.6.6org.springframeworkspring-webmvc3.2.3.RELEASEorg.springframeworkspring-context-support3.2.3.RELEASEorg.springframeworkspring-test3.2.3.RELEASEtestjunitjunit4.10test

配置spring

Spring-cache-anno.xml 文件:

xmlns:xsixmlns:aopxmlns:jeexmlns:utilxmlns:toolxsi:schemaLocation=” http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/jee/spring-jee-3.1.xsd http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/util/spring-util-3.1.xsd http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/tool/spring-tool-3.1.xsd http://www.springframework.org/schema/cache/spring-cache-3.1.xsd”default-lazy-initp:configLocationp:cacheManager-ref

配置ehcache

xsi:noNamespaceSchemaLocationmonitoring配置自定义缓存maxElementsInMemory:缓存中允许创建的最大对象数eternal:缓存中对象是否为永久的,如果是,超时设置将被忽略,对象从不过期。timeToIdleSeconds:缓存数据的钝化时间,也就是在一个元素消亡之前,两次访问时间的最大时间间隔值,这只能在元素不是永久驻留时有效,如果该值是 0 就意味着元素可以停顿无穷长的时间。timeToLiveSeconds:缓存数据的生存时间,也就是一个元素从构建到消亡的最大时间间隔值,这只能在元素不是永久驻留时有效,如果该值是0就意味着元素可以停顿无穷长的时间。overflowToDisk:内存不足时,是否启用磁盘缓存。memoryStoreEvictionPolicy:缓存满了之后的淘汰算法。timeToIdleSecondsmaxElementsOnDiskdiskExpiryThreadIntervalSeconds<cache maxElementsInMemory=”10000″maxElementsOnDisk=”1000″ eternal=”false” overflowToDisk=”true”diskSpoolBufferSizeMB=”20″ timeToIdleSeconds=”300″ timeToLiveSeconds=”600″memoryStoreEvictionPolicy=”LFU” />maxElementsOnDiskdiskSpoolBufferSizeMB

测试缓存

测试文件 Main.java

package com.cacheOfAnno;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.bean.Account;public class Main {/*** @param args main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext(“spring-cache-anno.xml”);// 加载 spring 配置文件AccountService s = (AccountService) context.getBean(“accountServiceBean1”);// 第一次查询,应该走数据库System.out.print(“first query…”);s.getAccountByName(“som”);// 第二次查询,应该不查数据库,直接返回缓存的值System.out.print(“second query…”);s.getAccountByName(“som”);System.out.println();System.out.println(“start testing clear cache…”);// 更新某个记录的缓存,首先构造两个账号记录,然后记录到缓存中Account account1 = s.getAccountByName(“somebody1”);Account account2 = s.getAccountByName(“somebody2”);// 开始更新其中一个account1.setId(1212);s.updateAccount(account1);s.getAccountByName(“somebody1”);// 因为被更新了,所以会查询数据库s.getAccountByName(“somebody2”);// 没有更新过,应该走缓存s.getAccountByName(“somebody1”);// 再次查询,应该走缓存// 更新所有缓存System.out.println();s.reload();s.getAccountByName(“somebody1”);// 应该会查询数据库s.getAccountByName(“somebody2”);// 应该会查询数据库s.getAccountByName(“somebody1”);// 应该走缓存s.getAccountByName(“somebody2”);// 应该走缓存 }}

测试结果:

创造条件,去改变生活,做生活的强者.愿你早日成为生活的强者

ehcache缓存配置(注解方式)

相关文章:

你感兴趣的文章:

标签云: