No project description provided
Project description
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!
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
Built Distribution
File details
Details for the file lc_cache-0.1.1.tar.gz
.
File metadata
- Download URL: lc_cache-0.1.1.tar.gz
- Upload date:
- Size: 5.4 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b78360953b26e92b7ea38a209f85c090d43e8b1ebe56abd16d0ac3ebc9516ae2 |
|
MD5 | 7edb2eb96c1bd7eb17e9c6b1a67e5efd |
|
BLAKE2b-256 | d83c8d6337f081d97f2cd0addf2946d0acbcd72da2c0e2be18e3f48ef71d50ac |
File details
Details for the file lc_cache-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: lc_cache-0.1.1-py3-none-any.whl
- Upload date:
- Size: 7.0 kB
- Tags: Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a1d707d060f5d4356fae3e805020d65898a0e08ae43f604e6372b72e0430ebde |
|
MD5 | f45abae5c90b47147841dfc5b55eadd3 |
|
BLAKE2b-256 | 5f505deb9280c3ee9d7e85c4a9c74cae5ed631d4c45c28b7f57be311493cee45 |