Cache

Custom caches and utilities

class LRUCache(func: Callable, maxsize: int = 8)[source]

Bases: object

Custom Least Recently Used (LRU) memory cache.

Parameters:
  • func (class or func) -- Function result or class instantiation to be cached.

  • maxsize (int, optional) -- Maximum size of the cache.

class LRUSharedRWCache(func: Callable, name: str | None = None, maxsize: int = 8, shared: bool = True, removecall: Callable | None = None, logger: Logger | None = None)[source]

Bases: object

Custom, dictionary-based, Least Recently Used (LRU) reader-writer lockable cache, shareable across processes.

Parameters:
  • func (Callable) -- Function to be cached.

  • name (str, optional) -- Name of the lock cache.

  • maxsize (int, optional) -- Maximum size of the cache.

  • shared (bool, optional) -- Share dictionary across processes (requires POSIX IPC support)

  • removecall (func, optional) -- Callback function for removing cached data.

  • logger (logging.Logger, optional) -- Logger object to display cache handling information.

call_unshared(*args, **kwargs)[source]

Get or recover a cached entry, not shared with other processes.

Parameters:
  • args (any) -- Hashable arguments to the cache dictionary.

  • kwargs (any) -- Hashable keyworded arguments to the cache dictionary.

Returns:

  • result -- Cached output.

  • lock -- Reader-Writer lock associated with args, set to None.

  • msg -- "OK" or error string in case of an exception.

  • :meta public:

call_shared(*args, **kwargs)[source]

Get or recover a cached entry, not shared with other processes.

Parameters:
  • args (any) -- Hashable arguments to the cache dictionary.

  • kwargs (any) -- Hashable keyworded arguments to the cache dictionary.

Returns:

  • result -- Cached output.

  • lock -- Reader-Writer lock associated with args.

  • msg -- "OK" or error string in case of an exception.

  • :meta public:

remove(*args) None[source]

Remove semaphores.

class SharedRWLock(name: str = 'RWLock')[source]

Bases: object

Custom reader-writer lock shareable across processes

See Raynal, Michel (2012), Concurrent Programming: Algorithms, Principles, and Foundations, Springer.

Parameters:

name (str, optional) -- Name of the lock.

acquire_read() None[source]

Acquire lock in read mode.

acquire_write() None[source]

Acquire lock in write mode.

release_read() None[source]

Release acquired read lock.

release_write() None[source]

Release acquired write lock.

remove(*args) None[source]

Remove files used by the RW lock semaphores and shared memory.