Python implements two LRU Algorithm Methods: pythonlru algorithm and
LRU: least recently used, which uses algorithms at least recently. Its application scenario is: when an object is stored in a limited space, when the space is full, the original object will be deleted according to certain principles. Common Principles (algorithms) include LRU, FIFO, LFU, etc. This algorithm is used in computer Cache hardware, Master memory-to-Virtual Memory Page replacement, and Redis Cache systems. I also encountered this problem during an interview and a written test.
The LRU algorithm is relatively simple. when the key is accessed (query, update, and add are generally implemented in the get () and set () methods, put the key at the frontend (or the last end) of the queue, so that the key can be sorted in descending (or ascending) Order of the last access time, when a new object is added to a space, if the space is full, delete the object at the end (or first) of the team.
In Python, you can use collections. OrderedDict to easily implement the LRU algorithm. Of course, if you cannot think of OrderedDict, you can use dict + list. This article mainly refers to lru cache in python, which is very well written and implements both functionality and simplicity and ease of reading. The Code of method 1 is basically the same as that of the reference document. method 2 is something I have come up with, which is complicated. In fact, OrderedDict itself is similar to this mechanism to achieve order.
However, the following implementation is problematic. The key: value key-value pair of the cache can only be of an unchangeable type. Because, if the value type is variable, all values returned by calling the get (key) method for the same key point to the same variable object. When you modify a value, all values are modified, even if you do not call the set () method. This is what we don't want to see. There are two solutions I have come up with: one is to save a mutable object after serialization, that is, to convert a mutable object to an immutable object; the other is to store a mutable object but get, returns a deep copy, so that the objects returned by each get () call do not affect each other. The first method is recommended. In addition, the str/unicode type is recommended for keys.
When concurrency occurs, a problem still exists, because it involves write operations on public resources, so you must lock set. In fact, in the case of concurrency, all write operations on public resources must be locked. If there is no concurrency and only a single thread exists, no lock is allowed.
Method 1: Use OrderedDict (recommended)
Copy codeThe Code is as follows:
From collections import OrderedDict
Class LRUCache (OrderedDict ):
''' You cannot store mutable objects or concurrently access set ()'''
Def _ init _ (self, capacity ):
Self. capacity = capacity
Self. cache = OrderedDict ()
Def get (self, key ):
If self. cache. has_key (key ):
Value = self. cache. pop (key)
Self. cache [key] = value
Else:
Value = None
Return value
Def set (self, key, value ):
If self. cache. has_key (key ):
Value = self. cache. pop (key)
Self. cache [key] = value
Else:
If len (self. cache) = self. capacity:
Self. cache. popitem (last = False) # pop outputs the first item
Self. cache [key] = value
Else:
Self. cache [key] = value
The test code is as follows:
Copy codeThe Code is as follows:
C = LRUCache (5)
For I in range (5, 10 ):
C. set (I, 10 * I)
Print c. cache, c. cache. keys ()
C. get (5)
C. get (7)
Print c. cache, c. cache. keys ()
C. set (10,100)
Print c. cache, c. cache. keys ()
C. set (9, 44)
Print c. cache, c. cache. keys ()
Output:
Copy codeThe Code is as follows:
OrderedDict ([(5, 50), (6, 60), (7, 70), (8, 80), (9, 90)]) [5, 6, 7, 8, 9]
OrderedDict ([(6, 60), (8, 80), (9, 90), (5, 50), (7, 70)]) [6, 8, 9, 5, 7]
OrderedDict ([(8, 80), (9, 90), (5, 50), (7, 70), (10,100)]) [8, 9, 5, 7, 10]
OrderedDict ([(8, 80), (5, 50), (7, 70), (10,100), (9, 90)]) [8, 5, 7, 10, 9]
Method 2: Use dict + list (not recommended)
Copy codeThe Code is as follows:
Class LRUCache (object ):
''' You cannot store mutable objects or concurrently access set ()'''
Def _ init _ (self, capacity ):
Self. l = []
Self. d = {}
Self. capacity = capacity
Def get (self, key ):
If self. d. has_key (key ):
Value = self. d [key]
Self. l. remove (key)
Self. l. insert (0, key)
Else:
Value = None
Return value
Def set (self, key, value ):
If self. d. has_key (key ):
Self. l. remove (key)
Elif len (self. d) = self. capacity:
Oldest_key = self. l. pop ()
Self. d. pop (oldest_key)
Self. d [key] = value
Self. l. insert (0, key)
The test code is as follows:
Copy codeThe Code is as follows:
C = LRUCache (5)
For I in range (5, 10 ):
C. set (I, 10 * I)
Print c. d, c. l
C. get (5)
C. get (7)
Print c. d, c. l
C. set (10,100)
Print c. d, c. l
C. set (9, 44)
Print c. d, c. l
The output is
Copy codeThe Code is as follows:
{8: 80, 9: 90, 5: 50, 6: 60, 7: 70} [9, 8, 7, 6, 5]
{8: 80, 9: 90, 5: 50, 6: 60, 7: 70} [7, 5, 9, 8, 6]
{5: 50, 7: 70, 8: 80, 9: 90, 10: 100} [10, 7, 5, 9, 8]
{5: 50, 7: 70, 8: 80, 9: 44, 10: 100} [9, 10, 7, 5, 8]