Spring AOP配置了ehcache缓存后没起作用,咋回事儿

zhutianxiang 2010-08-26 11:12:29

<ehcache>

<diskStore path="c:\\myapp\\cache" />

<defaultCache
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true" />

<cache
name="DEFAULT_CACHE"
maxElementsInMemory="100"
eternal="false"
timeToIdleSeconds="3000"
timeToLiveSeconds="6000"
overflowToDisk="true" />

</ehcache>


<!-- 引用ehCache的配置 -->
<bean id="defaultCacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation">
<value>classpath:ehcache.xml</value>
</property>
</bean>

<!-- 定义ehCache的工厂,并设置所使用的Cache name -->
<bean id="ehCache"
class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<property name="cacheManager">
<ref local="defaultCacheManager" />
</property>
<property name="cacheName">
<value>DEFAULT_CACHE</value>
</property>
</bean>

<!-- find/create cache拦截器 -->
<bean id="methodCacheInterceptor"
class="com.htdz.eCard.common.interceptor.MethodCacheInterceptor">
<property name="cache">
<ref local="ehCache" />
</property>
</bean>
<!-- flush cache拦截器 -->
<bean id="methodCacheAfterAdvice"
class="com.htdz.eCard.common.interceptor.MethodCacheAfterAdvice">
<property name="cache">
<ref local="ehCache" />
</property>
</bean>

<bean id="methodCachePointCut"
class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice">
<ref local="methodCacheInterceptor" />
</property>
<property name="patterns">
<list>
<value>find.*</value>
<value>get.*</value>
<value>list.*</value>
</list>
</property>
</bean>
<bean id="methodCachePointCutAdvice"
class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice">
<ref local="methodCacheAfterAdvice" />
</property>
<property name="patterns">
<list>
<value>add.*</value>
<value>create.*</value>
<value>insert.*</value>
<value>remove.*</value>
<value>delete.*</value>
<value>change.*</value>
<value>edit.*</value>
<value>update.*</value>
</list>
</property>
</bean>

<bean id="test"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<ref bean="orgAdminService" />
</property>
<property name="interceptorNames">
<list>
<value>methodCachePointCut</value>
<value>methodCachePointCutAdvice</value>
</list>
</property>
</bean>


/**
* 用来拦截Service的方法调用,
* 拦截到方法后,搜索该方法的结果在cache中是否存在,
* 如果存在,返回cache中的缓存结果,
* 如果不存在,返回查询数据库的结果,并将结果缓存到cache中。
* @author ztx
*
*/
public class MethodCacheInterceptor implements MethodInterceptor,
InitializingBean {

private static final Log logger = LogFactory
.getLog(MethodCacheInterceptor.class);

private Cache cache;

public void setCache(Cache cache) {
this.cache = cache;
}

public MethodCacheInterceptor() {
super();
}

/**
* 拦截Service的方法,并查找该结果是否存在,如果存在就返回cache中的值, 否则,返回数据库查询结果,并将查询结果放入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();
}

/**
* 获得cache key的方法,cache key是Cache中一个Element的唯一标识 cache key包括
* 包名+类名+方法名,如com.co.cache.service.UserServiceImpl.getAllUser
*/
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,检查cache是否为空
*/
public void afterPropertiesSet() throws Exception {
Assert.notNull(cache,
"Need a cache. Please use setCache(Cache) create it.");
}

}



public class MethodCacheAfterAdvice implements AfterReturningAdvice,
InitializingBean {

private static final Log logger = LogFactory
.getLog(MethodCacheAfterAdvice.class);

private Cache cache;

public void setCache(Cache cache) {
this.cache = cache;
}

public MethodCacheAfterAdvice() {
super();
}

public void afterReturning(Object arg0, Method arg1, Object[] arg2,
Object arg3) throws Throwable {
String className = arg3.getClass().getName();
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.");
}

}



/**
* 查询用户
*
* @author hyc 2010-08-05
* @return List<Oper> 用户对象集合
*/
public List<Oper> listOpers(Long orgID, Long deptID) {
System.out.println("---OrgAdminService:Cache内不存在该element,查找并放入Cache!");
return orgAdminDao.listOpers(orgID, deptID);
}


我多次查询,每次控制台都输出cache内不存在该element,查找并放入Cache...

哪里错了吗,配置正常加载了呀。
...全文
103 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhutianxiang 2010-08-26
  • 打赏
  • 举报
回复
orgAdminService这个bean配置在另外个配置文件里,我这里就没写出来了。反正启动都正常。

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