Skip to main content

Simple user directory respecting JSON cache for expensive functions.

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 is human-readable.

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://travis-ci.org/JohannesEbke/app_json_file_cache.svg?branch=master

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-0.2.2.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

app_json_file_cache-0.2.2-py2.py3-none-any.whl (6.1 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

  • Download URL: app_json_file_cache-0.2.2.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2+

File hashes

Hashes for app_json_file_cache-0.2.2.tar.gz
Algorithm Hash digest
SHA256 02f7544c6f8bc4c6dd88548b73b8871624fbf3ff0a955b2f91b9986e89085060
MD5 1a3284128f5aa696b8af71700272b8f6
BLAKE2b-256 d8d150f448873605888a6039bb60b15ca12d7f91005f7afd425eb762e30c930c

See more details on using hashes here.

File details

Details for the file app_json_file_cache-0.2.2-py2.py3-none-any.whl.

File metadata

  • Download URL: app_json_file_cache-0.2.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2+

File hashes

Hashes for app_json_file_cache-0.2.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 1e70a51ca61d964d4dbd1c2adfb3312dbb83eb22e54b3aa095d191a59625eb44
MD5 273f17f545b0b83e91fbee45e7acbc1a
BLAKE2b-256 58805be4ba862d01eddd301daa92d61d846c8638b4b7ae7d3eb0a97c59213fb5

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page