# sag_py_cache_decorator
[![Maintainability][codeclimate-image]][codeclimate-url]
[![Coverage Status][coveralls-image]][coveralls-url]
[![Known Vulnerabilities][snyk-image]][snyk-url]
A cache annotation that can be used to cache calls to a method
## What it does
- Caches calls to methods if the same parameters are used
- Removes the least recently used cache item if a optional maximum is reached
- Supports sync and async functions
- Possibility to skip the cache by parameter
- Possibility to clear the cache entirely or for one set of parameters
### Installation
pip install sag-py-cache-decorator
## How to use
```python
from sag_py_cache_decorator.lru_cache import lru_cache
@lru_cache(maxsize=3)
def my_function(str: str, str2: str) -> str:
return f"{str}-{str2}"
```
This is the regular use case of the cache.
Available decorator arguments:
| Argument | Description | Default |
|----------|----------------------------------------------------------------------------------------------------------------------------|---------|
| maxsize | If this size is reached, the least recently used cache item will be removed. Can be set to None to have a unlimited cache. | 128 |
```python
from sag_py_cache_decorator.lru_cache import lru_cache
@lru_cache(maxsize=3)
def my_function(
str: str,
lru_clear_cache: bool = False,
) -> str:
return f"{str}-{str2}"
my_function("one")
my_function("two")
# Before executing the next function the cache is cleared and then
# rebuilt with the results of three and four because of lru_clear_cache = True
my_function("three", lru_clear_cache = True)
my_function("four")
```
Available function arguments:
| Argument | Description | Default |
|---------------------|---------------------------------------------------------------------------------------------------------------------------------------|---------|
| lru_use_cache | If set to false, the function call skips the cache. Existing cached items are ignored and new ones are not written for that call. | True |
| lru_clear_cache | If set to true, the cache is cleared entirely before executing the method. The result of the call is then cached again. | False |
| lru_clear_arg_cache | If set to true, the result for this set of parameters is removed from cache(if present). The result of the call is then cached again. | False |
## How to start developing
### With vscode
Just install vscode with dev containers extension. All required extensions and configurations are prepared automatically.
### With pycharm
* Install latest pycharm
* Install pycharm plugin BlackConnect
* Install pycharm plugin Mypy
* Configure the python interpreter/venv
* pip install requirements-dev.txt
* pip install black[d]
* Ctl+Alt+S => Check Tools => BlackConnect => Trigger when saving changed files
* Ctl+Alt+S => Check Tools => BlackConnect => Trigger on code reformat
* Ctl+Alt+S => Click Tools => BlackConnect => "Load from pyproject.yaml" (ensure line length is 120)
* Ctl+Alt+S => Click Tools => BlackConnect => Configure path to the blackd.exe at the "local instance" config (e.g. C:\Python310\Scripts\blackd.exe)
* Ctl+Alt+S => Click Tools => Actions on save => Reformat code
* Restart pycharm
## How to publish
* Update the version in setup.py and commit your change
* Create a tag with the same version number
* Let github do the rest
## How to test
To avoid publishing to pypi unnecessarily you can do as follows
* Tag your branch however you like
* Use the chosen tag in the requirements.txt-file of the project you want to test this library in, eg. `sag_py_cache_decorator==<your tag>`
* Rebuild/redeploy your project
[codeclimate-image]:https://api.codeclimate.com/v1/badges/e29dcd8f76877962c93b/maintainability
[codeclimate-url]:https://codeclimate.com/github/SamhammerAG/sag_py_cache_decorator/maintainability
[coveralls-image]:https://coveralls.io/repos/github/SamhammerAG/sag_py_cache_decorator/badge.svg?branch=master
[coveralls-url]:https://coveralls.io/github/SamhammerAG/sag_py_cache_decorator?branch=master
[snyk-image]:https://snyk.io/test/github/SamhammerAG/sag_py_cache_decorator/badge.svg
[snyk-url]:https://snyk.io/test/github/SamhammerAG/sag_py_cache_decorator
Raw data
{
"_id": null,
"home_page": "https://github.com/SamhammerAG/sag_py_cache_decorator",
"name": "sag-py-cache-decorator",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.12",
"maintainer_email": null,
"keywords": "cache, decorator",
"author": "Samhammer AG",
"author_email": "support@samhammer.de",
"download_url": "https://files.pythonhosted.org/packages/1d/bf/69f8dc1d966742c5beb913e07fffcf254a2fcc40f64f34b21c6696ca6c38/sag_py_cache_decorator-0.3.0.tar.gz",
"platform": null,
"description": "# sag_py_cache_decorator\n[![Maintainability][codeclimate-image]][codeclimate-url]\n[![Coverage Status][coveralls-image]][coveralls-url]\n[![Known Vulnerabilities][snyk-image]][snyk-url]\n\nA cache annotation that can be used to cache calls to a method\n\n## What it does\n- Caches calls to methods if the same parameters are used\n- Removes the least recently used cache item if a optional maximum is reached\n- Supports sync and async functions\n- Possibility to skip the cache by parameter\n- Possibility to clear the cache entirely or for one set of parameters\n\n### Installation\npip install sag-py-cache-decorator\n\n## How to use\n```python\nfrom sag_py_cache_decorator.lru_cache import lru_cache\n\n@lru_cache(maxsize=3)\ndef my_function(str: str, str2: str) -> str:\n return f\"{str}-{str2}\"\n```\n\nThis is the regular use case of the cache.\n\nAvailable decorator arguments:\n\n| Argument | Description | Default |\n|----------|----------------------------------------------------------------------------------------------------------------------------|---------|\n| maxsize | If this size is reached, the least recently used cache item will be removed. Can be set to None to have a unlimited cache. | 128 |\n\n```python\nfrom sag_py_cache_decorator.lru_cache import lru_cache\n\n@lru_cache(maxsize=3)\ndef my_function(\n str: str,\n lru_clear_cache: bool = False,\n) -> str:\n return f\"{str}-{str2}\"\n\nmy_function(\"one\")\nmy_function(\"two\")\n# Before executing the next function the cache is cleared and then\n# rebuilt with the results of three and four because of lru_clear_cache = True\nmy_function(\"three\", lru_clear_cache = True)\nmy_function(\"four\")\n```\nAvailable function arguments:\n| Argument | Description | Default |\n|---------------------|---------------------------------------------------------------------------------------------------------------------------------------|---------|\n| lru_use_cache | If set to false, the function call skips the cache. Existing cached items are ignored and new ones are not written for that call. | True |\n| lru_clear_cache | If set to true, the cache is cleared entirely before executing the method. The result of the call is then cached again. | False |\n| lru_clear_arg_cache | If set to true, the result for this set of parameters is removed from cache(if present). The result of the call is then cached again. | False |\n\n## How to start developing\n\n### With vscode\n\nJust install vscode with dev containers extension. All required extensions and configurations are prepared automatically.\n\n### With pycharm\n\n* Install latest pycharm\n* Install pycharm plugin BlackConnect\n* Install pycharm plugin Mypy\n* Configure the python interpreter/venv\n* pip install requirements-dev.txt\n* pip install black[d]\n* Ctl+Alt+S => Check Tools => BlackConnect => Trigger when saving changed files\n* Ctl+Alt+S => Check Tools => BlackConnect => Trigger on code reformat\n* Ctl+Alt+S => Click Tools => BlackConnect => \"Load from pyproject.yaml\" (ensure line length is 120)\n* Ctl+Alt+S => Click Tools => BlackConnect => Configure path to the blackd.exe at the \"local instance\" config (e.g. C:\\Python310\\Scripts\\blackd.exe)\n* Ctl+Alt+S => Click Tools => Actions on save => Reformat code\n* Restart pycharm\n\n## How to publish\n* Update the version in setup.py and commit your change\n* Create a tag with the same version number\n* Let github do the rest\n\n## How to test\n\nTo avoid publishing to pypi unnecessarily you can do as follows\n\n* Tag your branch however you like\n* Use the chosen tag in the requirements.txt-file of the project you want to test this library in, eg. `sag_py_cache_decorator==<your tag>`\n* Rebuild/redeploy your project\n\n[codeclimate-image]:https://api.codeclimate.com/v1/badges/e29dcd8f76877962c93b/maintainability\n[codeclimate-url]:https://codeclimate.com/github/SamhammerAG/sag_py_cache_decorator/maintainability\n[coveralls-image]:https://coveralls.io/repos/github/SamhammerAG/sag_py_cache_decorator/badge.svg?branch=master\n[coveralls-url]:https://coveralls.io/github/SamhammerAG/sag_py_cache_decorator?branch=master\n[snyk-image]:https://snyk.io/test/github/SamhammerAG/sag_py_cache_decorator/badge.svg\n[snyk-url]:https://snyk.io/test/github/SamhammerAG/sag_py_cache_decorator\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A decorator to cache method call results with similar parameters",
"version": "0.3.0",
"project_urls": {
"Bug Reports": "https://github.com/SamhammerAG/sag_py_cache_decorator/issues",
"Documentation": "https://github.com/SamhammerAG/sag_py_cache_decorator",
"Homepage": "https://github.com/SamhammerAG/sag_py_cache_decorator",
"Source": "https://github.com/SamhammerAG/sag_py_cache_decorator"
},
"split_keywords": [
"cache",
" decorator"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "0de8432e3892de2c24a0016d8a13d429ae9b91096edb9e1adad893d2a61a4581",
"md5": "797d5d5a4e94a024b69970695f1ff210",
"sha256": "4a133543f6ab440cfeb27e9089e0e701e75f305844a806922eaccc5d08e99d70"
},
"downloads": -1,
"filename": "sag_py_cache_decorator-0.3.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "797d5d5a4e94a024b69970695f1ff210",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.12",
"size": 6589,
"upload_time": "2024-09-17T13:57:50",
"upload_time_iso_8601": "2024-09-17T13:57:50.313358Z",
"url": "https://files.pythonhosted.org/packages/0d/e8/432e3892de2c24a0016d8a13d429ae9b91096edb9e1adad893d2a61a4581/sag_py_cache_decorator-0.3.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "1dbf69f8dc1d966742c5beb913e07fffcf254a2fcc40f64f34b21c6696ca6c38",
"md5": "54dcdff90a837f3f0425293411e36031",
"sha256": "13e3494d942a5b90a607dc9494fe8fb67c40dbaafc81d4d1c025499281712d52"
},
"downloads": -1,
"filename": "sag_py_cache_decorator-0.3.0.tar.gz",
"has_sig": false,
"md5_digest": "54dcdff90a837f3f0425293411e36031",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.12",
"size": 7549,
"upload_time": "2024-09-17T13:57:51",
"upload_time_iso_8601": "2024-09-17T13:57:51.681831Z",
"url": "https://files.pythonhosted.org/packages/1d/bf/69f8dc1d966742c5beb913e07fffcf254a2fcc40f64f34b21c6696ca6c38/sag_py_cache_decorator-0.3.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-09-17 13:57:51",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "SamhammerAG",
"github_project": "sag_py_cache_decorator",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [],
"lcname": "sag-py-cache-decorator"
}