编程知识 cdmana.com

Second level cache of mybatis, using redis as secondary cache

Catalog
  • What is L2 cache ?
    • 1. Enable L2 cache
      • How to use L2 cache :
      • userCache and flushCache
    • 2. Use Redis Second level cache
      • How to use
    • 3. Redis Source code analysis of L2 cache

What is L2 cache ?

The principle of L2 cache and L1 cache is the same , First query , Will put the data in the cache , Then the second query will go directly to the cache . But the first level cache is based on sqlSession, The second level cache is based on mapper Of documents namespace Of , in other words Multiple sqlSession Sure share One mapper The secondary cache area in the , And how about two mapper Of namespace identical , Even two mapper, Then these two mapper In the implementation of sql The query data will also be stored in the same secondary cache area

image-20201111211828145

  • Pictured above sqlSession1 The query will be made from UserMapper Second level cache of To take , If not, perform the database query operation .
  • And then write it to the L2 cache
  • sqlSession2 Do the same thing UserMapper When inquiring , From UserMapper Second level cache of To take , At this point, there is already content in the secondary cache , So you can get it directly , No more interaction with the database .
  • sqlSession3 The transaction is performing an operation ( Insert 、 to update 、 Delete ) when , It will be emptied UserMapper Second level cache of

1. Enable L2 cache

How to use L2 cache :

mybatis in , The first level cache is enabled by default , But the secondary cache needs to be configured before it can be used

  1. In the global profile sqlMapConfig. Add the following code to :

    <!-- Enable L2 cache --><settings> <setting name="cacheEnabled" value="true"/></settings>
  2. Second, which namespace The configuration of secondary level is where it is opened , because mybatis There are notes and

  • annotation
    image-20201111221707937
    Annotation extension :

    // By default, we use mybatis Built in L2 cache , It is implemented in PerpetualCache Class , So it can be written as @CacheNamespace(implementation = PerpetualCache.class)// If using redis As a second level cache , The second part of this article will talk about 
  • image-20201111221501041
    So it turns on UserMapper Second level cache of

  1. Test one :

    We need to be based on the user id Query user information :

    image-20201111222635859

    Be careful : Will cache pojo Realization Serializable Interface , In order to extract the cache data, the deserialization operation is performed , Because the secondary cache has a variety of storage media , Not necessarily only in memory , It could be in the hard disk , If we're going to take out this cache again , You need to deserialize . therefore mybatis Of pojo To achieve Serializable Interface
    image-20201111222950505
    Finally, execute to see the print log :
    image-20201111223421674
    Why? System.out.println(user1==user2) by false ?

    L2 cache is different from L1 cache , The L2 cache does not cache objects , It's data , In the second query, the underlying layer recreates a User object , And the data in the secondary cache is repackaged into objects and returned . therefore user1 and user2 Not an object .

  2. Test two :
    Let's do a transaction operation in test two , See if you can empty the secondary cache :
    image-20201111224352601

    image-20201111224330557

​ Added a modification operation , It was found that two select, Indicates that the commit transaction will refresh the secondary cache

userCache and flushCache

It can also be configured userCache and flushCache

  • userCache : It is used to set whether to disable the secondary cache , stay statement Setting in can be Ban At present select sentence Second level cache of , That is to say, every query will send out sql. The default is true.

    image-20201111225137076
    image-20201111225544937

  • flushCache : stay mapper The same namespace in , If there are other add, delete and modify operations, you need to refresh the cache , If the cache is flushed, dirty reads will appear .
    Set up statement The configuration of the flushCache="true", That is to refresh the cache , If change to false It doesn't refresh , There may be dirty reading . So in general, there is no need to change
    image-20201111225901338

Mybatis The second level cache is used as well as the first level cache org.apache.ibatis.cache.impl.PerpetualCache

This class is mybatis The default cache class of , meanwhile , If you want to customize the cache, you have to implement cache Interface

image-20201112005725676

2. Use Redis Second level cache

Mybatis There are drawbacks to the built-in L2 cache , It's this cache that works on a single server , Can't implement distributed caching .
image-20201112010039059

So in order to solve this problem , We have to find a distributed cache to store cache data .

版权声明
本文为[Irving the procedural ape]所创,转载请带上原文链接,感谢

Scroll to Top