Package: timedLruCache
A time constraint LRUCache ImplementationInstallationpip install timedLruCacheSummaryThe timed LRUCache is a dict-like container that is also size limited.
It uses the prune method when instantiated with time to remove time
expired objects.ExampleSimple LRUCachefromlrucache.lru_cacheimportLRUCachelru=LRUCache(maxSize=4)lru.insert_key_value("a",99)lru.insert_key_value("b",202)lru["c"]=203lru["d"]=204lru["e"]=205lru.get_value("a")lru.get("a",None)print(lru)print(lru.values())# LRUCache(timeout=None, size=4, data={'b': 202, 'c': 203, 'd': 204, 'e': 205})# [202, 203, 204, 205]# insert into lrucachelru.insert_key_value("j",302)# orlru["k"]=403# retrieve from the lrucachelru.get_value("z")# orlru.get("z",None)# delete from the lrucachelru.delete_key("z")# orlru.pop("z",None)Test memoization: Sample use with the decoratorfromlrucache.lru_cacheimportLRUCachefromlrucache.memoize_decoratorimportmemoized_cache@memoized_cache(cache=LRUCache(maxSize=5))defget_random(max_value):importrandomreturnrandom.random()*max_valueprint(get_random(1))# 0.6869437097681024print(get_random(1))# 0.6869437097681024print(get_random(3))# 1.2792457326076399print(get_random(4))# 1.9216226691107239print(get_random(5))# 3.442601057826532print(get_random(5))# 3.442601057826532print(get_random(7))# 0.6831533160972438print(get_random(8))# 7.40200570325546print(get_random(1))# 0.37636284785825047print(get_random.misses)# => 7print(get_random.hits)# => 2# cache infoget_random.cache_info()# => memoized_cache(hits=2, misses=7, maxsize=5, currsize=5)# check the cache stored key, value, items pairsprint(get_random.cache.keys())# => dict_keys([-5205072475343462643, 8575776084210548143, -2238842041537299568, -8811688270097994377, 2613783748954017437])print(get_random.cache.values())# => [1.9216226691107239, 3.442601057826532, 0.6831533160972438, 7.40200570325546, 0.37636284785825047]print(get_random.cache.items())# => [#     (-5205072475343462643, 1.9216226691107239), (8575776084210548143, 3.442601057826532),#     (-2238842041537299568, 0.6831533160972438), (-8811688270097994377, 7.40200570325546),#     (2613783748954017437, 0.37636284785825047)# ]# Simple report on performance# check report before get_random.reset()report=f'Hit %:{(float(get_random.hits)/(get_random.hits+get_random.misses))}'print(report)# => Hit %: 0.2222222222222222# reset hits and misses count to zero.get_random.reset()Test time constraintfromtimeimportsleepfromlrucache.lru_cacheimportLRUCachetimed_lru=LRUCache(maxSize=4,timeout=10)timed_lru["a"]=202timed_lru["b"]=203timed_lru["c"]=204timed_lru["d"]=205timed_lru["e"]=206# cache size remains 4, after inserting 5 items into cache.print(timed_lru)# LRUCache(timeout=10, size=4, data={'b': 203, 'c': 204, 'd': 205, 'e': 206})sleep(60)# cache should be empty after 60s as it clears its entry after 10s (timeout)timed_lru["d"]=203timed_lru["e"]=204timed_lru["f"]=205timed_lru["g"]=206timed_lru["h"]=207# cache now has new entriesprint(timed_lru)# LRUCache(timeout=10, size=4, data={'e': 204, 'f': 205, 'g': 206, 'h': 207})sleep(30)# cache entry expires after 10s and as a result we have nothing in the cache (data = {}).print(timed_lru)# LRUCache(timeout=10, size=4, data={})timed_lru.stop_timer()Sample Usage with the decorator (maxSize and timeout)@memoized_cache(cache=LRUCache(maxSize=5,timeout=5))defget_random(max_value):importrandomreturnrandom.random()*max_valueget_random(1)get_random(1)get_random(3)get_random(4)get_random(5)get_random(5)get_random(7)get_random(8)get_random(1)get_random.cache_info()# memoized_cache(hits=2, misses=7, maxsize=5, currsize=5)print(get_random.cache.values())# => [2.108203625973244, 0.2784180276772963, 3.9932738384806856, 1.2462533799577011, 0.8501249397423805]print(get_random.cache.items())# [(7793041093296417556, 2.108203625973244), (-5573334794002472495, 0.2784180276772963), (6169942939433972205, 3.9932738384806856), (-179359314705978364, 1.2462533799577011), (2135404498036021478, 0.8501249397423805)]print(get_random.cache.keys())# dict_keys([7793041093296417556, -5573334794002472495, 6169942939433972205, -179359314705978364, 2135404498036021478])print(get_random.cache.values())# [2.108203625973244, 0.2784180276772963, 3.9932738384806856, 1.2462533799577011, 0.8501249397423805]sleep(20)get_random.cache_info()# memoized_cache(hits=2, misses=7, maxsize=5, currsize=0)# stop the timerget_random.cache.stop_timer()