Java--Hibernate (first level cache)

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.

Simply put: it is the strategy of how to search the database faster to find the desired data

exist to improve efficiency

Copy to physical data source Copy to memory

figure 1

 

 

If it misses, it will be copied in

First, introduce the first-level cache:

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

2. When querying an entity from the database through the session, the entity is 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, he is also destroyed

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

Second, Hibernate first-level cache API:

The first-level cache is the default cache of Hibernate and cannot be cancelled. It is managed in two ways:

1. evict(): Used to clear an object from the first level cache of the Session.

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

Ps: Level 1 cache also sometimes affects the performance of the program.

Database preparation

 

 

method one

 

 

result

 

 

in conclusion

 

 

The second case is a bit special

 

 

new tool class

 

 

 

 

Method 2

 

 

 

 

 

 

Method 3

 

 

 

 

 

Method 4

 

 

 

 

 

// After the HQL query, the query result will enter the first level cache but the query will not use the first level cache

// criteria=> will put the query result in the cache and the query will not use the first level cache to be consistent with HQL

}

// When sql query, if the query result is encapsulated into an object, the object will be put into the first level cache

 

Guess you like

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