views:

123

answers:

1

1.for 2nd level cache, can only set timeout period but cannot force refresh/clear cache of entity? or putthing annotation @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) like auto refresh/clear the cache each time doing saveorupdate/mergeupdate? what is hibernateTemplate.flush() relate to this?


2. is it good to enable 2nd level cache for all entity ? what is the average timeout that you folks use for ehcache for this case if i do not want too long caching time as i cache all entities?

+1  A: 

1) You can manually evict entities from the 2nd level cache if you have to. SessionFactory has several methods for this purpose; some evict single instance of an entity (or collection); others evict all entities of given class (or given entity name / role name).

That said, you normally shouldn't have to do this - Hibernate would keep the cache updated for you (unless you're doing some rather specific stuff like SQL updates - in which case do clarify your question).

2) Generally - no, it's not good to cache all entities. It's usually a good idea to cache immutable entities as well as those that are frequently loaded / rarely updated; assuming you don't have tons of them. But again, it really depends on what you're doing.

Timeout is a crutch (or a safeguard if you will) intended to keep your cache size / state reasonable even if you don't. Good caching strategy should not rely on it.

ChssPly76