2013-04-25 79 views
0

我想将Ehcache包含在托管在Tomcat中的Java Web应用程序中。我想要做的是检查我的缓存中是否有密钥,如果密钥存在,则检索它,如果不存在,则将其添加到缓存中供以后检索(就像memcached使用场景一样)。Ehcache with tomcat简单示例

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

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

好吗?

回答

4

这样做

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。还要检查弹簧与它的整合情况。

+0

我也用这个答案http://stackoverflow.com/questions/6206996/tomcat-java-servlet-initialize-class-on-application-startup初始化缓存。谢谢 – Serafeim 2013-04-25 10:19:09