Skip to main content

A decorator to cache method call results with similar parameters

Project description

sag_py_cache_decorator

Maintainability Coverage Status Known Vulnerabilities

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 Mypy
  • Configure the python interpreter/venv
  • pip install requirements-dev.txt
  • 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

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

sag_py_cache_decorator-0.3.4.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sag_py_cache_decorator-0.3.4-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file sag_py_cache_decorator-0.3.4.tar.gz.

File metadata

  • Download URL: sag_py_cache_decorator-0.3.4.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sag_py_cache_decorator-0.3.4.tar.gz
Algorithm Hash digest
SHA256 8aa752f01ddb4da35d4b7be60b9a042c116295a2b98a2f715de9ab6aad87a9b3
MD5 b6421163e978046e73cfcbdd1c7e4c4d
BLAKE2b-256 e150c374e60c99dc9d35a6d54b128fb680d53f33497fafeda0fc9e74d7d947c8

See more details on using hashes here.

File details

Details for the file sag_py_cache_decorator-0.3.4-py3-none-any.whl.

File metadata

File hashes

Hashes for sag_py_cache_decorator-0.3.4-py3-none-any.whl
Algorithm Hash digest
SHA256 5928d1cdd3e7d71bf7735f80ccdbcdc312fa5b0bba79d4b61ba499f9a8596429
MD5 cae9a5b7c64bc215ae2eaf7546970bc6
BLAKE2b-256 e331bbb2130ee7ca956f1573cf792e4f600ffcc193a3e6a720c9511e623e661e

See more details on using hashes here.

Supported by

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