Home  >  Article  >  Java  >  Introduction to hibernate caching mechanism

Introduction to hibernate caching mechanism

巴扎黑
巴扎黑Original
2017-07-17 17:52:491800browse

hibernate first-level cache

1.Hibernate first-level cache is also called "Session cache" and "session-level cache".

2. When querying an entity from the database through Session, the entity will be stored in the memory. The next time you query the same entity, it will no longer be obtained from the database, but from the memory. This is cache

3. The life cycle of the first-level cache is the same as that of the Session. When the Session is destroyed, it is also destroyed.

4. The applicable range of data in the first-level cache is within the current session.

why (Why use Hibernate cache?)

Hibernate is a persistence layer framework that often accesses physical databases .

In order to reduce the frequency of application access to physical data sources, thereby improving the running performance of the application.

The data in the cache is a copy of the data in the physical data source. The application reads and writes data from the cache at runtime. At a specific moment or event, the data in the cache and the physical data source will be synchronized


#what( What is the principle of Hibernate cache? ) Hibernate cache includes two categories: Hibernate first-level cache and Hibernate second-level cache.

1.Hibernate first-level cache is also called "Session cache".

Session built-in cannot be unloaded, and the Session cache is a transaction-scope cache (the life cycle of the Session object usually corresponds to a database transaction or an application transaction).

In the first-level cache, each instance of the persistent class has a unique OID.

2.Hibernate second-level cache is also called "SessionFactory cache".

Since the life cycle of the SessionFactory object corresponds to the entire process of the application, the Hibernate second-level cache is a process-wide or cluster-wide cache. Concurrency problems may occur, so an appropriate concurrent access strategy needs to be adopted. Policies provide transaction isolation levels for cached data.

The second level cache is optional and is a configurable plug-in. SessionFactory will not enable this plug-in by default.

Hibernate provides the org.hibernate.cache.CacheProvider interface, which acts as an adapter between the cache plug-in and Hibernate.

What kind of data is suitable to be stored in the second level cache?

1) Data that is rarely modified
2) Data that is not very important, occasional concurrent data is allowed
3) Data that will not be accessed concurrently
4) Constant data
Data that is not suitable for storage in the second level cache?
1) Data that is frequently modified
2) Data that is never allowed to be accessed concurrently, such as financial data, is never allowed to be accessed concurrently
3) Data shared with other applications.


API for managing the first-level cache

1.evict(), used to clear an object from the Session’s first-level cache.

2.clear(), used to clear all objects in the first-level cache.

Qurey.list() and Qurey.iterate()

1.Qurey.list() query data , will not search from the first-level cache, directly send the SQL statement to the database, and retain the object returned by the query in the cache.

2.Qurey.iterate() does not search from the first-level cache, but directly sends sql to the database to query the id. When other attributes of the object need to be used, the object is first searched for in the cache according to the id. If there is no Then send a sql query to the database, so using this method alone to query will cause an N+1 problem (that is, sending N+1 statements to the database to query the information of N objects)

3. If this requirement exists: The same object needs to be accessed in two different sessions. Two sql statements or more need to be sent through Qurey.list(). In order to avoid setting up a second-level cache, Query.list() in the first session , in the second session, directly obtain it from the second-level cache through Qurey.iterate() iteration

hibernate second-level cache (SessionFactory cache)

1. Add the configuration in hibernate.cfg.xml

        <!-- 开启二级缓存 --><property name="hibernate.cache.use_second_level_cache">true</property><!-- 二级缓存的提供类 在hibernate4.0版本以后我们都是配置这个属性来指定二级缓存的提供类--><property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property><!-- 二级缓存配置文件的位置 --><property name="hibernate.cache.provider_configuration_file_resource_path">ehcache.xml</property>
2. For details about the configuration of ehcache.xml, see


3. Configure the entity through annotations, and add @Cache(usage=CacheConcurrencyStrategy.READ_ONLY) in front of the class

Common CacheConcurrencyStrategy attribute values ​​​​are READ_ONLY (the table data corresponding to the entity is only read and then cached) and READ-WIRTE (table data can be read or updated)

4, be sure to note: hibernate's second-level cache must be the entire object. If only some attributes of the object are queried, then the object will not Cached

Query cache (SessionFactory level cache)

1. Continue based on the above second-level cache Configuration

2. Add configuration

  <property name="hibernate.cache.use_query_cache">true</property>
in hibernate.cfg.xml

2. Add @Cacheable to the entity annotation

3. Call the setCacheable(true) method after the hql statement

4. Only when the HQL query statement is exactly the same, even the parameters The settings must be the same, and the query cache is effective at this time

hibernate's three states (transient (transient state), persistent (persistent state) and detached ( Offline))


The above is the detailed content of Introduction to hibernate caching mechanism. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn