The LRU cache in Python3.3 has O (1) insertion, deletion, and search.
The project uses a circular doubly linked list of records (ranging from oldest to newest) and a hash table to search for individual links. Cache hits use a hash table to find the appropriate link and move it to the top of the list. The cache skips deleting the oldest link and creates a new link at the top of the linked list.
Here is a simplified (but quick) version of 33 lines of very simple Python (using only simple vocabulary and lists). It works on Python2.0 and later (either PyPy or Jython or Python3.x):
class LRU_Cache: def __init__(self, original_function, maxsize=1024):
Raymond hettinger
source share