简体   繁体   中英

How to use Hibernate to cache table data and speed-up queries?

I am using Hibernate 4, JSF, and Spring in my project.

I have tables like: ValidStatus , Valid Group , ValidAccess , in which I already populated some default data.

I have to put all these tables data in the Cache so that, anytime in different pages, I can use these table for authorizing users.

How can we cache these table data? Do I have to use the Second-Level Cache for this scenario or something else will work as well?

The first level cache is only valid for as long as the current running Session (aka EntityManager ) is open.

Two concurrent transactions will run in different Session (s), hence each one will have it's own 1st Level Cache, which is not what you aim for.

You want the 2nd Level Cache , which is shared between concurrent Session (s):

  • EhCache is a very well-known 2nd Level Cache implementation,
  • Infinispan which is developed by Red Hat.

Hibernate has default cahching capability.

Hibernate first level cache is Session level cache, so if the object is currently in the Hibernate session results will be fetched from it.

Therefore, yoou do not need to cache fetched results, it is already cached by hibernate.

See also

https://stackoverflow.com/a/20369520/892994

Hibernate Caching

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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