Skip to main content

Caching results of operations on heavy file trees

Project description

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.

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.7 or higher. Just use pip for Python 3 (You have pip, right?) to install it and its dependencies:

python3 -m pip install fscacher

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

fscacher-0.4.4.tar.gz (35.3 kB view details)

Uploaded Source

Built Distribution

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

fscacher-0.4.4-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file fscacher-0.4.4.tar.gz.

File metadata

  • Download URL: fscacher-0.4.4.tar.gz
  • Upload date:
  • Size: 35.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for fscacher-0.4.4.tar.gz
Algorithm Hash digest
SHA256 742ab436ea698c3ef06e9d9cde81a5687bf2c2910019679a0d7f572d8ab97d79
MD5 0ab5e190565bf35398557678ad63bbb6
BLAKE2b-256 37b7d78437a034563d0b063d327d0bf9ac51854305ddf1f002ec54d18928e051

See more details on using hashes here.

File details

Details for the file fscacher-0.4.4-py3-none-any.whl.

File metadata

  • Download URL: fscacher-0.4.4-py3-none-any.whl
  • Upload date:
  • Size: 12.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for fscacher-0.4.4-py3-none-any.whl
Algorithm Hash digest
SHA256 f45632f318e979a7c27fc645b36bd633121fa443a50daf96de0fb9afe7c61b47
MD5 019b523abeb0c27e67abd3ecfd55a79c
BLAKE2b-256 d2f1816bb80e649ba7202cee656733ecea134370f9496c661fc16c129450bdbc

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