Caching for class based generators
Project description
Class based cache
Installation
Recommended installation with pip:
pip install class-cache
Usage
-
Basic usage:
from class_cache import Cache # Create cache cache = Cache() # Set item in cache # NOTE: Keys and values have to be pickle-serialisable cache["foo"] = "bar" # Save cache to backend (disk by default) cache.write() # During another program run just create same cache again and you can retrieve data del cache cache2 = Cache() assert cache2["foo"] == "bar"
-
Use multiple caches:
cache1 = Cache(1) cache2 = Cache(2) cache1["foo"] = "bar" cache2["foo"] = "zar" assert cache1["foo"] != cache2["foo"]
-
Use cache with default factory:
from class_cache import CacheWithDefault class MyCache(CacheWithDefault[str, str]): NON_HASH_ATTRIBUTES = frozenset({*CacheWithDefault.NON_HASH_ATTRIBUTES, "_misc"}) def __init__(self, name: str): # Attributes which affect default value generation should come before super().__init__() self._name = name super().__init__() # Other attributes should not affect how default value is generated, add them to NON_HASH_ATTRIBUTES self._misc = "foo" # Define logic for defaults in _get_data def _get_data(self, key: str) -> str: return f"{self._name}_{key}" cache = MyCache("first") assert cache["foo"] == "first_foo"
Development
-
Install dev dependencies:
pip install -e ".[dev]"
-
For linting and basic fixes ruff is used:
ruff check . --fix
-
This repository follows strict formatting style which will be checked by the CI.
-
To test code, use pytest:
pytest .
-
This repository follows semantic-release, which means all commit messages have to follow a style. You can use tools like commitizen to write your commits.
-
You can also use pre-commit to help verify that all changes are valid. Multiple hooks are used, so use the following commands to install:
pre-commit install pre-commit install --hook-type commit-msg
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
Hashes for class_cache-0.2.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 13f9865179c9a48e86f0cbe04b5cb4c3729c8b511aa3bee52345d5966eaffb61 |
|
MD5 | ad8c93fdd67702e792c816aab912946c |
|
BLAKE2b-256 | f713f9b6b25e7f0fd89ca52eb45f5ff582c71b9d69ae40771d304b887cac1acf |