Skip to content

Commit f457b99

Browse files
committed
Use OrderedDict for FIFO for LRU
1 parent 9eb2fac commit f457b99

File tree

2 files changed

+59
-59
lines changed

2 files changed

+59
-59
lines changed

fastcore/xtras.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
from time import sleep,time,perf_counter
3030
from os.path import getmtime
3131
from dataclasses import dataclass, field, fields, is_dataclass, MISSING, make_dataclass
32+
import asyncio
33+
from collections import OrderedDict
3234

3335
# %% ../nbs/03_xtras.ipynb
3436
def walk(
@@ -747,9 +749,8 @@ def is_namedtuple(cls):
747749
# %% ../nbs/03_xtras.ipynb
748750
def flexicache(*funcs, maxsize=128):
749751
"Like `lru_cache`, but customisable with policy `funcs`"
750-
import asyncio
751752
def _f(func):
752-
cache,states = {}, [None]*len(funcs)
753+
cache,states = OrderedDict(), [None]*len(funcs)
753754
def _cache_logic(key, execute_func):
754755
if key in cache:
755756
result,states = cache[key]
@@ -763,8 +764,7 @@ def _cache_logic(key, execute_func):
763764
cache[key] = cache.pop(key)
764765
return result
765766
cache[key] = (newres, [f(None) for f in funcs])
766-
# remove the oldest item when cache overflows
767-
if len(cache) > maxsize: del cache[next(iter(cache))]
767+
if len(cache) > maxsize: cache.popitem(last=False)
768768
return newres
769769

770770
@wraps(func)

0 commit comments

Comments
 (0)