slowdown.lrucache – Least Recently Used (LRU) Cache

This module provides an implementation of the Least Recently Used (LRU) cache.

Example:

>>> import slowdown.lrucache
>>> import time
>>> cache = \
...     slowdown.lrucache.LRUCache(
...         size=20000,            # cache size, the default is 20000
...
...         expiration_time=600.,  # the expiration time of the data,
...                                # the default is 600 seconds.
...
...         cleancycle=60.         # clear expired data at this
...                                # specified time, the default is
...                                # 60. seconds.
...     )
>>> cache['A'] = DATA_1
>>> cache['B'] = DATA_2
>>> cache['C'] = DATA_3
>>> if 'A' in cache:
...     del cache['A']
>>> cache.get('B')
DATA_2
>>> cache.pop('B')
DATA_2
>>> time.sleep(601.)
>>> time.get('C')
None
class slowdown.lrucache.LRUCache(size: int = - 1, expiration_time: float = - 1, cleancycle: float = - 1)

Dict-like Least Recently Used (LRU) cache.

__setitem__(key, value)

Set self[key] to value

__delitem__(key)

Delete self[key]

pop(key[, default]) → value

remove specified key and return the corresponding value.

get(key, default=None)

Return the value for key if key is in the dictionary, else default.