- 浏览: 188761 次
- 性别:
- 来自: 杭州
文章分类
最新评论
-
静夜独窗:
你确定你这个回调对应的是模板方法模式,而不是策略模式?
Java回调与模板方法模式 -
jzp12:
while (keySetIterator.hasNext( ...
keySet 与entrySet 遍历HashMap性能差别 -
LyAn_爱踢爱死:
那个Context是哪个包下的。
Java回调与模板方法模式 -
jhy_wind:
请问,你的mtrat插件是windows的吗?我在网上只找到了 ...
Spring中的ThreadPoolTaskExecutor -
ysushiwei:
差别确实不大,基本上可以忽略。我测试的是10万的数据量级别的。 ...
keySet 与entrySet 遍历HashMap性能差别
一.Ehcache简介
EhCache是一个纯Java的进程内缓存框架,具有如下特点:
1. 快速简单,非常容易和应用集成。
2.支持多种缓存策略 。
3. 缓存数据有两级:内存和磁盘,因此无需担心容量问题 。
4. 缓存数据会在虚拟机重启的过程中写入磁盘 。
5. 可以通过RMI、可插入API等方式进行分布式缓存。
6. 具有缓存和缓存管理器的侦听接口 。
7. 支持多缓存管理器实例,以及一个实例的多个缓存区域 等特点。
二.Ehcache配置的相关参数
Ehcache的配置很灵活,官方提供的配置方式有好几种,你可以通过声明配置、在xml中配置、在程序里配置或者调用构造方法时传入不同的参数。下面以最常用的XML配置为例说下配置的相关参数的意义,ehcache.xml是最常见的一个文件,ehcache一般会通过CacheManager从classpath加载该文件完成Cache的实例化。
1.ehcache.xml中的配置信息
ehcache.xml片段:
<ehcache> <diskStore path="java.io.tmpdir"/> <defaultCache name="name" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" /> </ehcache>
2.Cache中常用参数的具体意义
(1)name:Cache的唯一标识。
(2)maxElementsInMemory:内存中最大缓存对象数。
(3)eternal:Element是否永久有效,一旦设置true,timeout将不起作用。
(4)timeToIdleSeconds:设置Element在失效前的允许闲置时间。仅当element不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。
(5)timeToLiveSeconds:设置Element在失效前允许存活时间。最大时间介于创建时间和失效时间之间。仅当element不是永久有效时使用,默认是0.,也就是element存活时间无穷大。
(6)overflowToDisk:配置此属性,当内存中Element数量达到maxElementsInMemory时,Ehcache将会Element写到磁盘中。
(7)maxElementsOnDisk:磁盘中最大缓存对象数,若是0表示无穷大。
(8) memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理缓存中的内容。默认策略是LRU(最近最少使用),你也可以设置为FIFO(先进先出)或是LFU(较少使用)
三.Spring和Ehcache的集成
1.ehcache.xml
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd"> <diskStore path="java.io.tmpdir" /> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="600" overflowToDisk="false"> </defaultCache> <cache name="levelOneCache" maxElementsInMemory="1000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="1000" overflowToDisk="false" /> </ehcache>
2.beans.xml的配置
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation"> <value>classpath:ehcache.xml</value> </property> </bean> <bean id="levelOneCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean"> <property name="cacheManager"> <ref local="cacheManager" /> </property> <property name="cacheName"> <value>configCache</value> </property> </bean>
3.测试类
package org.mango.cache.ehcache; import net.sf.ehcache.Cache; import net.sf.ehcache.CacheManager; import net.sf.ehcache.Element; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; public class EhcacheTest { public static void main(String[] args) { Resource res = new ClassPathResource("beans.xml"); BeanFactory factory = new XmlBeanFactory(res); CacheManager cacheManager = (CacheManager) factory.getBean("cacheManager"); Cache levelOneCache = cacheManager.getCache("levelOneCache"); CacheObject cacheObject = null; for (int i = 0; i < 10; i++) { Element element = levelOneCache.get("key"); if (element == null) { cacheObject = new CacheObject("test"); element = new Element("key", cacheObject); levelOneCache.put(element); System.out.println("cacheObject[" + cacheObject + "]" + ",无法从缓存中取到"); } else { cacheObject = (CacheObject) element.getValue(); System.out.println("cacheObject[" + cacheObject + "]" + ",从缓存中取到"); } } } }
输出如下:
cacheObject[name:test],无法从缓存中取到 cacheObject[name:test],从缓存中取到 cacheObject[name:test],从缓存中取到 cacheObject[name:test],从缓存中取到 cacheObject[name:test],从缓存中取到
四.利用Spring AOP和Ehcache实现线程级方法缓存
在复杂的业务逻辑或在一次计算中需多次调用同一个DAO或远程服务,在这种情况下,均可对计算结果缓存起来,不但可以减少了不必要的调用次数,还同时可以提高系统运算性能。下面以缓存一个service为例说明一下其用法。
1.TestService接口
public interface TestService { /** * 根据userId取得用户名。 * * @param userId * @return */ public String getUserName(String userId); }
2.TestServiceImpl实现类
public class TestServiceImpl implements TestService { /* * @see org.mango.cache.ehcache.TestService#getUserName(java.lang.String) */ public String getUserName(String userId) { return userId; } }
3.拦截器的实现
public class CacheInterceptor implements MethodInterceptor { private Cache cache; /** * @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation) */ public Object invoke(MethodInvocation invocation) throws Throwable { Method method = invocation.getMethod(); String methodName = method.getName(); Object[] arguments = invocation.getArguments(); Object result = invocation.proceed(); String targetName = method.getDeclaringClass().getName(); String key = getCacheKey(targetName, methodName, arguments); Element element = cache.get(key); if (element == null) { result = invocation.proceed(); System.out.println("第一次调用方法并缓存其值:" + result); cache.put(new Element(key, result)); } else { result = element.getValue(); System.out.println("从缓存中取得的值为:" + result); } return result; } /** * 生成缓存中的KEY值。 */ protected String getCacheKey(String targetName, String methodName, Object[] arguments) { StringBuffer sb = new StringBuffer(); sb.append(targetName).append(".").append(methodName); if ((arguments != null) && (arguments.length != 0)) { for (int i = 0; i < arguments.length; i++) { sb.append(".").append(arguments[i]); } } return sb.toString(); } public void setCache(Cache cache) { this.cache = cache; } }
4.Bean的配置
<bean id="testService" class="org.mango.cache.ehcache.TestServiceImpl" /> <bean id="serviceMethodInterceptor" class="org.mango.cache.ehcache.CacheInterceptor"> <property name="cache"> <ref local="levelOneCache" /> </property> </bean> <bean id="serviceAutoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="interceptorNames"> <list> <value>serviceMethodInterceptor</value> </list> </property> <property name="beanNames"> <value>*Service</value> </property> </bean>
5.测试方法
public class ServiceTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); TestService testService = (TestService) context.getBean("testService"); for (int i = 0; i < 5; i++) { testService.getUserName("mango"); } } }
其输出结果如下:
第一次调用方法并缓存其值:mango 从缓存中取得的值为:mango 从缓存中取得的值为:mango 从缓存中取得的值为:mango 从缓存中取得的值为:mango
评论
请问在 web中如何检测 虚拟机重启 了?
谢谢!
ehcache适合做jvm内缓存(即在一个jvm内缓存),如果需要集群可以直接使用memcache(分布式缓存)。
尽量不要同步缓存,而是使用分布式。
分布式?什么意思?缓存同步不是分布式系统的缓存解决办法么?
集群 不同于分布式 先搞清楚概念吧
ehcache适合做jvm内缓存(即在一个jvm内缓存),如果需要集群可以直接使用memcache(分布式缓存)。
尽量不要同步缓存,而是使用分布式。
分布式?什么意思?缓存同步不是分布式系统的缓存解决办法么?
ehcache适合做jvm内缓存(即在一个jvm内缓存),如果需要集群可以直接使用memcache(分布式缓存)。
尽量不要同步缓存,而是使用分布式。
发表评论
-
Spring Dynamic Modules测试框架源码分析
2012-08-25 17:57 2718一.问题描述 在用spring-dm对某bundle ... -
Spring中的ThreadPoolTaskExecutor
2011-12-14 12:09 34177在观察线上系统的运行 ... -
JAXP在Spring中应用
2011-12-12 17:21 1754在阅读spring源码的过程中,想看下spring对x ... -
Spring中的FactoryBean
2011-12-08 23:50 52681.概述 Spring中有两种类型的Bean ... -
Spring MVC中VM页面使用自定义宏
2011-05-28 18:22 7753在使用vm做为视图展示时,Spring框架本身 ... -
Spring MVC的velocity视图技术
2011-05-21 20:55 112471.Velocity Velo ... -
AOP
2011-04-24 22:43 0<bean id="helloWordDao& ... -
spring中加载Bean配置文件的常用方式
2011-02-22 21:47 2786Spring中加载Bean配置文件的常用方式有两种,一种是通过 ... -
Spring MVC与Web Flow的集成
2010-01-10 16:03 7422要在Spring MVC环境 ... -
遇到了一个很怪的错误,记录下来
2008-12-06 21:42 19391.问题描述 org.springframework.bea ...
相关推荐
需要使用Spring来实现一个Cache简单的解决方案,具体需求如下:使用任意一个现有开源Cache Framework,要求可以Cache系统中Service或则DAO层的get/find等方法返回结果,如果数据更新(使用Create/update/delete方法...
本代码通过使用spring aop+ehcache的技术,实现了方法级别的查询缓存,主要原理是 方法的完整路径+方法参数值,作为key,放入cache中,下次访问时先判断cache中是否有该key.
NULL 博文链接:https://honda418.iteye.com/blog/354945
缘起需求:需要使用Spring来实现一个Cache简单的解决方案,具体需求如下:使用任意一个现有开源Cache Framework,要求可以Cache系统中Service或者DAO层的get/find等方法返回结果,如果数据更新(使用Create/update/...
【EhCache】Java缓存框架使用EhCache结合Spring AOP EhCache是一个纯Java的进程内缓存框架,具有如下特点: 1. 快速简单,非常容易和应用集成。 2.支持多种缓存策略 。 3. 缓存数据有两级:内存和磁盘,因此无需...
基于公司的项目在Spring中集成Ehcache,并提供EhcaheUtils工具类,并通过Spring的AOP编程实现方法缓存注解话,先奉献出核心代码,需要的朋友可以参考哦!
Context-support模块:提供了对第三方库嵌入Spring应用的集成支持,比如缓存(EhCache、Guava、JCache)、邮件服务(JavaMail)、任务调度(CommonJ、Quartz)和模板引擎(FreeMarker、JasperReports、速率)。 SpEL模块:...
spring中使用缓存的方案,根据需求,使用Spring AOP + ehCache可实现些功能,使用EHCACHE能方便地结合SPRING来实现缓存。
2、Spring Cache利用了AOP,实现了基于注解的缓存功能,并且进行了合理的抽象,业务代码不用关心底层是使用了什么缓存框架,只需要简单地加一个注解,就能实现缓存功能了,做到了对代码侵入性做小。 3、由于市面上的...
Aspect Cache是一个针对Spring Boot,基于AOP注解方式的轻量级缓存,目前支持EHCache,Redis缓存方式。数据类型支持POJO、Map和List数据类型,同时支持自定义缓存key解析,也支持自定义的缓存处理方式,或者扩展支持...
- Spring 缓存,包括redis、ehcache、spring-cache、memcached、使用redis实现session共享 等。 - spring-docs - Spring 文档生成工具,包括 Swagger - spring-bussiness - Spring 业务应用,包括 AOP、过滤...
1.标题所提及技术的整合,Spring包括mvc、aop、ioc等。个人属于强迫症类型,技术水平怎么样再说,代码必须好看 2.Hibernate几个级别缓存对比。见DaoImpl类 3.Ehcache方法缓存及页面缓存。见applicationContext-cache...
使用Spring AOP技术 + ehcache做的一个缓存。此代码为转载,供大家学习使用。
现在使用的缓存技术很多,比如Redis、 Memcache 、 EhCache等,甚至还有使用ConcurrentHashMap 或 HashTable 来实现缓存。但在缓存的使用上,每个人都有自己的实现方式,大部分是直接与业务代码绑定,随着业务...
Aspect Cache是一个针对Spring Boot,基于AOP注解方式的轻量级缓存,目前支持EHCache,Redis缓存方式。数据类型支持POJO、Map和List数据类型,同时支持自定义缓存key解析,也支持自定义的缓存处理方式,或者扩展支持...
Spring Boot中使用Ehcache缓存数据 Spring Boot中的JSON技术 Spring Boot中编写单元测试 Spring Boot整合Swagger2构建RESTful API 使用Actuator监控Spring Boot应用 使用Spring Boot发送邮件 使用Spring Boot Admin...
本实例的环境 eclipse + maven + spring + ehcache + junit EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的...使用Spring的AOP进行整合,可以灵活的对方法的返回结果对象进行缓存
(10) spring-support.jar 这个jar文件包含支持缓存Cache(ehcache)、JCA、JMX、邮件服务(Java Mail、COS Mail)、任务计划Scheduling(Timer、Quartz)方面的类。 (11) spring-web.jar 这个jar文件包含Web应用...