diff --git a/fsl/utils/cache.py b/fsl/utils/cache.py index 6d39d916cac771cd5f8f4917c8bd762c9cd09831..c49a6ff16c0d2e047afde6b3107f45fb2a02b809 100644 --- a/fsl/utils/cache.py +++ b/fsl/utils/cache.py @@ -63,7 +63,8 @@ class Cache(object): ``0`` will not expire. """ - if len(self.__cache) == self.__maxsize: + if len(self.__cache) == self.__maxsize and \ + key not in self.__cache: self.__cache.popitem(last=False) self.__cache[key] = CacheItem(key, value, expiry) @@ -105,11 +106,15 @@ class Cache(object): def clear(self): - """Remove all items fromthe cache. - """ + """Remove all items from the cache. """ self.__cache = collections.OrderedDict() + def __len__(self): + """Returns the number of items in the cache. """ + return len(self.__cache) + + def __parseDefault(self, *args, **kwargs): """Used by the :meth:`get` method. Parses the ``default`` argument, which may be specified as either a positional or keyword argumnet.