A simple file-based caching system using hash-based file names
Project description
HashStash
HashStash is a versatile and efficient caching library for Python that supports multiple storage engines and encoding options. It provides a simple dictionary-like interface for caching data with various backend options.
Features
- Multiple storage engines: file, sqlite, memory, shelve, redis, pickledb, diskcache, and lmdb
- Compression and base64 encoding options
- Simple dictionary-like interface
- Efficient storage and retrieval of cached data
- Support for any JSON-serializable Python object
- Performance profiling tools
Installation
You can install HashStash using pip:
pip install hashstash
Usage
Here's a quick example of how to use HashStash:
from hashstash import HashStash
# Create a cache instance
cache = HashStash(name="my_cache", engine="pairtree", compress=True, b64=True)
# Store a value
cache["my_key"] = {"name": "John", "age": 30}
# Retrieve a value
data = cache["my_key"]
print(data) # Output: {'name': 'John', 'age': 30}
# Check if a key exists
if "my_key" in cache:
print("Key exists!")
# Get a value with a default
value = cache.get("non_existent_key", "default_value")
print(value) # Output: default_value
# Clear the cache
cache.clear()
Supported Engines
HashStash supports the following storage engines:
- File (
engine="pairtree"
) - SQLite (
engine="sqlite"
) - In-memory (
engine="memory"
) - Shelve (
engine="shelve"
) - Redis (
engine="redis"
) - PickleDB (
engine="pickledb"
) - DiskCache (
engine="diskcache"
) - LMDB (
engine="lmdb"
)
Each engine has its own characteristics and is suitable for different use cases. Choose the engine that best fits your needs.
API
HashStash(name: str = DEFAULT_NAME, engine: ENGINE_TYPES = DEFAULT_ENGINE_TYPE, *args, **kwargs)
Create a new HashStash instance.
name
: The name of the cache (default: "unnamed")engine
: The type of cache to create (one of the supported engines)*args
,**kwargs
: Additional arguments to pass to the cache constructor
Methods
HashStash implements the MutableMapping
interface, providing the following methods:
__setitem__(key: str, value: Any)
: Set an item in the cache__getitem__(key: str) -> Any
: Get an item from the cache__contains__(key: str) -> bool
: Check if a key exists in the cacheget(key: str, default: Any = None) -> Any
: Get an item with a default valueclear() -> None
: Clear all items from the cache__len__() -> int
: Return the number of items in the cache__iter__()
: Iterate over all keys in the cache
Additional methods:
keys()
: Return an iterator over the cache keysvalues()
: Return an iterator over the cache valuesitems()
: Return an iterator over the cache key-value pairsupdate(other=None, **kwargs)
: Update the cache with key-value pairs from another dictionary or keyword argumentssetdefault(key, default=None)
: Set a key with a default value if it doesn't exist, and return the valuepop(key, default=None)
: Remove and return the value for a key, or return the default if the key doesn't existpopitem()
: Remove and return a (key, value) pair from the cache
Performance Profiling
HashStash includes a performance profiling tool to help you evaluate different cache configurations. You can use it as follows:
from hashstash import HashStash
# Create a cache instance
cache = HashStash(name="profile_cache", engine="pairtree")
# Run the profiler
results = cache.profile(
engine=["pairtree", "sqlite", "memory"],
compress=[True, False],
b64=[True, False],
size=[1000, 10000, 100000],
iterations=5
)
# The results are returned as a pandas DataFrame
print(results)
For more detailed usage of the profiler, refer to the performance.py
file.
Utilities
HashStash provides various utility functions for encoding, serialization, and more. These utilities are used internally but can also be helpful for advanced users. Some key utilities include:
stashed_result
: A decorator for caching function resultsEncoder
andDecoder
classes for handling data encoding and decodingSerializer
andDeserializer
classes for object serialization
Running Tests
To run the tests, use the following command:
pytest
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the GNU License.
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 hashstash-0.1.8.tar.gz
.
File metadata
- Download URL: hashstash-0.1.8.tar.gz
- Upload date:
- Size: 350.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ee6875f3b9a3acc90dce4b900a7ef5b3e3544973676d9cf054fc7732d7d06fc5 |
|
MD5 | fedf8dd6af8274b3f167dc74dea26384 |
|
BLAKE2b-256 | be96ed45aea522de0fb0c68f8b5541f58f44478eb8457ec0f3a7492d321927eb |
File details
Details for the file hashstash-0.1.8-py3-none-any.whl
.
File metadata
- Download URL: hashstash-0.1.8-py3-none-any.whl
- Upload date:
- Size: 355.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | add5d05a64cdae6d71c15c1f9cac22a3d7b2a16f2f45ed3e8c616fa5a4d49f73 |
|
MD5 | 192429a19304deed8d4e577fee084229 |
|
BLAKE2b-256 | ad2fba9b06c693dc469ac505d974e3732c0ca587e4d5b224d684c2626e05c730 |