Skip to main content

cache-py

Overview

Caches are useful as temporary, fast-access datastores for API responses, data computed with complex calculations, and any other kind of data that's relatively expensive to retrieve/compute. cache-py is a Python library consisting of a simple cache interface (base class), implementations of common caches using this interface, and a general purpose hashing function for Python objects. All implementations are currently local and in-memory, though one could easily use the interface to implement file or API-based caches.

Installation

Install from PyPi (preferred method)

pip install lc-cache

Install from GitHub with Pip

pip install git+https://github.com/libcommon/cache-py.git@vx.x.x#egg=lc_cache

where x.x.x is the version you want to download.

Install by Manual Download

To download the source distribution and/or wheel files, navigate to https://github.com/libcommon/cache-py/tree/releases/vx.x.x/dist, where x.x.x is the version you want to install, and download either via the UI or with a tool like wget. Then to install run:

pip install <downloaded file>

Do not change the name of the file after downloading, as Pip requires a specific naming convention for installation files.

Dependencies

cache-py does not have external dependencies. Only Python versions >= 3.6 are officially supported.

Getting Started

If you simply need to store values and later check for their presence, but don't necessarily care about retrieving the values themselves, use the HashsetCache.

import random

from lc_cache import HashsetCache

def main() -> int:
    # Create the cache
    cache = HashsetCache()

    # Insert some random values into the cache
    for _ in range(10):
        cache.insert(random.randrange(0, 10))

    # Check for their presence
    for i in range(10):
        if cache.check(i):
            print("Encountered value", i)

    return 0

If you need to retrieve values later on, and have a key to associate them with, use the HashmapCache. Below is an implementation of functools.lru_cache, though it is simply a reference. Do not use this implementation as it is not thread-safe, and the functools version has some optimizations that likely make it faster.

from functools import wraps
from typing import Any, Callable

from lc_cache import HashmapCache, gen_python_hash

_CACHE = HashmapCache()

def lru_cache(f: Callable[..., Any]) -> Callable[..., Any]:
    @wraps(f)
    def wrapper(*args, **kwargs) -> Any:
        args_hash = hash((gen_python_hash(args), gen_python_hash(kwargs)))
        # NOTE: Have to use check here because a cached value may be None
        if _CACHE.check(args_hash):
            return _CACHE.get(args_hash)
        return f(*args, **kwargs)
    return wrapper

The SizedHashmapCache and SizedLRUCache classes may be useful in long-running processes where you want to cap the number of items in the cache.

Use Case

The cache interface class, Cache, defines a simple API for manipulating the underlying storage object(s):

* `check`: check if a value is present in the cache
* `insert`: add a value to the cache, or update an existing one
* `get`: get a value from the cache, if it exists
* `remove`: remove a value from the cache, if it exists
* `clear`: clear the cache
* `iter`: returns a [Generator](https://wiki.python.org/moin/Generators) over the values in the dictionary

Note that the word "value" here is used loosely, and does not refer to a value in a Python dictionary. That being said, Python dictionaries are commonly used to cache values. The HashmapCache has a regular Python dictionary as the underlying storage object, and thus can be used just like a dictionary. Similarly, The SizedHashmapCache and SizedLRUCache cache classes are also backed by dictionaries, but with some extra constraints on cache size and logic for removing items.

If your use case isn't complicated, and a simple dictionary will suffice, then there's no need to use classes from this library. However, if you need something like an LRU cache, or if you're developing an MVP but will need a more complex cache implementation later on (and thus want a consistent API), this library can be helpful.

Contributing/Suggestions

Contributions and suggestions are welcome! To make a feature request, report a bug, or otherwise comment on existing functionality, please file an issue. For contributions please submit a PR, but make sure to lint, type-check, and test your code before doing so. Thanks in advance!

Metadata

Release files for lc-cache 0.1.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 lc-cache 0.1.1
File Size Uploaded Provenance Actions
lc_cache-0.1.1.tar.gz 5.4 kB Without provenance Details for lc_cache-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for lc-cache 0.1.1
File Interpreter ABI Platform Actions
lc_cache-0.1.1-py3-none-any.whl Python 3 none any Details for lc_cache-0.1.1-py3-none-any.whl

Total release size: 12.4 kB

Release files / lc_cache-0.1.1.tar.gz

Download URL lc_cache-0.1.1.tar.gz
Size 5.4 kB
Tags Source
SHA-256 checksum
How to use checksums
b78360953b26e92b7ea38a209f85c090d43e8b1ebe56abd16d0ac3ebc9516ae2
BLAKE2b-256 checksum
How to use checksums
d83c8d6337f081d97f2cd0addf2946d0acbcd72da2c0e2be18e3f48ef71d50ac
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7

Release files / lc_cache-0.1.1-py3-none-any.whl

Download URL lc_cache-0.1.1-py3-none-any.whl
Size 7.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a1d707d060f5d4356fae3e805020d65898a0e08ae43f604e6372b72e0430ebde
BLAKE2b-256 checksum
How to use checksums
5f505deb9280c3ee9d7e85c4a9c74cae5ed631d4c45c28b7f57be311493cee45
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7

Security for lc-cache 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

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