package com.ge.energy.confirm.cache;
import java.lang.reflect.Method;
import java.util.List;
import net.sf.ehcache.Cache;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
public class EhCacheAfterAdvice implements AfterReturningAdvice,
InitializingBean {
private static final Log logger = LogFactory
.getLog(EhCacheAfterAdvice.class);
private Cache cache;
public void setCache(Cache cache) {
this.cache = cache;
}
public EhCacheAfterAdvice() {
super();
}
public void afterReturning(Object arg0, Method arg1, Object[] arg2,
Object arg3) throws Throwable {
String className = arg3.getClass().getName();
@SuppressWarnings("rawtypes")
List list = cache.getKeys();
for (int i = 0; i < list.size(); i++) {
String cacheKey = String.valueOf(list.get(i));
if (cacheKey.startsWith(className)) {
cache.remove(cacheKey);
logger.debug("remove cache " + cacheKey);
}
}
}
public void afterPropertiesSet() throws Exception {
Assert.notNull(cache,
"Need a cache. Please use setCache(Cache) create it.");
}
}
-------------------------------------------------------------------
package com.ge.energy.confirm.cache;
import java.io.Serializable;
import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
public class EhCacheInterceptor implements MethodInterceptor, InitializingBean {
private static final Log logger = LogFactory
.getLog(EhCacheInterceptor.class);
private Cache cache;
public void setCache(Cache cache) {
this.cache = cache;
}
public EhCacheInterceptor() {
super();
}
/**
* Interceptor service .*find*.*,the result is or not exit,
* if it is exitting return the cache result, otherwire,connect db ,
* get dataset,& put the dataset into cache
*/
public Object invoke(MethodInvocation invocation) throws Throwable {
String targetName = invocation.getThis().getClass().getName();
String methodName = invocation.getMethod().getName();
Object[] arguments = invocation.getArguments();
Object result;
logger.debug("Find object from cache is " + cache.getName());
String cacheKey = getCacheKey(targetName, methodName, arguments);
Element element = cache.get(cacheKey);
if (element == null) {
logger.debug("Hold up method , Get method result and create cache........!");
result = invocation.proceed();
element = new Element(cacheKey, (Serializable)result);
cache.put(element);
}
return element.getValue();
}
/**
* get cache key method,cache key is the one Cache of Element unqire id, cache key is
* package+className+methodName,eg:com.ge.energy.confirm.service.BucketizationService.findModelsBySSOID(str)
*/
private 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();
}
/**
* implement InitializingBean,check cache or not null
*/
public void afterPropertiesSet() throws Exception {
Assert.notNull(cache,
"Need a cache. Please use setCache(Cache) create it.");
}
}
--------------------------------------------------------------------------
ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<!-- <diskStore path="${jboss-home}\\server\\default\\deploy\\pgs.war\\cache"/> -->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
/>
<cache name="pgs_cache"
maxElementsInMemory="100000"
eternal="false"
timeToIdleSeconds="300000"
timeToLiveSeconds="600000"
overflowToDisk="true"
/>
</ehcache>
-----------------------------------------------------
<!-- the below that is passable annotation use spring spring aop -->
<!-- get ehCache configuration file -->
<bean id="defaultCacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation">
<value>classpath:ehcache.xml</value>
</property>
</bean>
<!-- definition ehCache factory,and set usefull Cache name -->
<bean id="ehCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<property name="cacheManager">
<ref bean="defaultCacheManager" />
</property>
<property name="cacheName">
<value>pgs_cache</value>
</property>
</bean>
<!-- find/create cache interceptor -->
<bean id="ehCacheInterceptor" class="com.ge.energy.confirm.cache.EhCacheInterceptor">
<property name="cache">
<ref bean="ehCache" />
</property>
</bean>
<!-- flush cache interceptor -->
<bean id="ehCacheAfterAdvice" class="com.ge.energy.confirm.cache.EhCacheAfterAdvice">
<property name="cache">
<ref bean="ehCache" />
</property>
</bean>
<!-- Point -->
<bean id="ehCachePointCut"
class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice">
<ref bean="ehCacheInterceptor" />
</property>
<property name="patterns">
<list>
<value>.*find*.*</value>
</list>
</property>
</bean>
<!-- Point Advice -->
<bean id="ehCachePointCutAdvice"
class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice">
<ref bean="ehCacheAfterAdvice" />
</property>
<property name="patterns">
<list>
<value>.*save*.*</value>
<value>.*update*.*</value>
<value>.*delete*.*</value>
</list>
</property>
</bean>
<bean id="bucketizationServiceCache" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<ref bean="bucketizationService"></ref>
</property>
<property name="interceptorNames">
<list>
<value>ehCachePointCut</value>
<value>ehCachePointCutAdvice</value>
</list>
</property>
</bean>
/////////////////////////////////////////////////








































