简体   繁体   English

带有Tomcat的ehcache简单示例

[英]Ehcache with tomcat simple example

I want to include Ehcache in a Java web application hosted in Tomcat. 我想将Ehcache包含在Tomcat托管的Java Web应用程序中。 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). 我要做的是检查我的缓存中是否有密钥,如果该密钥存在,则将其检索,如果不存在,则将其添加到缓存中以供以后检索(就像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. 我只发现需要将ehcache-core.jar和slf4j * .jar与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 ? 我可以在示例中看到一个Ehcache缓存对象-但是我应该在哪里实例化该实例,以便可以从我的servlet / JSP访问它? Also, can you recommend a very simple cache configuration to put in ehcache.xml ? 另外,您是否可以建议将一个非常简单的缓存配置放入ehcache.xml中? Is the default 是默认值

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

ok ? 好 ?

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 . 请参阅http://ehcache.org/documentation/code-samples Also check spring integration with it. 还要检查与之集成的弹簧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM