繁体   English   中英

在 dropwizard 项目中使用 ehcache

[英]using ehcache in a dropwizard project

我正在使用 dropwizard 创建一个 RESTful 服务。 为了避免重击数据库,我正在寻找一个很好的 java 缓存解决方案。 在网上搜索使我找到了 ehcache。 我已经阅读了一些文档,但目前我不清楚如何在 dropwizard 项目中使用 ehcache。

例如,配置文件去哪里了? 我只需要一些东西来帮助我开始使用缓存。

如果这很难集成,那么最适合 dropwizard 项目的缓存解决方案是什么?

谢谢!

如果您最终想要一个更简单(比 ehcache)的缓存框架/API,请考虑使用来自 guava 的 CacheBuidler:

https://code.google.com/p/guava-libraries/wiki/CachesExplained

典型的缓存实现和使用只需要几行代码,不需要配置文件。

我知道这是一个老问题,但我已经能够设置它。 就是这样。

  1. 获取hibernate版本。 您可以从dropwizard-hibernate包的依赖项中dropwizard-hibernate 例如。 dropwizard-hibernate:1.3.12取决于hibernate-core:5.2.18.Final
  2. 添加匹配hibernate确切版本的ehcache,所以在这种情况下compile group: 'org.hibernate', name: 'hibernate-ehcache', version: '5.2.18.Final'
  3. 在休眠时启用二级缓存。 Dropwizard 在 config.yml 中管理配置。 所以这将是这样的:
# For documentation on available options please refer io.dropwizard.db.DataSourceFactory
database:
  # any properties specific to your JDBC driver:
  properties:
    # Enable second level cache with EhCache
    hibernate.cache.use_second_level_cache: true
    hibernate.cache.region.factory_class: org.hibernate.cache.ehcache.EhCacheRegionFactory
  1. 现在,您的缓存应该已启用。 您现在可以在实体上使用注释以使其可缓存。 请参阅@org.hibernate.annotations.Cache文档

我发现这篇文章是一个好的开始。 https://www.baeldung.com/hibernate-second-level-cache 还有其他类型的缓存,如查询缓存等,这可能会有所帮助,具体取决于您的用例。

如果这有帮助,请告诉我。

暂无
暂无

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

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