LRUCache principle:
Maintain a list of cached objects and arrange objects in the order they are accessed, that is, objects that have not been accessed are placed at the end of the queue and are about to be eliminated. The most recently visited objects were put on the team head and finally eliminated. And this queue is mainly implemented by Linkedhashmap.
Linkedhashmap: (The default is insert sort, i.e. the input is consistent with the output order)
Inherit from HashMap, using a doubly linked list to store entry order relationships in the map. The order has 2, one is the access order, the first is the insertion order, it can be constructed by its method linkedhashmap (int initialcapacity,float Loadfactor,boolean accessorder:true to denote sequential access, False is the insertion sort) specified. So, get,put,remove and so on, in addition to doing hashmap operation, but also need to adjust entry order list of work.
LRUCache in Android sets the default order in Linkedhashmap to the order of access, each time a get is called, the object is moved to the head of the linked list, and a put is called to insert a new object into the list header. When the memory cache reaches the maximum value, the object at the tail of the list is removed. Each time you put or remove, you need to determine whether the cache size is trimtosize enough.
Note: The list header-recently used or inserted, the tail of the list-rarely used recently.
This picture comes from the network, if there is infringement please contact me, will be deleted!
Android LRUCache Principle