快两个月没来更新了,由于报考了SCJP就一直都忙于复习,还好过了。
今天写Ehcache。Ehcache是一套比较成熟的缓存解决方案,很多主流框架像Spring,Hibernate都对其有很好的支持。且 Ehcache是支持集群环境的,API也比较简单,上手比较容易。下面就介绍一下Ehcache主要功能的使用和在Spring环境下如何集成。
Ehcache默认的配置文件是ehcache.xml,内容如下:
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd">
<!-- <diskStore path="c:\\mdcp_temp" />-->
<cacheManagerEventListenerFactory class="" properties="" />
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="600"
overflowToDisk="false"
>
</defaultCache>
<cache
name="configCache"
maxElementsInMemory="1000"
maxElementsOnDisk="1000"
eternal="true"
timeToIdleSeconds="300"
timeToLiveSeconds="1000"
overflowToDisk="false"
/>
</ehcache>
这里配置了一个名为configCache的缓存实例。参数说明如下:
name: 元素名称即缓存实例的名称。
maxElementsInMemory: 设定内存中保存对象的最大值。
overflowToDisk: 设置当内存中缓存到达maxElementsInMemory指定值时是否可以写到硬盘上。
eternal: 设置内存中的对象是否为永久驻留对象。如果是则无视timeToIdleSeconds和timeToLiveSeconds两个属性。
timeToIdleSeconds: 设置某个元素消亡前的停顿时间。指元素消亡之前,两次访问时间的最大时间间隔值。
timeToLiveSeconds: 设置某个元素消亡前的生存时间。指元素从构建到消亡的最大时间间隔。
注意:defaultCache不管用不用都是必须要配置的。
Java Code:
//初始化
CacheManager manager = new CacheManager(“src/config/ehcache.xml”);
//获取指定Cache对象
Cache configCache = manager.getCache(“configCache”);
//创建节点对象
Element element = new Element(“key1”,”value1”);
//保存节点到configCache
configCache.put(element);
//从configCache获取节点
Element element2 = configCache.getCache(“key1”);
Object value = element2.getValue();
//更新节点
configCache.put(new Element(“key1”,”value2”));
//删除节点
configCache.remove(“key1”);
以上是Ehcache的基本使用,是不是很简单?
下面介绍下在Spring环境下的配置。
首先需要一个jar文件:spring-modules-cache.jar。spring-module中包含了spring与其它框架的集成功能,除了cache外还有ant,jbpm,lucene,validation等。
Maven配置为:
<!-— Spring-modules-cache -->
<dependency>
<groupId>org.springmodules</groupId>
<artifactId>spring-modules-cache</artifactId>
<version>0.8</version>
</dependency>
<!-- Ehcache -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>1.6.2</version>
</dependency>
Spring, Spring-modules-cache 以及 Ehcache间的依赖关系如下:
![](/admin/uploads/diary/201106/17160633710.png)
首先Ehcache会依赖于其核心配置文件ehcache.xml, 而Spring也依赖于其配置文件application.xml。加入Spring-modules-cache.jar后就可以在application.xml中创建EhCacheManagerFactoryBean对象,这个类有一个属性configLocation用于指定ehcache的配置文件。这样依赖关系就变成了application.xml依赖于ehcache.xml了。Spring-modules-cache在两者间充当了一个“粘合剂”的作用。
接着是applicaion.xml中的配置:
<!-- load the ehcache config -->
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation">
<value>classpath:ehcache.xml</value>
</property>
</bean>
<!-- load the the configCache -->
<bean id="configCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<property name="cacheManager">
<ref local="cacheManager" />
</property>
<property name="cacheName">
<value>configCache</value>
</property>
</bean>
这样就可以通过Spring的IOC特性在需要用到configCache的类中进行注入。得到configCache后就能直接像之前那样操作cache对象了。
今天就写到这里,以后会继续更新Ehcache和Spring整合后利用AOP特性实现对于方法的缓存,以及在Hibernate中作为二级缓存的使用和它在集群环境中使用。
分享到:
相关推荐
基于公司的项目在Spring中集成Ehcache,并提供EhcaheUtils工具类,并通过Spring的AOP编程实现方法缓存注解话,先奉献出核心代码,需要的朋友可以参考哦!
spring集成ehcache所需的jar包
spring3整合EhCache注解实例
NULL 博文链接:https://cczakai.iteye.com/blog/1622708
1.通过google ehcache-spring-annotatios.jar自动注解方式实现整合Spring+Ehcache。 2.Action里通过struts2-spring-plugin.jar插件自动根据名字注入。 3.Ajax无刷新异步调用Struts2,返回Json数据,以用户注册为例。...
使用springboot 集成ehcache,里面附带数据库建库脚本,controller,service以及对ehcache集成,操作的详细样例,使用spring注解形式。
开源测试项目:spring mvc+springsecurity3+ehcache+bootstrap+mysql 内附MySQL表,直接导入就可运行 效果图请移步:http://blog.csdn.net/yangxuan0261/article/details/10053947
在Spring+Hibernate集成环境中使用EhCache缓存做的开发测试,在线程中打开数据库查询数据,及关闭数据库后查询数据的结果.
ssh,struts+hibernate+spring+ehcache集成
spring boot 集成activemq Datajpa Ehcache 里面有详细的例子
使用spring boot 创建的一个 集成了hibernate+shiro+ehcache的项目 分享
使用Intellj IDEA 集成开发工具 搭建的maven项目,使用Spring+Hibernate+Shiro+Ehcache集成,完成一个简单的用户角色菜单项目
SpringBoo2.x整合Ehcache3.x作为缓存底层存储。
项目简介:此项目只是简单的集成spring+springmvc+shiro+ehcahce 二: 步骤说明: 1:项目集成spring 在web.xml中配置spring容器的监听器。 2:项目集成springmvc 在web.xml中配置前端控制器 3:项目集成...
spring集成ehcache简单示例
使用SpringCache与Redis集成的优雅缓存解决方案,可以大大减少编写模板代码的工作量,提高应用程序的开发效率。同时,它还可以提高应用程序的性能,减少数据库压力。 SpringCache与Redis集成的优雅缓存解决方案是一...
Spring4MVC+Hibernate4+Freemarker+Ehcache+EasyUi整合,里面包含数据库,数据库使用的是Mysql
- Spring 缓存,包括redis、ehcache、spring-cache、memcached、使用redis实现session共享 等。 - spring-docs - Spring 文档生成工具,包括 Swagger - spring-bussiness - Spring 业务应用,包括 AOP、过滤...
NULL 博文链接:https://412887952-qq-com.iteye.com/blog/2294952
struts2+spring+hibernate集成例子,包含所有jar包,ehcache二级缓存,mysql数据,需要自己创建