Functions of hibernate cache:
Hibernate is a persistent layer framework that frequently accesses physical databases.ProgramThe frequency of access to the physical data source to improve the running performance of the application. Data in the cache is the replication of data in the physical data source. The application reads and writes data from the cache at runtime, and the data in the cache and the physical data source will be synchronized at a specific time or event.
Hibernate cache category:
Hibernate cache includes two types: hibernate level-1 cache and hibernate level-2 cache.
The Hibernate level-1 cache is also called the "session cache". It is built in and cannot be detached (the cache cannot be detached, which means it is not optional and must have functions, the session cache cannot be canceled ). Because the lifecycle of a Session Object usually corresponds to a database transaction or an application transaction, its cache is the cache of the transaction scope. The first-level cache is required and cannot be detached. In the first cache, each instance of the persistence class has a unique oid.
Hibernate second-level cache is also called "sessionfactory cache". Because the life cycle of the sessionfactory object corresponds to the entire process of the application, Hibernate second-level cache is a cache of process scope or cluster scope, concurrency problems may occur. Therefore, an appropriate concurrent access policy is required, which provides transaction isolation level for cached data. The second-level cache is optional and is a configurable plug-in. By default, sessionfactory does not enable this plug-in.
What kind of data is suitable for storing in the second-level cache?
1. Rarely modified data
2. Not very important data, allowing occasional Concurrent Data
3. data that will not be accessed concurrently
4. Constant data
Is it not suitable for storing data in the second-level cache?
1. frequently modified data
2. concurrent access data, such as financial data, is absolutely not allowed.
3. data shared with other applications.
How does hibernate search for objects to apply cache?
When hibernate accesses a Data Object Based on its ID, it first looks up the data object from the session level-1 cache. If no second-level cache is configured, it looks up from the second-level cache. If no second-level cache is found, query the database and put the result into the cache by ID.
When you delete, update, or add data, update the cache at the same time.
Hibernate manages cache instances
Whenever we manage the caches, when you pass an object to the SAVE (), update (), or saveorupdate () method, or use load () when the get (), list (), iterate (), or scroll () methods obtain an object, the object will be added to the internal cache of the session.
When the flush () method is subsequently called, the object state is synchronized with the database. If you do not want this synchronization operation to happen, or you are processing a large number of objects and need to manage the memory effectively, you can call the evict () method, remove these objects and their collections from the first-level cache.