Simple LRU cache
This more or less an OrderedDict that:
-
Stores items in the order they were most recently added, and
-
Removes as many of the least-recently updated (but not expired) items as necessary to reduce cache size to (maxsize - hysteresis)
Items in the cache expire when their age reaches the expiration limit, and also if they have not been accessed recently (i.e. "rolling" expiration).
Instantiate, then set and access values much like a python dict:
>>> from kuaiqu import Kuaiqu
>>> dut = Kuaiqu()
>>> dut.set('hello', 'world')
>>> dut.get('hello')
'world'
>>> dut['hello']
'world'
Configuration can be passed to the constructor:
-
maxsize is the maximum number of items that can be stored in the cache.
-
hysteresis is the number of items to remove when the cache gets trimmed. Items are removed according to their age (oldest items first).
-
rolling is the maximum number of minutes that are allowed between object retrievals before the object expires.
-
expiration is the maximum number of minutes that an object can exist in the cache, else None for no expiration.
Additional kwargs that are passed to the constructor will be passed to the cache as initial data.
Return the keys in the cache, as an OrderedDictKeysView.
Return (key, value) pairs for items in the cache, as an OrderedDictItemsView.
Add an object to the cache
Get an item from the cache, and update the rolling expiration. Also supports dictionary access.
Pop an item from the cache and return the value.
Pop a random item from the cache and return it as a (key, value) pair.
Delete an item from the cache
Remove items from the cache, using one or both of the expired and length strategies.
Clear all items from the cache.
Return the number of items in the cache. Note that this counts all (both expired and unexpired) items so may not provide the expected result in some cases.
Alternate call to items().
Return True if the specified key is in the specified instance, else None. Does not consider where the object has expired.
Items in the cache can be passed to a function using either *arg or **kwarg notation.