Skip to main content

Class based cache

tests codecov publish Python Version from PEP 621 TOML PyPI - Version Code Style: Ruff

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__()
            # They will be used to generate a unique id
            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"
    
  • Compress data before storing:

    from class_cache.wrappers import BrotliCompressWrapper
    from class_cache import Cache
    
    cache = BrotliCompressWrapper(Cache())
    # Use cache as normal
    

    This wrapper uses brotli algorithm for compression, which optimises read-time at expense of write-time. This will generally lead to less space being used and potentially faster reads if your data is compressible, e.g. text.

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 format the code, use the black format: black .
    • To sort the imports, user isort utility: isort .
  • 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
    

Metadata

Release files for class-cache 0.10.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for class-cache 0.10.1
File Size Uploaded
class_cache-0.10.1.tar.gz 14.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for class-cache 0.10.1
File Interpreter ABI Platform
class_cache-0.10.1-py3-none-any.whl Python 3 none any Details

Total release size: 25.9 kB

Release files / class_cache-0.10.1.tar.gz

Download URL class_cache-0.10.1.tar.gz
Size 14.2 kB
Tags Source
SHA-256 checksum
How to use checksums
71f307c0d419307466979cef9658d8b88ddb3ca352631e59e3a4c04c775b7b5c
BLAKE2b-256 checksum
How to use checksums
a2e01530ad3c5e21dece273f5b242d4eb3bc0c7d44a296172b0e2a2b09f1315b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.25.1

Release files / class_cache-0.10.1-py3-none-any.whl

Download URL class_cache-0.10.1-py3-none-any.whl
Size 11.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5dba48dc12eb0520fd327e8e008b5fe712960deb7e8a8c2430c8a020e838d761
BLAKE2b-256 checksum
How to use checksums
eab28ea0f6009e970caef06ce83e8c09fa11c2227d9037b2d3498e0033323e7b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.25.1

Release history Release notifications | RSS feed

This release

0.10.1 This release

2 release files

0.10.0

2 release files

0.9.0

2 release files

0.8.0

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page