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 20240422: New ConvCache and convof: a cache for conversions of file contents such as thumbnails or transcoded media.
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 mappingmax_size: optional maximum size for the cache, default 1024queue_length: option size for the queue to the worker, default 1024delitem_bg: optional callable to queue a delete of a key in the backing store; if unset then deleted are serialised in the worker threadsetitem_bg: optional callable to queue setting the value for a key in the backing store; if unset then deleted are serialised in the worker threadmissing_fallthrough: is true (defaultFalse) 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 ConvCache(cs.fs.HasFSPath)
A cache for conversions of file contents such as thumbnails
or transcoded media, etc. This keeps cached results in a file
tree based on a content key, whose default function is
cs.hashutils.file_checksum({HASHNAME_DEFAULT!r}).
Method ConvCache.__init__(self, fspath: Optional[str] = None, content_key_func=None):
Initialise a ConvCache.
Parameters:
fspath: optional base path of the cache, default fromConvCache.DEFAULT_CACHE_BASEPATH; if this does not exist it will be created usingos.mkdircontent_key_func: optional function to compute a key from the contents of a file, defaultcs.hashindex.file_checksum(the sha256 hash of the contents)
Method ConvCache.content_key(self, srcpath):
Return a content key for the filesystem path srcpath.
Method ConvCache.content_subpath(self, srcpath):
Return the content key based subpath component.
This default assumes the content key is a hash code and
breaks it hex representation into a 3 level hierarchy
such as 'd6/d9/c510785c468c9aa4b7bda343fb79'.
Method ConvCache.convof(self, srcpath, conv_subpath, conv_func, ext=None):
Return the filesystem path of the cached conversion of srcpath via conv_func.
Parameters:
srcpath: the source filesystem pathconv_subpath: a name for the conversion which encompasses the salient aspaects such as'png/64/64'for a 64x64 pixel thumbnail in PNG formatconv_func: a callable of the formconv_func(srcpath,dstpath)to convert the contents ofsrcpathand write the result to the filesystem pathdstpathext: an optional filename extension, default from the first component ofconv_subpath
Function convof(srcpath, conv_subpath, conv_func, ext=None)
ConvCache.convof using the default cache.
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 20240422: New ConvCache and convof: a cache for conversions of file contents such as thumbnails or transcoded media.
Release 20240412:
- New CachingMapping, a caching front end for another mapping.
- LRU_Cache: add keys() and items().
Release 20181228: Initial PyPI release.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cs.cache-20240422.tar.gz.
File metadata
- Download URL: cs.cache-20240422.tar.gz
- Upload date:
- Size: 8.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.10.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
606ebf28e0a9c4a637279e8511300901f732edb208c04b70425d6cd55ff31c39
|
|
| MD5 |
d86f721927ada4c240ae287237afc0c7
|
|
| BLAKE2b-256 |
b7ee1a78cb14fa0afec2a762acc4ae41a24b21f343f6fdf0452b4f10590e14e3
|
File details
Details for the file cs.cache-20240422-py3-none-any.whl.
File metadata
- Download URL: cs.cache-20240422-py3-none-any.whl
- Upload date:
- Size: 8.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.10.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fea10eb2e0a49cc8a62801a0ba29b3be8bc454c2cd8edbda7d47dd34d1874560
|
|
| MD5 |
abc6bafba9bcf8227de78418100fb4c8
|
|
| BLAKE2b-256 |
4423eee1d81978e4e227f01933663c86bffb44a440251ab6384fdb652ec768bc
|