簡體   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