Detailed explanation of Hibernate Cache Technology and hibernate Cache

Source: Internet
Author: User
Tags custom name

Detailed explanation of Hibernate Cache Technology and hibernate Cache
Reprinted with the source: http://www.cnblogs.com/xiaoming0601/p/5882980.html一: it does not mean the first-level cache of the computer's memory or CPU; cache is a strategy to reduce the frequency of application access to physical data sources and improve the running performance of applications. Ii. Why cache is used: 1. The efficiency of accessing the database by the ORM framework directly affects the running speed of the application, and it is vital to improve and optimize the execution efficiency of the orm framework. 2. Hibernate cache is an important means to improve and optimize the execution efficiency of Hibernate. Therefore, learning to use and configure Hibernate cache is the key to optimization. 3. to judge whether an ORM framework is excellent, the frequency of database access is an important criterion. Iii. Introduction of level 1 cache: 1. hibernate level-1 cache is also called "Session cache" and "Session-level cache" 2. when an object is queried from a database through a Session, the object is stored in the memory. The next query of the same object is not obtained from the database, but from the memory. This is cache 3. the lifecycle of a level-1 cache is the same as that of a Session. If a Session is destroyed, it is also destroyed. data in the first-level cache can be applied to the current session. IV. Hibernate first-level cache API: the first-level cache is the default cache of Hibernate and cannot be canceled. You can manage it in two ways: 1. evict (): clears an object from the Session cache. 2. clear (): Used to clear all objects in the first-level cache. 5. query. list () and query. differences between iterator () Methods: 1. each time the list operation uses a statement to directly retrieve all the data returned by the database (and store the object in the hibernate cache), the query statement is sent when the list () method is used twice in a session, prove that the list () method does not use cache; 2. iterator first extracts the IDs of all data using a statement, and then finds whether the object exists in the hibernate level-1 cache using the id. If the object exists, it will retrieve it directly, if no, issue an SQL statement again to obtain the object by id (and add it to the cache ), in this way, if none of the IDS are in the cache, n + 1 SQL statement will occur. 3. Therefore, the two must be used properly to maximize performance. 4. after list () is used, data exists in the first-level cache. If iterator () is used in the same session, database statements are not sent, proving that iterator () uses the first-level cache, when the list () method is used twice in a session, a query statement is sent, proving that the list () method does not use the cache.Vi. Introduction to Level 2 Cache1. the second-level cache is also called "Global cache" and "application-level cache". 2. data in the second-level cache can be used for all sessions of the current application. 3. the second-level cache is a pluggable cache. The default value is EHCache. It also supports other second-level cache components such as HashTable, OSCache, and SwarmCache. VII. Second-level cache (Cache shared by each session)1. cache shared by each session 2. secondary cache is not enabled by default. You need to manually configure. import jar package B. add ehcache. the xml configuration file maxElementsInMemory = "10000" indicates the maximum number of objects allowed to be created in the cache. Eternal = "false" indicates whether the cached object is permanent. TimeToIdleSeconds = "120" indicates the cache data passivation time (that is, the idle time before the object expires ). TimeToLiveSeconds = "120" indicates the cache data survival time (that is, the object's survival time before it expires ). OverflowToDisk = "true" indicates whether the disk is stored after overflow. C. hibernate. cfg. configure the external implementation Class d for the specified level-2 cache in xml. xx. hbm. in the xml configuration file, specify the second-level cache <cache usage = "read-only" region = "Employee (custom name) "/> Read-Only (region =" Employee (custom name) ": In ehcache. xml file custom configuration) 8. data with the following features is usually placed in the second-level cache. data that is rarely modified; 2. not very important data, allowing occasional concurrent data; 3. data that will not be accessed concurrently; 4. reference data 9. comparison of level 1 and level 2 Cache

 

10. Summary1. hibernate cache can improve retrieval efficiency; 2. hibernate caches are classified into level-1 caches and level-2 caches. level-1 caches are session-level caches and level-2 caches are application-level caches. 3. the cache of Hibernate also increases server consumption while improving retrieval. Therefore, pay attention to the Cache Usage Policy.

 

 


 

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.