Skip to content
This repository has been archived by the owner on Apr 17, 2024. It is now read-only.

phi-friday/diskcache-fernet

Repository files navigation

diskcache-fernet

License: Apache-2.0 github action PyPI version python version

how to install

$ pip install diskcache-fernet

how to use

from __future__ import annotations

from pathlib import Path
from pickle import UnpicklingError
from tempfile import TemporaryDirectory

from diskcache import Cache

from diskcache_fernet import FernetDisk


def main(temp: Path) -> None:
    origin = Cache(temp)
    fernet = Cache(temp, disk=FernetDisk)
    # or add fernet key
    # fernet = Cache(temp, disk=FernetDisk, disk_fernet=b"some fernet key")

    fernet["string"] = "value"
    fernet["pickle"] = {"key": "value"}

    from_fernet_string = fernet["string"]
    from_origin_string = origin["string"]

    assert from_fernet_string != from_origin_string
    assert from_fernet_string == "value"

    print(from_origin_string)
    # like:
    # gAAAAABlGtPWAPEcYLqu6waiUd551H4jfAvQlulWnfwyWTVtjZyF6AkUCVFQKPpIRz9vu29y1FoduIYoK-mOz5CJt0Kx-pv2zQ==

    from_fernet_pickle = fernet["pickle"]
    assert from_fernet_pickle == {"key": "value"}
    try:
        origin["pickle"]
    except Exception as exc:
        assert isinstance(exc, UnpicklingError)


if __name__ == "__main__":
    with TemporaryDirectory() as temp:
        main(Path(temp))

License

Apache-2.0, see LICENSE.