Skip to main content

A few caching data structures and other lossy things with capped sizes.

Project description

A few caching data structures and other lossy things with capped sizes.

Latest release 20240412:

  • New CachingMapping, a caching front end for another mapping.
  • LRU_Cache: add keys() and items().

Class CachingMapping(cs.resources.MultiOpenMixin, collections.abc.MutableMapping)

A caching front end for another mapping. This is intended as a generic superclass for a proxy to a slower mapping such as a database or remote key value store.

Note that this subclasses MultiOpenMixin to start/stop the worker Thread. Users must enclose use of a CachingMapping in a with statement. If subclasses also subclass MultiOpenMixin their startup_shutdown method needs to also call our startup_shutdown method.

Example:

class Store:
  """ A key value store with a slower backend.
  """
  def __init__(self, mapping:Mapping):
    self.mapping = CachingMapping(mapping)

.....
S = Store(slow_mapping)
with S:
  ... work with S ...

Method CachingMapping.__init__(self, mapping: Mapping, *, max_size=1024, queue_length=1024, delitem_bg: Optional[Callable[[Any], cs.result.Result]] = None, setitem_bg: Optional[Callable[[Any, Any], cs.result.Result]] = None, missing_fallthrough: bool = False): Initialise the cache.

Parameters:

  • mapping: the backing store, a mapping
  • max_size: optional maximum size for the cache, default 1024
  • queue_length: option size for the queue to the worker, default 1024
  • delitem_bg: optional callable to queue a delete of a key in the backing store; if unset then deleted are serialised in the worker thread
  • setitem_bg: optional callable to queue setting the value for a key in the backing store; if unset then deleted are serialised in the worker thread
  • missing_fallthrough: is true (default False) always fall back to the backing mapping if a key is not in the cache

Method CachingMapping.flush(self): Wait for outstanding requests in the queue to complete. Return the UNIX time of completion.

Method CachingMapping.items(self): Generator yielding (k,v) pairs.

Method CachingMapping.keys(self): Generator yielding the keys.

Class LRU_Cache

A simple least recently used cache.

Unlike functools.lru_cache this provides on_add and on_remove callbacks.

Method LRU_Cache.__init__(self, max_size, *, on_add=None, on_remove=None): Initialise the LRU_Cache with maximum size max, additon callback on_add and removal callback on_remove.

Method LRU_Cache.__delitem__(self, key): Delete the specified key, calling the on_remove callback.

Method LRU_Cache.__setitem__(self, key, value): Store the item in the cache. Prune if necessary.

Method LRU_Cache.flush(self): Clear the cache.

Method LRU_Cache.get(self, key, default=None): Mapping method: get value for key or default.

Method LRU_Cache.items(self): Items from the cache.

Method LRU_Cache.keys(self): Keys from the cache.

Function lru_cache(max_size=None, cache=None, on_add=None, on_remove=None)

Enhanced workalike of @functools.lru_cache.

Release Log

Release 20240412:

  • New CachingMapping, a caching front end for another mapping.
  • LRU_Cache: add keys() and items().

Release 20181228: Initial PyPI release.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cs.cache-20240412.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cs.cache-20240412-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file cs.cache-20240412.tar.gz.

File metadata

  • Download URL: cs.cache-20240412.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.6

File hashes

Hashes for cs.cache-20240412.tar.gz
Algorithm Hash digest
SHA256 2915313d557ddbc42db6ce920762ced155d74bd162d158e875b4e31c3edd4e3c
MD5 9844818e4b5cce639b80b88b878f00f4
BLAKE2b-256 414662e6f66ac93cff2608a2e9c4f121e0a92f7e8463088f25aa37d030c64fa3

See more details on using hashes here.

File details

Details for the file cs.cache-20240412-py3-none-any.whl.

File metadata

  • Download URL: cs.cache-20240412-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.6

File hashes

Hashes for cs.cache-20240412-py3-none-any.whl
Algorithm Hash digest
SHA256 e61589180ff0bdc5fbb7e6a3a410d47c4db86752c0bc88051837d2101a6b8547
MD5 75265fe94915509cbbf3372dc42e77ca
BLAKE2b-256 17ba5ee707ee0bfc27f73c4e7bcbd9a6677ff7e3423bbad435e0631437a5f89f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page