2nd level hibernate cache understanding
**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?
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.
精彩评论