A decorator to cache method call results with similar parameters
Project description
sag_py_cache_decorator
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
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 |
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file sag-py-cache-decorator-0.0.3253.1.tar.gz
.
File metadata
- Download URL: sag-py-cache-decorator-0.0.3253.1.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2a758400b5bd36e98bc1b2c111ed61c906dd0c883a40e6afd297b8a9a81d4335 |
|
MD5 | fdb855da7a16d84cc0b92fcb6248fded |
|
BLAKE2b-256 | 922b524ee7770a376d898e38f0b4250242d1fd121ddb3e82c5049e39eb161786 |
File details
Details for the file sag_py_cache_decorator-0.0.3253.1-py3-none-any.whl
.
File metadata
- Download URL: sag_py_cache_decorator-0.0.3253.1-py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ee76d0d5bb9a0c01cfbd71b38de0f5d6094b713be64694b6bdf36565aa13ab17 |
|
MD5 | 2abeae07b85aac3ef501d328a506af04 |
|
BLAKE2b-256 | ca3f23b8cdf7437824215540eb2b3824f296bf3c96f7babb9a39b2274192e39a |