拦截器将使用 ”org.taha.cache.METHOD_CACHE” 区域缓存方法返回结果。下面利用 Spring IoC 让 bean 来访问这一区域。































InitializingBean {
private static final Log logger = LogFactory
.getLog(MethodCacheInterceptor.class);
private Cache cache;
public void setCache(Cache cache) {
this.cache = cache;
}
/**
*
*/
public MethodCacheInterceptor() {
super();
// TODO 自动生成构造函数存根
}
/**
* 主方法
* 如果某方法可被缓存就缓存其结果
* 方法结果必须是可序列化的(serializable)
*/
public Object invoke(MethodInvocation invocation) throws Throwable {
String targetName = invocation.getThis().getClass().getName();
String methodName = invocation.getMethod().getName();
Object[] arguments = invocation.getArguments();
Object result;
logger.debug("在缓存中查找方法返回的对象!");
String cacheKey = getCacheKey(targetName, methodName, arguments);
Element element = cache.get(cacheKey);
if (element == null) {
logger.debug("正在拦截方法!");
result = invocation.proceed();
logger.debug("正在缓存对象!");
element = new Element(cacheKey, (Serializable)result);
cache.put(element);
}
return element.getValue();
}
/**
*创建一个缓存对象的标识: targetName.methodName.argument0.argument1...
*/
private 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();
}
/* (非 Javadoc)
* @see org.springframework.beans.factory.InitializingBeanafterPropertiesSet()
*/
public void afterPropertiesSet() throws Exception {
Assert.notNull(cache, "需要一个缓存. 使用setCache(Cache)分配一个.");
}
}

com.ph.serviceportal.infoboard.util.MethodCacheAfterAdvice











InitializingBean {
private Cache cache;
public void setCache(Cache cache) {
this.cache = cache;
}
/**
*
*/
public MethodCacheAfterAdvice() {
super();
}
/*
* (非 Javadoc)
*
* @see org.springframework.aop.AfterReturningAdviceafterReturning(java.lang.Object,
* java.lang.reflect.Method, java.lang.Object[], java.lang.Object)
*/
public void afterReturning(Object arg0, Method arg1, Object[] arg2,
Object arg3) throws Throwable {
StringBuffer buffer = new StringBuffer();
buffer.append(arg3.getClass().getName()).append(".").append(
arg1.getName());
if (arg2 != null && arg2.length != 0) {
for (int i = 0; i < arg2.length; i++) {
buffer.append(".").append(arg2[i]);
}
}
cache.remove(buffer);
}
/*
* (非 Javadoc)
*
* @see org.springframework.beans.factory.InitializingBeanafterPropertiesSet()
*/
public void afterPropertiesSet() throws Exception {
Assert.notNull(cache, "需要一个缓存. 使用setCache(Cache)分配一个.");
}
}

- 默认条件下,所有方法返回结果都被缓存了(methodNames 是 null)
- 缓存区利用 IoC 形成
- cacheKey 的生成还包括方法参数的因素(译注:参数的改变会影响 cacheKey)



























































































