What's difference between hibernate caching an

2020-06-17 05:26发布

问题:

I have an application where I get the data from database (using hibernate). I want to load the cache (per user) with database state once per day and use the cache instead.

I'm using Spring framework with Hibernate and I know that both of them have some caching possibilities.

What are the differences between them? What would be a better choice?

回答1:

Starting with Hibernate cache is a more prudent decision in my view, especially because the two don't exclude each other. In terms of performance Spring cache can offer you more, its much higher in the stack, you can cache business results (basically more than you can with hibernate's second level cache).

However one outstanding distinction is clearing the cache. With Spring cache you need to clear explicitly, while the hibernate cache is maintained automatically if your inserts, updates and deletions go through hibernate's framework.

In the context of the projects that I'm involved, using hibernate's second level cache is assumed, a default almost. Spring cache we use for the data that are much more static in nature.



回答2:

Hibernate has 2 levels of cache. 1. First level 2. Second level

Second level chache is for the whole application and first level cache is for the current hibernate session. There is no cache available per user basis.

Also if you are using spring managed beans + caching. There is a provision for you to cache the beans for entire application rather than for a user. (little uncertain on this)

It sounds to me that you want to store the data in the user's session. ie., as long as the user is having an active session. Spring has a session scoped beans you may want to use these to cache the data per user.