spring ehcache不起作用,在线等!

fireelf000 2009-11-19 11:35:52
使用了spring的ehcache配置缓存,在自定义的spring拦截器中缓存方法返回结果,但是缓存没有作用,每次从缓存中通过key来获取到的Element都是null,求教!
这是spring中的配置

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation">
<value>classpath:ehcache.xml</value>
</property>
</bean>

<bean id="user_cache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<property name="cacheManager">
<ref bean="cacheManager"/>
</property>
<property name="cacheName">
<value>user_cache</value>
</property>
</bean>

<bean id="myInterceptor" class="com.interceptor.MyInterceptor">
<property name="cache">
<ref bean="user_cache"/>
</property>
</bean>

<bean id="methodCachePointCut" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice">
<ref bean="myInterceptor"/>
</property>
<property name="patterns">
<list>
<value>.*find.*</value>
</list>
</property>
</bean>

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

<bean id="test" class="com.interceptor.Test"/>


这是拦截器

public class MyInterceptor implements MethodInterceptor {

private Cache cache;

public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("----------进入拦截器----------");
String targetName = invocation.getThis().getClass().getName();
String methodName = invocation.getMethod().getName();
Object []arguments = invocation.getArguments();
Object result;

String cacheKey = this.getCacheKey(targetName, methodName, arguments);

Element element = cache.get(cacheKey);

System.out.println(cacheKey);
System.out.println(element);

if (null == element) {
result = invocation.proceed();
element = new Element(cacheKey,(Serializable)result);
cache.put(element);
}
System.out.println("----------退出拦截器----------");
return element.getValue();
}

private String getCacheKey(String targetName,String methodName,Object[] arguments) {
StringBuilder result = new StringBuilder();
result.append(targetName).append(".").append(methodName);
if (null != arguments && 0 != arguments.length) {
for (int i = 0; i < arguments.length; i++) {
result.append(".").append(arguments[i]);
}
}
return result.toString();
};

public Cache getCache() {
return cache;
}

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


求高手解决。。。在线急等,还等着ehcache和acegi做整合呢
...全文
492 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
bgbug 2009-11-19
  • 打赏
  • 举报
回复
因为servlet里是每次请求会从Application 新加载,所会每次都会创建一个bean,所以你EHcatche对象都是一个新的。所以你ehcatch是拿不到值。
zl3450341 2009-11-19
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 fireelf000 的回复:]
解决了,原来测试的时候是从一个servlet里通过Application的getBean获取到代理bean的,这样做每次调用的时候都会创建一个新的cache,后来改成把代理bean注入一个action中就没问题了
[/Quote]
刚来就解决啦
很久以前飞 2009-11-19
  • 打赏
  • 举报
回复
不清楚 学习学习
panhaichun 2009-11-19
  • 打赏
  • 举报
回复
哦,楼主自己解决的太快了,我刚才还没看到呢
panhaichun 2009-11-19
  • 打赏
  • 举报
回复
你的cacheKey 跟传入的参数有关。每次传入的参数都一样吗?



参数都是些什么类型,

result.append(".").append(arguments[i]);
有时候object toString之后得到的是一个表示内存地址的字符串哦。
道光2008 2009-11-19
  • 打赏
  • 举报
回复
接分
fireelf000 2009-11-19
  • 打赏
  • 举报
回复
解决了,原来测试的时候是从一个servlet里通过Application的getBean获取到代理bean的,这样做每次调用的时候都会创建一个新的cache,后来改成把代理bean注入一个action中就没问题了
  • 打赏
  • 举报
回复
帮顶
fireelf000 2009-11-19
  • 打赏
  • 举报
回复
这是ehcache.xml


<ehcache>
<diskStore path="java.io.tmpdir"/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="0"
timeToLiveSeconds="0"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"/>

<cache name="user_cache"
maxElementsInMemory="10000"
eternal="true"
overflowToDisk="true"/>
</ehcache>
道光2008 2009-11-19
  • 打赏
  • 举报
回复
上面的程序看不出问题
ehcache.xml配置文件看看,
qustgjk 2009-11-19
  • 打赏
  • 举报
回复
不清楚,帮你顶一个

81,094

社区成员

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

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