Skip to main content
CI Status https://codecov.io/gh/con/fscacher/branch/master/graph/badge.svg https://img.shields.io/pypi/pyversions/fscacher.svg MIT License

GitHub | PyPI | Issues | Changelog

fscacher provides a cache & decorator for memoizing functions whose outputs depend upon the contents of a file argument.

If you have a function foo() that takes a file path as its first argument, and if the behavior of foo() is pure in the contents of the path and the values of its other arguments, fscacher can help cache that function, like so:

from fscacher import PersistentCache

cache = PersistentCache("insert_name_for_cache_here")

@cache.memoize_path
def foo(path, ...):
    ...

Now the outputs of foo() will be cached for each set of input arguments and for a “fingerprint” (timestamps & size) of each path. If foo() is called twice with the same set of arguments, the result from the first call will be reused for the second, unless the file pointed to by path changes, in which case the function will be run again. If foo() is called with a non-path-like object as the value of path, the cache is ignored.

memoize_path() optionally takes an exclude_kwargs argument, which must be a sequence of names of arguments of the decorated function that will be ignored for caching purposes.

memoize_path() can also take a custom_fingerprint callable to use instead of stat(); it returns None to fall back to stat(). For example, fscacher.annex_key_fingerprint fingerprints locked git-annex’ed files by their keys, so results survive their content being dropped:

from fscacher import annex_key_fingerprint

@cache.memoize_path(custom_fingerprint=annex_key_fingerprint)
def foo(path, ...):
    ...

Caches are stored on-disk and thus persist between Python runs. To clear a given PersistentCache and erase its data store, call the clear() method.

By default, caches are stored in the user-wide cache directory, under an fscacher-specific folder, with each one identified by the name passed to the constructor (which defaults to “cache” if not specified). To specify a different location, use the path argument to the constructor instead of passing a name:

cache = PersistentCache(path="/my/custom/location")

If your code runs in an environment where different sets of libraries or the like could be used in different runs, and these make a difference to the output of your function, you can make the caching take them into account by passing a list of library version strings or other identifiers for the current run as the token argument to the PersistentCache constructor.

Finally, PersistentCache’s constructor also optionally takes an envvar argument giving the name of an environment variable. If that environment variable is set to “clear” when the cache is constructed, the cache’s clear() method will be called at the end of initialization. If the environment variable is set to “ignore” instead, then caching will be disabled, and the cache’s memoize_path method will be a no-op. If the given environment variable is not set, or if envvar is not specified, then PersistentCache will query the FSCACHER_CACHE environment variable instead.

Installation

fscacher requires Python 3.9 or higher. Just use pip for Python 3 (You have pip, right?) to install it and its dependencies:

python3 -m pip install fscacher

Metadata

Release files for fscacher 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fscacher 0.5.0
File Size Uploaded
fscacher-0.5.0.tar.gz 43.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fscacher 0.5.0
File Interpreter ABI Platform
fscacher-0.5.0-py3-none-any.whl Python 3 none any Details

Total release size: 62.0 kB

Release files / fscacher-0.5.0.tar.gz

Download URL fscacher-0.5.0.tar.gz
Size 43.8 kB
Tags Source
SHA-256 checksum
How to use checksums
414afda5a98bb4f84a1c8e159d0cee3db254139cd832eec2dd74fa5124e92d14
BLAKE2b-256 checksum
How to use checksums
57ea5a9be8cf5bfa19e9cab85993dd961c80f41f7ae169cf1f7f44656945c790
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.5

Release files / fscacher-0.5.0-py3-none-any.whl

Download URL fscacher-0.5.0-py3-none-any.whl
Size 18.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
26ac128c7544895d46ccec0ba0d032069cd5b706d673448c75b1ce3cc1f046be
BLAKE2b-256 checksum
How to use checksums
00fae316e47fb3b5003f14544aa7dfc3aaccc9c05bcffb22b58ac82e01b2aca0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.5

Release history Release notifications | RSS feed

This release

0.5.0 This release

2 release files

0.4.4

2 release files

0.4.3

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page