ehcache 或 Spring MVC 的 Spring 缓存中的最佳缓存实践是啥?

Posted

技术标签:

【中文标题】ehcache 或 Spring MVC 的 Spring 缓存中的最佳缓存实践是啥?【英文标题】: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吗? 没有我想知道这两个是怎么支持的,还是需要同时使用来实现缓存? 【参考方案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

【讨论】:

以上是关于ehcache 或 Spring MVC 的 Spring 缓存中的最佳缓存实践是啥?的主要内容,如果未能解决你的问题,请参考以下文章

Java十大常用框架介绍(spring系+dubbo+RabbitMQ+Ehcache+redis)

spring boot和mvc区别

精品源码Spring MVC+Mybatis+Shiro+Bootstrap仓库管理系统

第一个使用Spring Tool Suite(STS)和Maven建立的Spring mvc 项目

Ehcache学习ehcache与spring注解实例

Spring MVC处理流程总结