Hibernate caching strategy

Chapter 1:

What is cache?

      It does not refer to the computer's memory or the first and second level caches of the CPU.

      Caching refers to a strategy to reduce the frequency of application access to physical data sources, thereby improving the running performance of the application.

Why use cache?

       The efficiency of the ORM framework accessing the database directly affects the running speed of the application. It is very important to improve and optimize the execution efficiency of the ORM framework.

        Hibernate's cache is an important means to improve and optimize Hibernate's execution efficiency, so learning the use and configuration of Hibernate's cache is the key to optimization.

        

 

 

 

Chapter 2:

What did you find after reading the code:



 

1. When querying the same object for the second time, the database query is not executed again

2. When querying the same object multiple times in different sessions, multiple database queries will be executed

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




 
 Section three:

Introducing Hibernate Level 1 Cache

1, Hibernate first-level cache is also known as "Session cache", "Session-level cache"

2. When an entity is queried from the database through the session, the entity will be stored in the memory. The next time the same entity is queried, it will not be obtained from the database, but from the memory. This is the 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 scope of the data in the first-level cache is within the current session.



 

Chapter Four:

       L2 cache application

Question: After caching some commonly used data in one session, we want to use it directly in other sessions without having to cache it again?

Answer: Use a high-level second-level cache, a cache shared by each session.

 

Configuration steps for the second level cache:

       Add the jar package corresponding to the second level cache

       Add a description of the Provider class to the hibernate configuration file

       Add a second level cache property configuration file

       Add the <cache/> tag to the mapping file corresponding to the table that needs to be cached



 



 

 

 Introduction to the second level cache:

         L2 cache is also known as "global cache", "application level cache"

         The data in the second-level cache can be used in all sessions of the current application.

         The second-level cache is a pluggable cache, the default is EHCache, and also supports other second-level cache components such as: Hashtable/OSCache, SwarmCache, JBoss TreeCache, etc.

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326338981&siteId=291194637