First-level cache and second-level cache are functions of the database itself. First-level cache starts by default, and second-level cache needs to be opened manually.
In fact, the first-level cache and the second-level cache are not very good, because when you close the database, the cache is automatically emptied, not as intelligent as redis database.
First level cache:
The first level cache refers to sqlsession. In sqlsession, there is a data area, which is a map structure. This area is the first level cache area.
The key in the first level cache is a unique value composed of SQL statement, condition, state and other information. The value in the first level cache is the result object of the query.
For the first time, the user information with user ID 1 is queried. First, the user information with user ID 1 is found in the cache. If not, the user information is queried from the database. User information is obtained and stored in a first-level cache. If sqlSession performs the commit operation(Perform insertion, update, deletion) and empty the primary cache in SqlSession to keep the latest information stored in the cache and avoid dirty reading. The second time to initiate a query for user information with user ID 1, first to find if there is user information with ID 1 in the cache, cacheYes, get user information directly from the cache.
Two level cache:
The second level cache refers to the mapper under the same namespace. There is also a map structure in the second level cache, which is the first level cache area. The key in the first level cache is a unique value composed of SQL statement, condition, state and other information. Class AThe value in the cache is the result object of the query.
The so-called secondary caching is to store the same SQL query results using namespace classification.It needs to be opened in different Mapper. XML files.
The secondary cache is at the mapper level. For the first time, we call SQL under mapper to query user information. The information queried will be stored in the secondary cache area corresponding to the mapper. The second call is the same in the mapper mapping file under the same namespaceSQL queries user information. Result is fetched from the corresponding secondary cache. If you call add or delete the SQL in the mapper mapping file under the same namespace, and perform the commit operation. This clears the secondary cache under the namespace.
If sqlSession is turned off, but because it is a secondary cache, it will still be stored in memory and the same Mapper file will be retrieved from the cache.
Here I want to declare that when I use secondary caching, I customize a User PO class. When I run the data search, I will report errors.
The mistake is to tell us that the User class you created is not serialized.
What is serialization? The process of converting the state information of an object into a form that can be stored or transmitted. During serialization, an object writes its current state to a temporary or persistent store. After that, the object can be recreated by reading or deserializing the state information of the object from the store.
When does serialization need to be done? When we need to transfer the object’s state information through the network, or we need to persist the object’s state information, so that we need to serialize the object in the future, such as writing the object to the local file, we also need to serialize it.
Because the data of secondary cache is not necessarily stored in memory, its storage medium is various, so it is necessary to serialize the cached object. If there is a parent class in this class, the parent class also needs to be serialized.