java - 使用哪个 Ehcache 导入?

标签 java caching ehcache

我刚刚开始使用 Ehcache,并尝试在 JAX-RS 框架中缓存方法调用的结果。有人可以告诉我我的类(class)导入应该是什么吗?由于某种原因,我似乎无法在我读过的(非常令人困惑的)示例中找到这些行。我也很感激 Ehcache 中 Java 方法缓存的任何链接...我发现的所有内容似乎都在尝试做非常复杂的事情!

import org.ehcache.Cache;
import org.ehcache.CacheManager;

/**
 *
 * @author king
 */
public class CacheTest {
CacheManager cacheMgr = CacheManager.newInstance();

//EJB?Stateless?
HelloService hello;


public Object getCache(){
    //Initialise a cache if it does not already exist
    if (cacheMgr.getCache("MyCache") == null) {
        cacheMgr.addCache("MyCache");
    }
    Cache cache = cacheMgr.getCache("MyCache");

    String s=hello.getUserInfo(103);
    //Store an element
    cache.put(new Element("103", s));

    //Retrieve an element
    Element el = cache.get("key");
    Serializable myObj = <Serializable>el.getObjectValue();
    return myObj;
}

}

ehcache.xml(在资源文件夹中)

<ehcache>
    <diskStore path="java.io.tmpdir"/>
    <cache name="MyCache"
       maxEntriesLocalHeap="10000"
       eternal="false"
       timeToIdleSeconds="120"
       timeToLiveSeconds="120"
       maxEntriesLocalDisk="10000000"
       diskExpiryThreadIntervalSeconds="120"
       memoryStoreEvictionPolicy="LRU"
        >
       <persistence strategy="localTempSwap"/>
    </cache>
</ehcache>

最佳答案

您似乎在使用 ehcache2 (net.sf.ehcache) 配置文件,而在代码中您使用的是 ehcache3 (org.ehcache)

使用 ehcache3 兼容的 xml 文件重试(您可以在 ehcache3 official websitethe peeper example 甚至 this little project I setup the other day 上找到灵感)

关于java - 使用哪个 Ehcache 导入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45723917/

相关文章:

Spring @Cacheable 不缓存

java - 如何使用 mockito 测试 ehcache?

java - 为什么我们可以在类加载之前使用new运算符

java - 如何获得当前日期之前的最后一个星期日?

当我添加包含整数的对象时,Java ArrayList 保持为空

android - OkHttp 客户端的最佳/不易出错的缓存大小是多少(与 exoplayer 一起使用)

java - 将 .class 转换为 .java

html - 在 <audio> 标签中强制缓存 MP3/OGG

c# - 网络核心 : should I use AddScoped or AddSingleton on a cache service

Spring:多个缓存管理器