首页 文章

Ehcache用tomcat简单的例子

提问于
浏览
0

我想将Ehcache包含在Tomcat中托管的Java Web应用程序中 . 我想要做的是检查我的缓存中的密钥,如果密钥存在则检索它,如果没有,则将其添加到缓存中以便以后检索(就像memcached使用场景一样) .

我搜索了文档,找不到有关如何实现这个简单示例的有用信息 . 我发现我需要将ehcache-core.jar和slf4j * .jar与ehcache.xml放在我的类路径中 . 那又怎样?我可以在示例中看到一个Ehcache缓存对象 - 但是我应该在哪里实例化它以便可以从我的servlet / JSP访问?另外,你能推荐一个非常简单的缓存配置放在ehcache.xml中吗?是默认值

<defaultCache
           maxEntriesLocalHeap="0"
           eternal="false"
           timeToIdleSeconds="1200"
           timeToLiveSeconds="1200">
    </defaultCache>

好 ?

1 回答

  • 5

    做点什么

    CacheManager.getInstance().addCache("xyz"); // creates a cache called xyz.
    
    Cache xyz = CacheManager.getInstance().getCache("xyz");
    
    xyz.put(new Element("key", new Person()));
    Element e = xyz.get("key");
    Person p = (Person) e.getObjectValue();
    

    有更好的优雅方式来缓存 . 请参阅http://ehcache.org/documentation/code-samples . 还要检查 spring 与它的集成 .

相关问题