Hiber cache : Cache all fixed data permanently
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 totrue
in your configuration
- set the
- choose a 2nd level cache provider (I suggest EHCache)
- set the
hibernate.cache.provider_class
property accordingly
- set the
- mark your entities as cacheable (using the
read-only
strategy)- Add
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
to your entities
- Add
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
精彩评论