<完>
相关推荐
Mybatis-ehcache 1.2.1 是一个集成Mybatis和Ehcache的缓存模块,用于提高Mybatis框架的查询效率。Ehcache是一个广泛使用的Java缓存解决方案,它能够有效地存储和检索数据,减少数据库的负载,提高应用程序性能。在...
Ehcache 3 是一个广泛使用的开源Java缓存解决方案,特别是在需要高性能、低延迟的数据存储和检索场景下。Ehcache 3 提供了丰富的功能,包括本地内存缓存、磁盘持久化、多线程支持以及在分布式环境中实现集群共享缓存...
赠送jar包:ehcache-3.3.1.jar; 赠送原API文档:ehcache-3.3.1-javadoc.jar; 赠送源代码:ehcache-3.3.1-sources.jar; 赠送Maven依赖信息文件:ehcache-3.3.1.pom; 包含翻译后的API文档:ehcache-3.3.1-javadoc-...
Ehcache是一个高性能的、基于Java的进程内缓存解决方案,它被广泛应用于各种Java应用程序,包括Java EE和轻量级容器。Ehcache的主要优势在于它的快速响应、易用性和丰富的缓存策略。它提供了两种级别的缓存存储:...
赠送jar包:ehcache-3.9.9.jar; 赠送原API文档:ehcache-3.9.9-javadoc.jar; 赠送源代码:ehcache-3.9.9-sources.jar; 赠送Maven依赖信息文件:ehcache-3.9.9.pom; 包含翻译后的API文档:ehcache-3.9.9-javadoc-...
Ehcache是一个广泛使用的开源Java缓存库,它为应用程序提供了高效的内存管理和数据缓存功能。Ehcache的核心目标是提高应用性能,通过将频繁访问的数据存储在内存中,减少对数据库的依赖,从而降低系统负载。这次我们...
Ehcache是一个开源的Java缓存库,广泛用于提高应用程序性能,通过存储数据副本来减少对数据库的访问。它在Java社区中非常流行,因为它的灵活性、可扩展性和易用性。这里我们关注的是Ehcache的核心库,具体是三个不同...
赠送jar包:ehcache-3.9.9.jar; 赠送原API文档:ehcache-3.9.9-javadoc.jar; 赠送源代码:ehcache-3.9.9-sources.jar; 赠送Maven依赖信息文件:ehcache-3.9.9.pom; 包含翻译后的API文档:ehcache-3.9.9-javadoc-...
1.解压缩到目录下,复制ehcache-monitor-kit-1.0.0\lib\ehcache-probe-1.0.0.jar包到application的web-inf/lib目录下 2.将以下配置copy的ehcache.xml文件的ehcache标签中,注:上述链接中说的配置少写了个probe包名...
【标题解析】:“ehcache.xsd_ehcache.xml代码提示.rar”这个标题表明这是一个与Ehcache缓存系统相关的资源包,主要目的是为Ehcache的配置文件ehcache.xml提供代码提示功能。Ehcache是一个广泛使用的开源Java缓存...
EHcache是一款广泛使用的开源Java分布式缓存系统,主要设计用于提高应用程序的性能和可伸缩性。在Java应用程序中,特别是那些基于Hibernate或MyBatis的持久层框架中,EHcache作为二级缓存工具,能够显著提升数据访问...
Ehcache是一个开源的Java缓存库,广泛用于提高应用程序性能,通过存储经常访问的数据来减少数据库查询。在本文中,我们将深入探讨Ehcache 2.10.5版本的相关知识点。 首先,Ehcache的核心特性包括内存和磁盘缓存、...
Ehcache是一个开源的、高性能的缓存解决方案,广泛应用于Java应用程序中,以提高数据访问的速度和效率。本文将深入探讨Ehcache的简单监控,帮助开发者更好地理解其工作原理和性能状态。 首先,了解Ehcache的核心...
Ehcache是一款高效、流行的Java缓存库,它允许应用程序快速访问经常使用的数据,从而提高性能和响应速度。在分布式环境中,为了实现数据共享和高可用性,Ehcache提供了集群功能。而Jgroups则是Java中一个强大的集群...
赠送jar包:ehcache-core-2.6.11.jar; 赠送原API文档:ehcache-core-2.6.11-javadoc.jar; 赠送源代码:ehcache-core-2.6.11-sources.jar; 赠送Maven依赖信息文件:ehcache-core-2.6.11.pom; 包含翻译后的API文档...
Ehcache是一款流行的Java缓存解决方案,用于提升应用程序性能,减少数据库负载。它提供了一个高效、内存友好的机制来存储和检索数据。"ehcache-monitor-kit-1.0.3"是一个专门针对Ehcache的监控工具包,帮助开发者和...
### Spring Boot集成Ehcache实现缓存 #### 一、Ehcache简介 Ehcache是一个高效的纯Java进程内缓存框架,以其快速且轻便的特点而被广泛应用于各种应用场景中,尤其在Java EE和轻量级容器环境中更是受到青睐。...
ehcache是一种广泛使用的Java缓存框架,用于提高应用程序性能,特别是在数据访问操作中。通过将数据存储在内存中,ehcache能够显著减少数据库查询次数,从而加快应用响应速度。本文将深入探讨ehcache.xml配置文件中...
当我们谈论“Spring + Ehcache + Redis”两级缓存时,我们实际上是在讨论如何在Java环境中利用Spring框架来集成Ehcache作为本地缓存,并利用Redis作为分布式二级缓存,构建一个高效且可扩展的缓存解决方案。...
Ehcache是一个流行的Java缓存库,用于在应用程序中存储数据,以提高性能并减少对数据库的访问。它被广泛应用于各种系统,特别是在处理大量数据和需要快速响应时间的应用中。下面将详细介绍Ehcache的核心概念、配置...