Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hibernate cache mode - put vs refresh

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34671
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JavaDoc for CacheMode.PUT and CacheMode.REFRESH have the same first sentence "The session will never read items from the cache, but will add items to the cache as it reads them from the database."

REFRESH adds "In this mode, the effect of hibernate.cache.use_minimal_puts is bypassed, in order to force a cache refresh ". Presumably this sentence is the difference between the two.

I'm not sure I understand the difference. Is it that REFRESH forces a cache update and PUT only does it sometimes? (I do have a book, but it describes it in essentially the same way and I don't follow that either.)
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On a serious note.

I think it makes a little more sense in thinking in terms of using CacheMode on a query by query use case. In some cases you don't need to check the cache for data, you might be just adding stuff to the cache. Like lets say at startup we load the 50 states into our cache. Either PUT or REFRESH will load all 50 states into the cache. We don't need to read from the cache here, we are just loading into cache for our app to use later. In those later queries to get the states to present to the user, we don't need either PUT or REFRESH because we know the entire 50 states are already in the cache.

OK, now lets say we have some task that runs once a day, and it is to update some data that we put into the cache, that changes a little bit every 24 hours, and now we want to update that data that is now stale in the cache. Do we want to flush and refresh the entire cache region for this data or just update the things that changed. This I believe is the distinction between PUT and REFRESH. Put will just update the things that are changed, and refresh will clear out the entire cache region and reload everything. In the reload/REFRESH scenario is probably a bit faster if there is a lot of difference, and therefore doesn't have code that compares the new loaded data versus what is in the cache, which I think PUT would do.

Sounds like it now makes sense to me, but I can't guarantee it's accuracy. ;)

Mark
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34671
367
Eclipse IDE Java VI Editor
 
Brendon Woodford
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
As Mark has written
I think it makes a little more sense in thinking in terms of using CacheMode on a query by query use case. In some cases you don't need to check the cache for data, you might be just adding stuff to the cache.

This does make sense.
But my question is if the CacheMode.GET signifies :The session may read items from the cache, but will not add items, except to invalidate items when updates occur ;how on earth is it possible to have the objects in the cache in the first place if the session does not allow the data to be added to the Second level cache?

The same question pertains to CacheMode.PUT : The session will never read items from the cache, but will add items to the cache as it reads them from the database.. If the session does not allow items to be read from the cache then why would anybody read them to the second level cache in the first place?
Your response is appreciated.
Thank you all
Brendon
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic