diff --git a/fsl/utils/cache.py b/fsl/utils/cache.py index 5a89817d99520d83b3a8d3729b9dd117e0cb0be9..e8de25aa12d8f030b153dfce653698cf89ca2029 100644 --- a/fsl/utils/cache.py +++ b/fsl/utils/cache.py @@ -42,14 +42,20 @@ class Cache(object): raised. """ - def __init__(self, maxsize=100): + def __init__(self, maxsize=100, lru=False): """Create a ``Cache``. :arg maxsize: Maximum number of items allowed in the ``Cache`` before it starts dropping old items + + :arg lru: (least recently used) If ``False`` (the default), items + are dropped according to their insertion time. Otherwise, + items are dropped according to their most recent access + time. """ self.__cache = collections.OrderedDict() self.__maxsize = maxsize + self.__lru = lru def put(self, key, value, expiry=0): @@ -94,14 +100,26 @@ class Cache(object): else: entry = self.__cache[key] + # Check to see if the entry + # has expired + now = time.time() + if entry.expiry > 0: - if time.time() - entry.storetime > entry.expiry: + if now - entry.storetime > entry.expiry: self.__cache.pop(key) if defaultSpecified: return default else: raise Expired(key) + # If we are an lru cache, update + # this entry's expiry, and update + # its order in the cache dict + if self.__lru: + entry.storetime = now + self.__cache.pop(key) + self.__cache[key] = entry + return entry.value @@ -134,7 +152,7 @@ class Cache(object): - ``True`` if a default argument was specified, ``False`` otherwise. - - The specifeid default value, or ``None`` if it wasn't + - The specified default value, or ``None`` if it wasn't specified. """