Ser*_*eim 0 java tomcat caching ehcache java-ee
我想将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>
好 ?
做点什么
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();
Run Code Online (Sandbox Code Playgroud)
有更好的优雅方式来缓存.请参阅http://ehcache.org/documentation/code-samples.还要检查弹簧与它的集成.