LRU of Mybatis second-generation cache

LRU algorithm, delete the oldest, that is, the key that is used very rarely.

1. The bottom layer is implemented by LinkedHashMap:

mybatis implements the construction method of LinkedHashMap, and the last parameter accessOrder is true to open the LRU algorithm.

Override the removeEldestEntry method and return true when size() is greater than the current setting.

 

2. Double-line linked list structure in LinkedHashMap, head represents the current oldest node.

Every time you get or put, if accessOrder is true, afterNodeAccess() is called to move the current node to the end of the linked list.

Then call afterNodeInsertion() to delete the head node when put and compute.

 

3. LinekedHashMap inherits from HashMap, HashMap does not implement afterNodeAccess(), afterNodeInsertion(),

The specific logic is implemented in LinekedHashMap, and then when deleting, the removeNode() of HashMap is still called.

Guess you like

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