java Ehcache-spring-annotations @Cacheable 不捕获以字符串对象为参数的方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16582517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:21:54  来源:igfitidea点击:

Ehcache-spring-annotations @Cacheable not catching method with String object as parameter

javaspringehcache

提问by abishkar bhattarai

I am using ehcache-spring-annotations-1.2.0.jar .This is my cache.xml file

我正在使用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>

This is my ehcache.xml

这是我的 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" />

This is my main code

这是我的主要代码

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.

    }
}

I have taken reference from

我参考了

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

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

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

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

EditFirstImplementing the solution given below by kabochkov I have tested it .But also i am unable to cache.

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

I could not figure out where i have done mistake.Any help please?

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

回答by abishkar bhattarai

I finally solved the problem.What mistake i was doing was that according to documentation https://code.google.com/p/ehcache-spring-annotations/wiki/UsingCacheableIt was written You can place the @Cacheable annotation on a method on an interface, or a public method on a class. So before my method was not public .So i changed it to public and it worked.

我终于解决了这个问题。我做的错误是根据文档 https://code.google.com/p/ehcache-spring-annotations/wiki/UsingCacheable写的 你可以将@Cacheable 注释放在一个方法上在接口上,或在类上的公共方法上。所以在我的方法不是公开的之前。所以我把它改成了公开的并且它起作用了。

回答by kabochkov

If you create object directly, spring can't handle @Cacheable annotation.

如果直接创建对象,spring 无法处理@Cacheable 注解。

EcacheSpringtest test = new EcacheSpringtest();

Use dependency injection and it will work!

使用依赖注入,它会起作用!

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:cache.xml"})
public abstract class TestEcacheSpring {

  @Autowired
  EcacheSpringtest test;

  @Test
  public void test(){
    test.getName("test");
    test.getName("test"); 
  }

}

EcacheSpringtest shoul'd be marked as @Service

EcacheSpringtest 应该被标记为@Service

@Service
public class EcacheSpringtest {

@Cacheable(cacheName = "messageCache")
String getName(String name) {
 System.out.println(name+ " "+"has not been found in the cache so called getName()");
 return "testObject";
}

}

回答by Anuj Patel

One thing I doubt here is that there is no reference at all to your ehcache.xml in your context.

我在这里怀疑的一件事是,在您的上下文中根本没有对您的 ehcache.xml 的引用。

I have used ehcache in production with following configuration in context.xmland it works perfectly.

我已经在生产中使用了 ehcache 并具有以下配置,context.xml并且它运行良好。

Could you try after adding following to context (Note: You'll have to remove <ehcache:annotation-driven />and also add spring-cache xmlns to context)

您可以在将以下内容添加到上下文后尝试吗(注意:您必须删除 <ehcache:annotation-driven />并将 spring-cache xmlns 添加到上下文中)

<cache:annotation-driven cache-manager="cacheManager" mode="proxy" proxy-target-class="true" />
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:config-location="classpath:ehcache.xml" p:shared="true" />
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cacheManager-ref="ehcache" />

Add
xmlns:cache="http://www.springframework.org/schema/cache"

添加
xmlns:cache="http://www.springframework.org/schema/cache"

xsi:http://www.springframework.org/schema/cachehttp://www.springframework.org/schema/cache/spring-cache.xsd

xsi:http: //www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd