Preliminary understanding of cache in hibernate

Source: Internet
Author: User

Why Cache

The purpose is to reduce the number of times the application accesses the physical data source and improve the running performance of the application. if hibernate uses the cache, it first goes to the cache for data query. if it finds the cache, it will be used directly. If it cannot be found, it will be retrieved from the physical data source, after frequently-used data is loaded into the cache area, you can greatly reduce the application's access to the physical data source, significantly improving the running performance of the program.

The implementation process of using the cache for query is as follows: First, check whether the required data exists in the level-1 cache. If no data exists, query the level-2 Cache. If no data exists in the level-2 cache, then, query the database. Note that the query speed of these three methods is reduced in turn.

The Hibernate cache is divided into two levels. The first level is stored in the session, which is called the first level cache. It is carried by default and cannot be detached. The second level is the process-level cache controlled by sessionFactory, which is also known as the process-level cache or SessionFactory-level cache. The second level cache can be shared by the session. The life cycle is the same as that of SessionFactory. SessionFactory can manage the second-level cache.

Level 1 Cache

A level-1 cache is a cache object, and a Load/get/iterate query object supports level-1 cache.

Evit (Object obj) clears the specified persistent Object from the first-level cache, releases the memory resources occupied by the Object, and changes the Object from the persistent state to the out-of-the-tube state to the free Object.

Clear () clears all persistent objects in the first-level cache and releases the memory resources it occupies.

Contains (Object obj) determines whether the specified Object exists in the first-level cache.

Flush () refreshes the content of the level-1 cache so that it can be synchronized with the database data.

Why do I need to use level-2 Cache if I have a level-1 cache?

Because the Session life cycle is usually very short, and the first-level fastest cache life cycle in the Session is also very short, the first-level cache hit rate is very low. Its Improvements to system performance are also limited.

Level 2 Cache

The implementation principle of level-2 cache is the same as that of level-1 cache, and level-2 cache is also used to cache entity objects. Evict (Classarg0, Serializable arg1) clears the persistence object of the specified ID of a class from the second-level cache and releases the resources occupied by the object.

When can it be loaded to the second-level cache:

1. Low data update frequency

2. Allow occasional concurrent errors of non-important data

3. data that will not be accessed concurrently

4. Constant data

5. Data not modified by a third party

Otherwise, the second-level cache is not suitable for the following scenarios:

1. frequently modified data

2. concurrent access to data is absolutely not allowed

3. data shared with other applications

Interaction between a level-1 cache and a level-2 cache is prohibited.

Query Cache

The query cache is disabled by default and is not enabled. The query cache caches the result set of common attributes and caches the id of the result set of the object. Lifecycle: When the associated table is modified, the lifecycle of the query cache ends. It is not necessary to enable the query cache in the configuration file. It must also be enabled in the program.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.