问题:
在Web开发过程中,我们经常会需要对页面、对象、数据进行缓存,同时支持集群/分布式缓存。在javaEE中比较流行通过ehcache进行解决。学前参考:http://www.cnblogs.com/deepbreath/p/5456413.html
解决办法:
spring4.X+ehcache.X集群。代码如下:
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>testAll</artifactId> <groupId>com.yada.test</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>ehCacheDemo</artifactId> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>4.2.3.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>org.ehcache</groupId> <artifactId>ehcache</artifactId> <version>3.1.1</version> </dependency> <!-- spring begin--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <!--spring end--> <!-- ehcache 相关jar包 **beging** --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.10</version> </dependency> <dependency> <groupId>com.googlecode.ehcache-spring-annotations</groupId> <artifactId>ehcache-spring-annotations</artifactId> <version>1.2.0</version> </dependency> <!--<dependency>--> <!--<groupId>net.sf.ehcache</groupId>--> <!--<artifactId>ehcache</artifactId>--> <!--<version>2.10.2</version>--> <!--<type>pom</type>--> <!--</dependency>--> </dependencies> </project>
ehcache.xml
<?xml version="1.0" encoding="UTF-8"?> <ehcache> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" /> <cache name="infoCache" maxElementsInMemory="1000" maxElementsOnDisk="1000" overflowToDisk="false" diskSpoolBufferSizeMB="20" timeToIdleSeconds="180" timeToLiveSeconds="180" memoryStoreEvictionPolicy="LRU"> <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy=false, replicateRemovals=true "/> </cache> <!-- rmi本地cache服务 --> <cacheManagerPeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory" properties="hostName=localhost, port=40001, socketTimeoutMillis=2000"/> <!-- rmi远程cache服务 --> <cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory" properties="peerDiscovery=manual, rmiUrls= //22.7.16.98:40002/infoCache"/> </ehcache>
applicationContext-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:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"> <cache:annotation-driven cache-manager="ehcacheManager"/> <bean id="infoCache" class="com.test.ehcache.InfoCache"> <constructor-arg name="cacheManager" ref="ehcacheManager"/> <constructor-arg name="cacheName" value="infoCache"/> </bean> <!-- cacheManager工厂类,指定ehcache.xml的位置 --> <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:/ehcache.xml"/> </bean> <!-- 声明cacheManager --> <bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> <property name="cacheManager" ref="cacheManagerFactory"/> </bean> </beans>
InfoCache.java
package com.test.ehcache; import org.springframework.cache.Cache; import org.springframework.cache.ehcache.EhCacheCacheManager; /** * Created by ** on 2016/9/5. */ public class InfoCache { private EhCacheCacheManager cacheManager; private String cacheName; public InfoCache(EhCacheCacheManager cacheManager,String cacheName){ this.cacheManager =cacheManager ; this.cacheName = cacheName ; } public String put(String infoKey, String doc) { cacheManager.getCache(cacheName).put(infoKey, doc); return doc; } public String get(String smsKey) { Cache.ValueWrapper valueWrapper = cacheManager.getCache(cacheName).get(smsKey); if(valueWrapper==null){ // System.out.println(valueWrapper.get()); return "no result" ; } return valueWrapper.get().toString(); } public void remove(String smsKey) { cacheManager.getCache(cacheName).evict(smsKey); } }
App.java
package com.test.ehcache; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Created by ** on 2016/9/5. */ public class App { public static void main(String[] args) { try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); System.out.println("start secceed!"); InfoCache infoCache =(InfoCache)classPathXmlApplicationContext.getBean("infoCache") ; int num = 0 ; while (true){ try { Thread.sleep(1000); num++; System.out.println("Who are 2b ? "+infoCache.get("2b")); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
节点二:App.java
import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Created by ** on 2016/9/5. */ public class App { public static void main(String[] args) { ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); System.out.println("start secceed!"); InfoCache infoCache =(InfoCache)classPathXmlApplicationContext.getBean("infoCache") ; int num = 0 ; infoCache.put("2b","is bob !") ; while (true){ try { num++; infoCache.remove("2b"); infoCache.put("2b","is bob !") ; Thread.sleep(1000); } catch (Exception e) { e.printStackTrace(); } } } }
节点三:App.app
public class App { public static void main(String[] args) { ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); System.out.println("start secceed!"); InfoCache infoCache =(InfoCache)classPathXmlApplicationContext.getBean("infoCache") ; int num = 0 ; while (true){ try { Thread.sleep(1000); num++; System.out.println("Who are 2b ? "+infoCache.get("2b")); } catch (Exception e) { e.printStackTrace(); } } } }
结果输出:
Who are 2b ? no result
Who are 2b ? is bob !
Who are 2b ? is bob !
Who are 2b ? is bob !
Who are 2b ? is joy
Who are 2b ? is joy
Who are 2b ? is joy
Who are 2b ? is bob !
Who are 2b ? is bob !
Who are 2b ? is bob !
Who are 2b ? is joy
参数说明:
<diskStore>:当内存缓存中对象数量超过maxElementsInMemory时,将缓存对象写到磁盘缓存中(需对象实现序列化接口)。
<diskStore path="">:用来配置磁盘缓存使用的物理路径,Ehcache磁盘缓存使用的文件后缀名是*.data和*.index。
name:缓存名称,cache的唯一标识(ehcache会把这个cache放到HashMap里)。
maxElementsOnDisk:磁盘缓存中最多可以存放的元素数量,0表示无穷大。
maxElementsInMemory:内存缓存中最多可以存放的元素数量,若放入Cache中的元素超过这个数值,则有以下两种情况。
1)若overflowToDisk=true,则会将Cache中多出的元素放入磁盘文件中。
2)若overflowToDisk=false,则根据memoryStoreEvictionPolicy策略替换Cache中原有的元素。
Eternal:缓存中对象是否永久有效,即是否永驻内存,true时将忽略timeToIdleSeconds和timeToLiveSeconds。
timeToIdleSeconds:缓存数据在失效前的允许闲置时间(单位:秒),仅当eternal=false时使用,默认值是0表示可闲置时间无穷大,此为可选属性即访问这个cache中元素的最大间隔时间,若超过这个时间没有访问此Cache中的某个元素,那么此元素将被从Cache中清除。
timeToLiveSeconds:缓存数据在失效前的允许存活时间(单位:秒),仅当eternal=false时使用,默认值是0表示可存活时间无穷大,即Cache中的某元素从创建到清楚的生存时间,也就是说从创建开始计时,当超过这个时间时,此元素将从Cache中清除。
overflowToDisk:内存不足时,是否启用磁盘缓存(即内存中对象数量达到maxElementsInMemory时,Ehcache会将对象写到磁盘中),会根据标签中path值查找对应的属性值,写入磁盘的文件会放在path文件夹下,文件的名称是cache的名称,后缀名是data。
diskPersistent:是否持久化磁盘缓存,当这个属性的值为true时,系统在初始化时会在磁盘中查找文件名为cache名称,后缀名为index的文件,这个文件中存放了已经持久化在磁盘中的cache的index,找到后会把cache加载到内存,要想把cache真正持久化到磁盘,写程序时注意执行net.sf.ehcache.Cache.put(Element element)后要调用flush()方法。
diskExpiryThreadIntervalSeconds:磁盘缓存的清理线程运行间隔,默认是120秒。
diskSpoolBufferSizeMB:设置DiskStore(磁盘缓存)的缓存区大小,默认是30MB
memoryStoreEvictionPolicy:内存存储与释放策略,即达到maxElementsInMemory限制时,Ehcache会根据指定策略清理内存,共有三种策略,分别为LRU(最近最少使用)、LFU(最常用的)、FIFO(先进先出)。
相关推荐
以前都是用SSH,第一次使用spring+springMVC+mybatis自己做的测试练习,主要是配置注解的使用,加了稍微详细的注释说明。另外在测试ehcache的时候也继承进去了,紧做练习参考. 环境:tomcat使用7.x,JDK1.7开发工具...
ehcache缓存jar(ehcache-core-2.4.6.jar+ehcache-web-2.0.4.jar)
1.通过google ehcache-spring-annotatios.jar自动注解方式实现整合Spring+Ehcache。 2.Action里通过struts2-spring-plugin.jar插件自动根据名字注入。 3.Ajax无刷新异步调用Struts2,返回Json数据,以用户注册为例。...
开源测试项目:spring mvc+springsecurity3+ehcache+bootstrap+mysql 内附MySQL表,直接导入就可运行 效果图请移步:http://blog.csdn.net/yangxuan0261/article/details/10053947
hibernate4+spring4+springmvc+ehcache+自己写的cache系统
积分最低,Spring MVC+Mybatis+Ehcache+Apache Shiro+Bootstrap整合开发java仓库管理系统源码 开发环境:Eclipse ,JDK 1.8 ,Tomcat7 技术选型 后端技术 SpringMVC MVC框架 Spring Framework 容器 Apache ...
SpringBoo2.x整合Ehcache3.x作为缓存底层存储。
自己项目的开发包集合,其中包括:缓存处理ehcache相关jar,spring MVC4.0 jar,ehcache依赖jar,以及其他jar(图像处理thumbnailator-0.4.2),包虽然不是很新但可用。实际使用时找包较为麻烦,现在整理出来,希望...
spring+ehcache+mabatis。测试用例用的是mysql,数据库的配置在jdbc.properties里面。所要的sql在src/main/resources下的student.sql。测试的话调用controller下的update和getOne那两个接口测试。
spring + ehcache + redis两级缓存
Spring+EhCache实例源码,本实例的环境 eclipse + maven + spring + ehcache + junit,实例详解请访问博主博客:http://blog.csdn.net/u013142781
本后台管理系统,采用流行的框架springMvc+spring+mybatis+shiro+redis+ehcache开发,实现了权限管理(菜单权限、数据权限),solr全文搜索引擎,activiti工作流程引擎,cas单点登陆等功能,完善的代码生成器 后期还...
SpringMVC+Mybatis+Spring+Shiro+ehcache整合配置文件
struts1.x+spring+hibernate集成例子,包含所有jar包,ehcache二级缓存,数据库mysql,需要自己创建
Spring4MVC+Hibernate4+Freemarker+Ehcache+EasyUi整合,里面包含数据库,数据库使用的是Mysql
spring+ibatis+ehcache整合完整例子,数据库MySQL
spring+ehcache示例整合Demo
自己整合的Spring+Hibernate+Ehcache 整合的项目 需要struts的自己添加。 里面带有注释,而且带Jar.写的很清楚,良心分。
使用Intellj IDEA 集成开发工具 搭建的maven项目,使用Spring+Hibernate+Shiro+Ehcache集成,完成一个简单的用户角色菜单项目