Skip to main content

Simple JSON cache for expensive functions, respecting user directories

Project description

Provides a function decorator that caches the return value in a JSON file in the appropriate application cache directory.

It requires all function parameters and return values to be encodeable to JSON, so that the cache content is human-readable. The file names are based on a SHA512 hash of the function parameters.

It supports a “vary” guard value (e.g. a data model version) that protects against using old versions of cache.

Additionally, a cheaper default function (e.g. returning shipped, precalculated values) can be specified as a stand-in until an explicit recalculate function is called.

https://github.com/JohannesEbke/app_json_file_cache/actions/workflows/test.yaml/badge.svg

Usage

Example usage:

from app_json_file_cache import AppCache
cache = AppCache("myapp")

@cache("expensive")
def expensive_function():
    return calculator()

More Example usage:

from app_json_file_cache import AppCache
cache = AppCache("myapp")

@cache("expensive", vary=VERSION)
def expensive_function(param):
    return calculator(param)

expensive_function.recalculate(param1) # recalculate if external effects change

Using a default function until the next recalculate:

from app_json_file_cache import AppCache
cache = AppCache("myapp")

def cheap_standin(param):
    return estimate(param)

@cache("expensive", vary=VERSION, cheap_default_func=cheap_standin)
def expensive_function(param):
    return calculator(param)

expensive_function(param1) # cheap_standin is used
expensive_function.recalculate(param1) # cache is updated with expensive_function
expensive_function(param1) # cached value from expensive_function is used

Caveats

  • Names must be unique per app. If you reuse names, chaos ensues.

  • Each set of function parameter values creates a new file. This may lead to too many files in a directory on some systems.

  • Mixing positional and keyword arguments is not supported

  • It’s your responsibility that return values are serializable to JSON.

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

app_json_file_cache-1.0.0.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

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

app_json_file_cache-1.0.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file app_json_file_cache-1.0.0.tar.gz.

File metadata

  • Download URL: app_json_file_cache-1.0.0.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for app_json_file_cache-1.0.0.tar.gz
Algorithm Hash digest
SHA256 a402abe201b3f95d9bc52c0681bbf4ca34c7fd0350ebcf3d49cf62e74163a56d
MD5 973bf3c04f41582f5637b96854a8f38e
BLAKE2b-256 17b8eeb55b17779b515990389d350c8d3e4bb1e2f53a3c333b21d8b825926515

See more details on using hashes here.

File details

Details for the file app_json_file_cache-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for app_json_file_cache-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 67b699329c57e9bab612f2944e82aa3bc4ff7b3bc25f7261785f3a89f3936c35
MD5 f1115340ecf4816926a837400629eec0
BLAKE2b-256 506e2ad24a0394e6f96eb132dd727f7bb9b8f2525a42d3675981c799ec134266

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