File system based caching
Project description
fscacher
Caching solution for functions that operate on the file system.
Installation
pip install <path to repository>
Usage
Simple usage:
from fscacher import Cache
cache = Cache(cache_path)
fn = cache.memoize(expensive_fn)
result = fn("arg1", "arg2")
When fn
is called the first time, the function expensive_fn
is evaluated
and its return value is serialized and stored at cache_path
. Subsequent
calls to fn
deserializes the stored result instead of re-evaluating
expensive_fn
.
Optional arguments to memoize
include:
key
: Function with arguments(func, args, kwargs)
and return value of typestr
, used to create the function call signaturedump
: Function with arguments(return_value, filename)
and no return value for serializing the result ofexpensive_fn
load
: Function with arguments(filename, )
for de-serializing the binary data on disk as a return valuedigest
: Function with arguments(stream, )
and return value of typestr
for digesting function call signature as well as the contents of serialized filesprotocol
: Use predefined functions forkey
,dump
,load
anddigest
. A list of known protocol schemes are presented below. If both protocol and explicit functions are set, the explicit functions takes presedence.
If any of the arguments key
, dump
, load
or digest
are set to
"default"
, the functions defined by the cache.defaults
dict are used
instead.
Default key function is constructed as follows:
- Arguments and keyword arguments (both keys and values) are converted to
string by the
str
function. Numpy arrays are converted to lists before conversion. - Any arguments which are too long (> 22 chars) or contain invalid characters
(
= \/:*?"<>|
) are utf-8 encoded and converted to a 64-bit truncated sha256 hash. - Keyword arguments are joined as key-value pairs of the form
k=v
. - If short enough, the key is the function name followed by the space-separated argument list. If too long, the key is the function name followed by the full sha256 hash of the space-separated argument list.
Default dump and load functions are from the python pickle module.
Default digest function is sha256.
Implemented protocols include:
filename/<suffix>
: Return value is interpreted as the name of a temporary file, which should have the suffix<suffix>
, including any leading dot.key
is default key, except that<suffix>
is appended.dump
moves the file to the index location.load
returns the file name as a string.filehash/<suffix>
: Return value is interpreted as the name of a temporary file, which should have the suffix<suffix>
, including any leading dot.key
is default key with the suffix.hash
appended.dump
computes the hash of the temporary file and renames it to the hash value (unless already present). Thereafter, it copies the file (as a hardlink if possible) to the location specified bykey
, except that.hash
is replaced by<suffix>
. Finally, the hash is stored as a string (lowercase hex) at the location specified bykey
(the index location).load
returns the contents of the file at the index location and interprets it as apathlib
path. In the end, this protocol works likefilename/<suffix>
, except that multiple function calls can be mapped to the same file if their return value has equal contents.
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
File details
Details for the file semantic-fscacher-0.3.0.tar.gz
.
File metadata
- Download URL: semantic-fscacher-0.3.0.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | aacb07381b81c4a01d9793d7b70a28f844d60ab3487c9e4f6a79c1bfdc6c5a37 |
|
MD5 | 043db98f0a84a15e89218bcbc8651479 |
|
BLAKE2b-256 | 21c2813c96b57b8f5667ccb9e5d25596b42d3b9d32fd6394d37cbe11e931baeb |
File details
Details for the file semantic_fscacher-0.3.0-py3-none-any.whl
.
File metadata
- Download URL: semantic_fscacher-0.3.0-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 55fb294956afd9be2b351c1f571cda56250ef8e8c5d572a4cddba24b9de7a333 |
|
MD5 | 0fe4783cbf90cf7860f9db0ab8b1624f |
|
BLAKE2b-256 | d7df7007f848002f48513d9445527722f94e7da9bba65343875ccf9923c8ef21 |