vinegar.utils.cache

Caching utilities.

This module provides various Cache implementations that can be used to store data that might be reused, while limiting the max. amount of memory that may be consumed.

For a cache implementation implementing the LRU strategy, see LRUCache.

class vinegar.utils.cache.Cache(*args, **kwds)

Interface for cache implementations

This interface is similar to a MutableMapping, but it does not provide an iterator and it does not provide the methods keys(), items(), setdefault(...), update(...), and values(). It does not support comparison either.

In addition to that, code using a cache should not assume that accessing a key (be it by subscripts or through the get method) is a read-only operation, because the cache might internally keep track of which key has been accessed last.

An obvious difference to regular mappings is that a cache is typically limited in size, so inserting a new key might have the side effect of removing another one.

abstract clear() → None

Clear cache (remove all items).

get(key: KeyType, default: ValueType = None) → ValueType

Return the item for the specified key or default if the key is not stored in this cache.

Parameters
  • key – key that identifies the entry in this cache.

  • default – default value that is returned if the key is not found.

Returns

value for key or default if key is not found.

class vinegar.utils.cache.LRUCache(*args, **kwds)

Cache using the last recently used (LRU) strategy.

This cache implementation is not thread-safe. If it is supposed to be used from multiple threads, wrap it in a SynchronizedCache.

clear() → None

Clear cache (remove all items).

class vinegar.utils.cache.NullCache(*args, **kwds)

Cache that actually does not cache anything.

This implementation is useful when code expects a cache to be present, but caching shall be disabled.

clear() → None

Clear cache (remove all items).

class vinegar.utils.cache.SynchronizedCache(*args, **kwds)

Wrapper around a cache instance that makes it thread-safe.

clear() → None

Clear cache (remove all items).