app\_json\_file\_cache
======================
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.
.. image:: https://img.shields.io/pypi/v/app-json-file-cache
:alt: PyPI
:target: https://pypi.org/project/app-json-file-cache/
.. image:: https://github.com/JohannesEbke/app_json_file_cache/actions/workflows/test.yaml/badge.svg
:target: https://github.com/JohannesEbke/app_json_file_cache/actions/workflows/test.yaml/
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.
Raw data
{
"_id": null,
"home_page": null,
"name": "app-json-file-cache",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": null,
"keywords": "xdg, json, cache, function cache, memoization, memoisation",
"author": null,
"author_email": "Johannes Ebke <johannes@ebke.org>",
"download_url": "https://files.pythonhosted.org/packages/15/2e/f083fe1004524297c1c400a99c3304252829ab9b433ef1f13f3e4666d1bb/app_json_file_cache-1.0.1.tar.gz",
"platform": null,
"description": "app\\_json\\_file\\_cache\n======================\n\nProvides a function decorator that caches the return value in a JSON file in the appropriate application cache directory.\n\nIt requires all function parameters and return values to be encodeable to JSON, so that the cache content\nis human-readable. The file names are based on a SHA512 hash of the function parameters.\n\nIt supports a \"vary\" guard value (e.g. a data model version) that protects against using old versions of cache.\n\nAdditionally, a cheaper default function (e.g. returning shipped, precalculated values) can be specified as a stand-in\nuntil an explicit recalculate function is called.\n\n.. image:: https://img.shields.io/pypi/v/app-json-file-cache\n :alt: PyPI\n :target: https://pypi.org/project/app-json-file-cache/\n\n.. image:: https://github.com/JohannesEbke/app_json_file_cache/actions/workflows/test.yaml/badge.svg\n :target: https://github.com/JohannesEbke/app_json_file_cache/actions/workflows/test.yaml/\n\n\nUsage\n-----\n\nExample usage::\n\n from app_json_file_cache import AppCache\n cache = AppCache(\"myapp\")\n\n @cache(\"expensive\")\n def expensive_function():\n return calculator()\n\nMore Example usage::\n\n from app_json_file_cache import AppCache\n cache = AppCache(\"myapp\")\n\n @cache(\"expensive\", vary=VERSION)\n def expensive_function(param):\n return calculator(param)\n\n expensive_function.recalculate(param1) # recalculate if external effects change\n\nUsing a default function until the next recalculate::\n\n from app_json_file_cache import AppCache\n cache = AppCache(\"myapp\")\n\n def cheap_standin(param):\n return estimate(param)\n\n @cache(\"expensive\", vary=VERSION, cheap_default_func=cheap_standin)\n def expensive_function(param):\n return calculator(param)\n\n expensive_function(param1) # cheap_standin is used\n expensive_function.recalculate(param1) # cache is updated with expensive_function\n expensive_function(param1) # cached value from expensive_function is used\n\nCaveats\n-------\n\n* Names must be unique per app. If you reuse names, chaos ensues.\n* Each set of function parameter values creates a new file. This may lead to too many files in a directory on some systems.\n* Mixing positional and keyword arguments is not supported\n* It's your responsibility that return values are serializable to JSON.\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Simple JSON cache for expensive functions, respecting user directories",
"version": "1.0.1",
"project_urls": {
"Homepage": "https://github.com/JohannesEbke/app_json_file_cache",
"Issues": "https://github.com/JohannesEbke/app_json_file_cache/issues",
"Repository": "https://github.com/JohannesEbke/app_json_file_cache.git"
},
"split_keywords": [
"xdg",
" json",
" cache",
" function cache",
" memoization",
" memoisation"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "10f8c6363879ae9ba513cbec95d67718c10c41689226a713b7288acac022b2c1",
"md5": "2f8451c81f1ea95dbf0a4ae28456179a",
"sha256": "751a382cf3e4a3a345f54e2a8edeb0b3613ea01e071707f18c124431998967ea"
},
"downloads": -1,
"filename": "app_json_file_cache-1.0.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "2f8451c81f1ea95dbf0a4ae28456179a",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 6289,
"upload_time": "2025-01-23T19:13:30",
"upload_time_iso_8601": "2025-01-23T19:13:30.714878Z",
"url": "https://files.pythonhosted.org/packages/10/f8/c6363879ae9ba513cbec95d67718c10c41689226a713b7288acac022b2c1/app_json_file_cache-1.0.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "152ef083fe1004524297c1c400a99c3304252829ab9b433ef1f13f3e4666d1bb",
"md5": "cf1dbdb5696821ec26f7e5a41119cfbf",
"sha256": "1ae47b28accbf58ba29697fdfb2d4ee4d1deaee1e8e470d4980943146acef173"
},
"downloads": -1,
"filename": "app_json_file_cache-1.0.1.tar.gz",
"has_sig": false,
"md5_digest": "cf1dbdb5696821ec26f7e5a41119cfbf",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 4864,
"upload_time": "2025-01-23T19:13:32",
"upload_time_iso_8601": "2025-01-23T19:13:32.498643Z",
"url": "https://files.pythonhosted.org/packages/15/2e/f083fe1004524297c1c400a99c3304252829ab9b433ef1f13f3e4666d1bb/app_json_file_cache-1.0.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-01-23 19:13:32",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "JohannesEbke",
"github_project": "app_json_file_cache",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "app-json-file-cache"
}