java Ehcache 与 tomcat 简单示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16211420/
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 22:14:11  来源:igfitidea点击:

Ehcache with tomcat simple example

javajakarta-eetomcatcachingehcache

提问by Serafeim

I want to include Ehcache in a Java web application hosted in Tomcat. What I want to do is check my cache for a key and if the key exists then retrieve it, if not then add it to the cache for later retrieval (just like the memcached usage scenario).

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

I searched the documentation and couldn't find useful information on how to implement this simple example. I only found out that I need to put ehcache-core.jar and slf4j*.jar in my classpath along with ehcache.xml. Then what ? I can see an Ehcache cache object in the examples - but where should I instantiate that in order to be accessible from my servlets / JSPs ? Also, can you recommend a very simple cache configuration to put in ehcache.xml ? Is the default

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

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

ok ?

好的 ?

回答by Bhushan Bhangale

Do something like

做类似的事情

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();

There are better elegant ways to play with cache. Refer to http://ehcache.org/documentation/code-samples. Also check spring integration with it.

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