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"
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
class_cache-0.2.0.tar.gz
(6.3 kB
view hashes)
Built Distribution
Close
Hashes for class_cache-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e558ee8537f179f67b850ed42174f0c9a7c78eefdab9c84eb8ea5df1dbde912d |
|
MD5 | 6f4e6c16cf8723c446190d7ba9024712 |
|
BLAKE2b-256 | 9b0a2de0f9e165fa6549760368191ffe0050431773e372ab3135e150b1f8cd8a |