【问题标题】:Ehcache with tomcat simple exampleehcache与tomcat的简单例子
【发布时间】:2013-04-19 03:37:30
【问题描述】:

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

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

好的?

【问题讨论】:

    标签: java jakarta-ee tomcat caching ehcache


    【解决方案1】:

    做类似的事情

    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 与它的集成。

    【讨论】:

    猜你喜欢
    • 2016-08-25
    • 2014-09-01
    • 1970-01-01
    • 2016-10-08
    • 2013-03-22
    • 2014-04-18
    • 2017-08-16
    • 2016-10-28
    • 1970-01-01
    相关资源
    最近更新 更多