Skip to main content

No project description provided

Project description

disklru

pip install disklru

Creates a disk based lru (least recently used) cache, backed by sqlite, that you can use in your apps.

Zero dependency package. Only relies on the python standard lib. Cross platform tests.

Linting

MacOS_Tests Ubuntu_Tests Win_Tests

Usage

from disklru import DiskLRUCache

LRU_CACHE_FILE = "cache.db"
MAX_ENTRIES = 4
cache = DiskLRUCache(LRU_CACHE_FILE, MAX_ENTRIES)
cache.put("key", "value")
assert cache.get("key1") == "val"
cache.clear()

API

class DiskLRUCache:
    """Disk-based LRU cache using SQLite."""

    def get(self, key: str) -> str | None:
        """Returns the value associated with the given key, or None if the key is not in the cache."""

    def compare_and_swap(self, key: str, prev_val: str, new_val: str) -> tuple[bool, str | None]:
        """Performs compare and swap"""

    def get_bytes(self, key: str) -> bytes | None:
        """Returns the bytes values associated with the given key"""

    def get_json(self, key: str) -> Any:
        """Returns the value associated with the given key, or None if the key is not in the cache."""

    def put(self, key: str, value: str) -> None:
        """Sets the value associated with the given key."""

    def put_bytes(self, key: str, value: bytes) ->: None:
        """Sets the byte value associated with the given key."""

    def put_json(self, key: str, val: Any) -> None:
        """Sets the value associated with the given key."""

    def delete(self, key) -> None:
        """Deletes the given key from the cache."""

    def purge(self, timestamp) -> None:
        """Purges all elements less than the timestamp."""

    def clear(self) -> None:
        """Clears the cache."""

    def __del__(self) -> None:
        """Destructor."""
        self.close()

    def close(self) -> None:
        """Closes the connection to the database."""

Development

First install development dependencies:

pip install -e ".[dev]"

Windows

This environment requires you to use git-bash.

Linting

Run ./lint.sh to find linting errors using pylint, flake8, mypy and other tools.

Releases

  • 2.0.3 - Added compare_and_swap() for atomic swapping of strings.
  • 2.0.2 - contains operator is now defined so that you can do "key" in disklur
  • 2.0.1 - max_size is now max_entries
  • 2.0.0 - Overhaul - now allows multithreaded access, connection pool, get/put bytes. purge() now takes in a timestamp aware value for purging.

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

disklru-2.0.3.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

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

disklru-2.0.3-py2.py3-none-any.whl (6.5 kB view details)

Uploaded Python 2Python 3

File details

Details for the file disklru-2.0.3.tar.gz.

File metadata

  • Download URL: disklru-2.0.3.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.5

File hashes

Hashes for disklru-2.0.3.tar.gz
Algorithm Hash digest
SHA256 1c00f7420da9bd20adf1a496d1ae8de62f6422897c1c049e34eb5f8e6caa0aea
MD5 80a9b9e128aa18f07bf1bec773900d5b
BLAKE2b-256 0928166b9daf81f8ee830c0a68dcbcc5ea5752c9c2b37f8826f4d1c88db00943

See more details on using hashes here.

File details

Details for the file disklru-2.0.3-py2.py3-none-any.whl.

File metadata

  • Download URL: disklru-2.0.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.5

File hashes

Hashes for disklru-2.0.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 4031191e3bdc19c24bb5a27c2c76db0472e90e4cec441337a669de0c40218c53
MD5 125d97b353a936f8b6f52548734239b7
BLAKE2b-256 e6ea5f9fece773de5c49dd097157a599387ac4891f7e75922e4dec9de864de1b

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