gpt4 book ai didi

java - Ehcache-spring-annotations @Cacheable 没有捕获以 String 对象为参数的方法

转载 作者:行者123 更新时间:2023-11-29 09:41:17 24 4
gpt4 key购买 nike

我正在使用 ehcache-spring-annotations-1.2.0.jar 。这是我的 cache.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:context="http://www.springframework.org/schema/context"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">

<ehcache:annotation-driven />



<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"/>



</beans>

这是我的ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">

<cache name="messageCache" eternal="false"
maxElementsInMemory="5" overflowToDisk="false" diskPersistent="false"
timeToIdleSeconds="1000" timeToLiveSeconds="3000"
memoryStoreEvictionPolicy="LRU" />

这是我的主要代码

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.googlecode.ehcache.annotations.Cacheable;

public class EcacheSpringtest {
private static ApplicationContext cachecontext = new ClassPathXmlApplicationContext("cache.xml");

@Cacheable(cacheName = "messageCache")
String getName(String name) {

System.out.println(name+ " "+"has not been found in the cache so called getName()");
return "testObject";

}

public static void main(String[] args) {
long starttime;
long endtime;
long calldifferencesecs;

EcacheSpringtest test = new EcacheSpringtest();
test.getName("test");
try {
starttime = System.currentTimeMillis();
Thread.sleep(150);
endtime = System.currentTimeMillis();
calldifferencesecs = starttime - endtime ;
System.out.println("preparing to call getName with test as paramter after" +" "+ (~calldifferencesecs)+1 +"milliseconds");
} catch (InterruptedException e) {

}
test.getName("test"); // In this case testObject should be returned from cache but it is again calling getName().The method getName() is again called when same "test" String object is passed to method after 1491 milliseconds.

}
}

我引用了

http://ehcache.org/documentation/recipes/spring-annotations

https://code.google.com/p/ehcache-spring-annotations/wiki/UsingCacheable

EditFirst 实现下面由 kabochkov 给出的解决方案,我已经对其进行了测试。但我也无法缓存。

我不知道我哪里做错了。有什么帮助吗?

最佳答案

我终于解决了这个问题。我犯的错误是根据文档 https://code.google.com/p/ehcache-spring-annotations/wiki/UsingCacheable它被写成您可以将@Cacheable 注释放在接口(interface)的方法上,或类的公共(public)方法上。所以在我的方法不公开之前。所以我将其更改为公开并且有效。

关于java - Ehcache-spring-annotations @Cacheable 没有捕获以 String 对象为参数的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16582517/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com