简体   繁体   English

在 dropwizard 项目中使用 ehcache

[英]using ehcache in a dropwizard project

I am using dropwizard to create a RESTful service.我正在使用 dropwizard 创建一个 RESTful 服务。 To avoid hammering the database, I am looking for a good caching solution in java.为了避免重击数据库,我正在寻找一个很好的 java 缓存解决方案。 Searching the web lead me to ehcache.在网上搜索使我找到了 ehcache。 I have read the documentation a bit, but currently it is not clear to me how to use ehcache in a dropwizard project.我已经阅读了一些文档,但目前我不清楚如何在 dropwizard 项目中使用 ehcache。

For instance, where does the configuration file go?例如,配置文件去哪里了? I just need something to help me start using the cache.我只需要一些东西来帮助我开始使用缓存。

If this is difficult to integrate, what would be the most suited caching solution for dropwizard project?如果这很难集成,那么最适合 dropwizard 项目的缓存解决方案是什么?

Thanks!谢谢!

If you ultimately want a simpler (than ehcache) cache framework/API, consider using CacheBuidler from guava:如果您最终想要一个更简单(比 ehcache)的缓存框架/API,请考虑使用来自 guava 的 CacheBuidler:

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

A typical cache implementation and use requires just a few lines of code and no configuration files.典型的缓存实现和使用只需要几行代码,不需要配置文件。

I know this is an old question, but I have been able to set it up.我知道这是一个老问题,但我已经能够设置它。 Here's how.就是这样。

  1. Get the hibernate version.获取hibernate版本。 You can get this from the dependencies of dropwizard-hibernate package.您可以从dropwizard-hibernate包的依赖项中dropwizard-hibernate eg.例如。 dropwizard-hibernate:1.3.12 depends on hibernate-core:5.2.18.Final . dropwizard-hibernate:1.3.12取决于hibernate-core:5.2.18.Final
  2. Add ehcache matching the exact version of hibernate, so in this case compile group: 'org.hibernate', name: 'hibernate-ehcache', version: '5.2.18.Final'添加匹配hibernate确切版本的ehcache,所以在这种情况下compile group: 'org.hibernate', name: 'hibernate-ehcache', version: '5.2.18.Final'
  3. Enable 2nd level caching on hibernate.在休眠时启用二级缓存。 Dropwizard manages configuration in config.yml. Dropwizard 在 config.yml 中管理配置。 So this would be something like:所以这将是这样的:
# 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. Now, your cache should be enabled.现在,您的缓存应该已启用。 You can now use annotations on entities to make them cacheable.您现在可以在实体上使用注释以使其可缓存。 see doc on @org.hibernate.annotations.Cache请参阅@org.hibernate.annotations.Cache文档

I found this article to be a good start.我发现这篇文章是一个好的开始。 https://www.baeldung.com/hibernate-second-level-cache . https://www.baeldung.com/hibernate-second-level-cache There are also other types of caches like query cache etc which may help depending on your use case.还有其他类型的缓存,如查询缓存等,这可能会有所帮助,具体取决于您的用例。

Let me know if this helped.如果这有帮助,请告诉我。

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

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