【问题标题】:What are the best Cache practices in ehcache or spring cache for spring MVC?ehcache 或 Spring MVC 的 Spring 缓存中的最佳缓存实践是什么?
【发布时间】:2017-01-13 00:40:27
【问题描述】:

计划在基于 Spring Web 的应用程序中实现静态数据缓存机制,谁能解释一下哪个是最好的以及它是如何工作的?

  • EhCache
  • Spring 缓存

【问题讨论】:

  • 你想比较一下ehcache和spring cache吗?
  • 没有我想知道这两个是怎么支持的,还是需要同时使用来实现缓存?

标签: java spring spring-mvc ehcache spring-cache


【解决方案1】:

免责声明:我是Terracotta / Software AG 员工,Ehcache 的维护者

Ehcache 是一个 JVM 缓存库,以用作 Hibernate ORM 的默认二级缓存而闻名

Spring cache 在 Spring 3.1 中引入,并带来了 @CachePut 等注解来定义 Spring 应用程序中缓存的使用;默认情况下 Spring 缓存使用普通 Map,但您可以将其配置为使用任何流行的缓存框架,包括 Ehcache

自 Spring 4.1 起,Spring 缓存支持JSR-107,这是 JVM 上缓存的标准。

这意味着,您可以添加 JSR-107 缓存注释,然后选择您的缓存库 (ehcache 2 or 3 / guava / caffeine / etc.):您不受任何缓存的约束供应商,甚至不绑定到 Spring 缓存注释!

this nice blog post explaining the parallel between Spring cache annotations and JSR-107 annotations,如果您选择在Spring Boot 应用程序中使用Ehcache3,there's another interesting blog post explaining you how to integrate it in your app

【讨论】:

  • JSR 注释的一个怪癖是没有针对雷鸣般的羊群问题的保护。该功能需要使用加载器,以便不调用 CacheResult 方法主体。它变得非常丑陋。
  • 非常感谢安东尼,我怀疑我是否需要用户 en-cache 和 spring 缓存?
  • @DelliKilari,我建议您在您的应用程序中启用 Spring 缓存,在您的代码中使用 Jcache / jsr107 注释并配置 ehcache3 或任何其他 jsr107 实现作为您的缓存实现(请参阅我的答案中的链接)
  • 非常感谢您抽出宝贵时间回答我的疑问。
  • @AnthonyDahanne : 在使用 hibernate 二级缓存实现 ehcache 时,您能否提供最佳和快速的 ehcache.xml 默认缓存配置?
【解决方案2】:

Ehcache3 支持 JSR 107

对于 Ehcache2 可能你可以使用这个实现

https://github.com/ehcache/ehcache-jcache/tree/master/ehcache-jcache

【讨论】:

    猜你喜欢
    • 2015-04-19
    • 1970-01-01
    • 1970-01-01
    • 2010-12-18
    • 2012-12-12
    • 2010-10-31
    • 2015-05-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多