If you have queries that run over and over, with the same parameters, query caching provides performance gains.
Caching introduces overhead in the area of transactional processing. For example, if you cache results of a query against an object, Hibernate needs to keep track of whether any changes have been committed against the object, and invalidate the cache accordingly. In addition, the benefit from caching query results is limited, and highly dependent on the usage patterns of your application. For these reasons, Hibernate disables the query cache by default.
The query cache does not cache the state of the actual entities in the cache. It caches identifier values and results of value type. Therefore, always use the query cache in conjunction with the second-level cache for those entities which should be cached as part of a query result cache.
Caching introduces overhead in the area of transactional processing. For example, if you cache results of a query against an object, Hibernate needs to keep track of whether any changes have been committed against the object, and invalidate the cache accordingly. In addition, the benefit from caching query results is limited, and highly dependent on the usage patterns of your application. For these reasons, Hibernate disables the query cache by default.
The query cache does not cache the state of the actual entities in the cache. It caches identifier values and results of value type. Therefore, always use the query cache in conjunction with the second-level cache for those entities which should be cached as part of a query result cache.
For fine-grained control over query cache expiration policies, specify a named cache region for a particular query by calling
To force the query cache to refresh one of its regions and disregard any cached results in the region, call
Query.setCacheRegion().To force the query cache to refresh one of its regions and disregard any cached results in the region, call
org.hibernate.Query.setCacheMode(CacheMode.REFRESH). In conjunction with the region defined for the given query, Hibernate selectively refreshes the results cached in that particular region. This is much more efficient than bulk eviction of the region via org.hibernate.SessionFactory.evictQueries().
Hibernate is compatible with several second-level cache providers. None of the providers support all of Hibernate's possible caching strategies. Section 6.2.3, “Second-level cache providers for Hibernate” lists the providers, along with their interfaces and supported caching strategies. For definitions of caching strategies, see Section 6.2.2, “Caching strategies”.
You can configure your cache providers using either annotations or mapping files.
Set the global default cache concurrency strategy The cache concurrency strategy with thehibernate.cache.default_cache_concurrency_strategy configuration property. See Section 6.2.2, “Caching strategies” for possible values.
Instead of
Entities
By default, entities are not part of the second-level cache, and their use is not recommended. If you absolutely must use entities, set the
shared-cache-mode element in persistence.xml, or use propertyjavax.persistence.sharedCache.mode in your configuration. Use one of the values in Table 6.1, “Possible values for Shared Cache Mode”.Set the global default cache concurrency strategy The cache concurrency strategy with thehibernate.cache.default_cache_concurrency_strategy configuration property. See Section 6.2.2, “Caching strategies” for possible values.
Note
When possible, define the cache concurrency strategy per entity rather than globally. Use the
@org.hibernate.annotations.Cache annotation.
Example 6.3. Configuring cache providers using mapping files
<cache usage="transactional" region="RegionName" include="all" />Just as in the Example 6.2, “Configuring cache providers using annotations”, you can provide attributes in the mapping file. There are some specific differences in the syntax for the attributes in a mapping file.
- usage
- The caching strategy. This attribute is required, and can be any of the following values.
- transactional
- read-write
- nonstrict-read-write
- read-only
- region
- The name of the second-level cache region. This optional attribute defaults to the class or collection role name.
- include
- Whether properties of the entity mapped with
lazy=truecan be cached when attribute-level lazy fetching is enabled. Defaults toalland can also benon-lazy.
<cache>, you can use <class-cache> and <collection-cache> elements in hibernate.cfg.xml.| Cache | Interface | Supported strategies | ||
|---|---|---|---|---|
| HashTable (testing only) |
| |||
| EHCache |
| |||
| OSCache |
| |||
| SwarmCache |
| |||
| JBoss Cache 1.x |
| |||
| JBoss Cache 2.x |
|
Syncing or removing a cached item
Determining whether an item belongs to the Session cache
The Session provides a
contains() method to determine if an instance belongs to the session cache.
Example 6.5. Second-level cache eviction
You can evict the cached state of an instance, entire class, collection instance or entire collection role, using methods of
SessionFactory.sessionFactory.getCache().containsEntity(Cat.class, catId); // is this particular Cat currently in the cache sessionFactory.getCache().evictEntity(Cat.class, catId); // evict a particular Cat sessionFactory.getCache().evictEntityRegion(Cat.class); // evict all Cats sessionFactory.getCache().evictEntityRegions(); // evict all entity data sessionFactory.getCache().containsCollection("Cat.kittens", catId); // is this particular collection currently in the cache sessionFactory.getCache().evictCollection("Cat.kittens", catId); // evict a particular collection of kittens sessionFactory.getCache().evictCollectionRegion("Cat.kittens"); // evict all kitten collections sessionFactory.getCache().evictCollectionRegions(); // evict all collection data
The CacheMode controls how a particular session interacts with the second-level cache.
| CacheMode.NORMAL | reads items from and writes them to the second-level cache. |
| CacheMode.GET | reads items from the second-level cache, but does not write to the second-level cache except to update data. |
| CacheMode.PUT | writes items to the second-level cache. It does not read from the second-level cache. It bypasses the effect of hibernate.cache.use_minimal_puts and forces a refresh of the second-level cache for all items read from the database. |
No comments:
Post a Comment