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.
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.
Metadata
Release files for app-json-file-cache 1.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| app_json_file_cache-1.0.1.tar.gz | 4.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| app_json_file_cache-1.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.2 kB
Release files / app_json_file_cache-1.0.1.tar.gz
| Download URL | app_json_file_cache-1.0.1.tar.gz |
|---|---|
| Size | 4.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1ae47b28accbf58ba29697fdfb2d4ee4d1deaee1e8e470d4980943146acef173
|
|
BLAKE2b-256 checksum How to use checksums |
152ef083fe1004524297c1c400a99c3304252829ab9b433ef1f13f3e4666d1bb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.1
|
Release files / app_json_file_cache-1.0.1-py3-none-any.whl
| Download URL | app_json_file_cache-1.0.1-py3-none-any.whl |
|---|---|
| Size | 6.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
751a382cf3e4a3a345f54e2a8edeb0b3613ea01e071707f18c124431998967ea
|
|
BLAKE2b-256 checksum How to use checksums |
10f8c6363879ae9ba513cbec95d67718c10c41689226a713b7288acac022b2c1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.1
|