fsl.utils.cache

This module provides the Cache class., a simple in-memory cache.

exception fsl.utils.cache.Expired[source]

Bases: Exception

Exception raised by the Cache.get() metho when an attempt is made to access a cache item that has expired.

__module__ = 'fsl.utils.cache'
__weakref__

list of weak references to the object (if defined)

class fsl.utils.cache.CacheItem(key, value, expiry=0)[source]

Bases: object

Internal container class used to store Cache items.

__init__(key, value, expiry=0)[source]
__dict__ = mappingproxy({'__module__': 'fsl.utils.cache', '__doc__': 'Internal container class used to store :class:`Cache` items. ', '__init__': <function CacheItem.__init__>, '__dict__': <attribute '__dict__' of 'CacheItem' objects>, '__weakref__': <attribute '__weakref__' of 'CacheItem' objects>, '__annotations__': {}})
__module__ = 'fsl.utils.cache'
__weakref__

list of weak references to the object (if defined)

class fsl.utils.cache.Cache(maxsize=100, lru=False)[source]

Bases: object

The Cache is a simple in-memory cache built on a collections.OrderedDict. The Cache class has the following features:

  • When an item is added to a full cache, the oldest entry is automatically dropped.

  • Expiration times can be specified for individual items. If a request is made to access an expired item, an Expired exception is raised.

__init__(maxsize=100, lru=False)[source]

Create a Cache.

Parameters:
  • maxsize – Maximum number of items allowed in the Cache before it starts dropping old items

  • 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.

put(key, value, expiry=0)[source]

Put an item in the cache.

Parameters:
  • key – Item identifier (must be hashable).

  • value – The item to store.

  • expiry – Expiry time in seconds. An item with an expiry time of 0 will not expire.

get(key, *args, **kwargs)[source]

Get an item from the cache.

Parameters:
  • key – Item identifier.

  • default – Default value to return if the item is not in the cache, or has expired.

clear()[source]

Remove all items from the cache.

__len__()[source]

Returns the number of items in the cache.

__getitem__(key)[source]

Get an item from the cache.

__setitem__(key, value)[source]

Add an item to the cache.

__contains__(key)[source]

Check whether an item is in the cache. Note that the item may be in the cache, but it may be expired.

keys()[source]

Return all keys in the cache.

values()[source]

Return all values in the cache.

items()[source]

Return all (key, value) pairs in the cache.

__parseDefault(*args, **kwargs)

Used by the get() method. Parses the default argument, which may be specified as either a positional or keyword argumnet.

Returns:

A tuple containing two values:

  • True if a default argument was specified, False otherwise.

  • The specified default value, or None if it wasn’t specified.

__dict__ = mappingproxy({'__module__': 'fsl.utils.cache', '__doc__': 'The ``Cache`` is a simple in-memory cache built on a\n    ``collections.OrderedDict``. The ``Cache`` class has the following\n    features:\n\n       - When an item is added to a full cache, the oldest entry is\n         automatically dropped.\n\n       - Expiration times can be specified for individual items. If a request\n         is made to access an expired item, an :class:`Expired` exception is\n         raised.\n    ', '__init__': <function Cache.__init__>, 'put': <function Cache.put>, 'get': <function Cache.get>, 'clear': <function Cache.clear>, '__len__': <function Cache.__len__>, '__getitem__': <function Cache.__getitem__>, '__setitem__': <function Cache.__setitem__>, '__contains__': <function Cache.__contains__>, 'keys': <function Cache.keys>, 'values': <function Cache.values>, 'items': <function Cache.items>, '_Cache__parseDefault': <function Cache.__parseDefault>, '__dict__': <attribute '__dict__' of 'Cache' objects>, '__weakref__': <attribute '__weakref__' of 'Cache' objects>, '__annotations__': {}})
__module__ = 'fsl.utils.cache'
__weakref__

list of weak references to the object (if defined)