开发者

Hiber cache : Cache all fixed data permanently

开发者 https://www.devze.com 2023-01-19 05:36 出处:网络
I have few tables like Country,state city which has static data. User do开发者_JS百科 not enter any data in this data. I create pojo for Country, State, City. There are few pojo which has mapping with

I have few tables like Country,state city which has static data. User do开发者_JS百科 not enter any data in this data. I create pojo for Country, State, City. There are few pojo which has mapping with static data. My requirement is that whenever any request comes to Hibernate for Country (21), it do not hit database but return data from cache . Is this possible in Hibernate. I need few pointers and your views to implement caching in my project. I am using hibernate annotations.


My requirement is that whenever any request comes to Hibernate for Country (21), it do not hit database but return data from cache. Is this possible in Hibernate.

Yes, this is possible using the Second Level Cache and this kind of Entities (read-only) are the perfect candidates for caching (they are the easiest to manage). You'll need to:

  • enable the 2nd level cache
    • set the hibernate.cache.use_second_level_cache property to true in your configuration
  • choose a 2nd level cache provider (I suggest EHCache)
    • set the hibernate.cache.provider_class property accordingly
  • mark your entities as cacheable (using the read-only strategy)
    • Add @Cache(usage = CacheConcurrencyStrategy.READ_ONLY) to your entities

References

  • Hibernate Core Reference Guide
    • 3.4.4. Second-level and query cache
    • 19.2. The Second Level Cache
  • Hibernate Annotations 3.4 Reference Guide
    • 2.4.8. Cache

More Resources

  • Hibernate: Truly Understanding the Second-Level and Query Caches
0

精彩评论

暂无评论...
验证码 换一张
取 消