29
29
from time import sleep ,time ,perf_counter
30
30
from os .path import getmtime
31
31
from dataclasses import dataclass , field , fields , is_dataclass , MISSING , make_dataclass
32
+ import asyncio
33
+ from collections import OrderedDict
32
34
33
35
# %% ../nbs/03_xtras.ipynb
34
36
def walk (
@@ -747,9 +749,8 @@ def is_namedtuple(cls):
747
749
# %% ../nbs/03_xtras.ipynb
748
750
def flexicache (* funcs , maxsize = 128 ):
749
751
"Like `lru_cache`, but customisable with policy `funcs`"
750
- import asyncio
751
752
def _f (func ):
752
- cache ,states = {} , [None ]* len (funcs )
753
+ cache ,states = OrderedDict () , [None ]* len (funcs )
753
754
def _cache_logic (key , execute_func ):
754
755
if key in cache :
755
756
result ,states = cache [key ]
@@ -763,8 +764,7 @@ def _cache_logic(key, execute_func):
763
764
cache [key ] = cache .pop (key )
764
765
return result
765
766
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 )
768
768
return newres
769
769
770
770
@wraps (func )
0 commit comments