Flexible caching utilities for Python functions
Project description
twat-cache
(work in progress)
A flexible caching utility package for Python functions that provides a unified interface for caching function results using various backends (memory, disk, SQL).
Features
- Simple decorator interface for caching function results
- Multiple caching backends:
- Memory-based LRU cache (always available)
- SQL-based disk cache using
diskcache(optional) - Efficient array caching using
joblib(optional)
- Automatic cache directory management
- Type hints and modern Python features
Installation
pip install twat-cache
Usage
Basic usage with default memory caching:
from twat_cache import ucache
@ucache()
def expensive_computation(x):
# Results will be cached automatically
return x * x
result = expensive_computation(42) # Computed
result = expensive_computation(42) # Retrieved from cache
Using SQL-based disk cache:
@ucache(folder_name="my_cache", use_sql=True)
def process_data(data):
# Results will be cached to disk using SQL backend
return data.process()
Using joblib for efficient array caching:
import numpy as np
from twat_cache import ucache
@ucache(folder_name="array_cache")
def matrix_operation(arr):
# Large array operations will be efficiently cached
return np.dot(arr, arr.T)
Cache Location
The package automatically manages cache directories using the following strategy:
- If
platformdirsis available, uses the platform-specific user cache directory - Otherwise, falls back to
~/.cache
You can get the cache path programmatically:
from twat_cache import get_cache_path
cache_dir = get_cache_path("my_cache")
Dependencies
- Required: None (basic memory caching works without dependencies)
- Optional:
platformdirs: For platform-specific cache directoriesdiskcache: For SQL-based disk cachingjoblib: For efficient array caching
License
MIT 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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file twat_cache-1.6.0.tar.gz.
File metadata
- Download URL: twat_cache-1.6.0.tar.gz
- Upload date:
- Size: 39.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
af44a2f18a0e05b67b33105478d9d3fe190533bee32737e4e9e94e63b6929456
|
|
| MD5 |
3088d93095e12a0d8b50de4e2a05b9c8
|
|
| BLAKE2b-256 |
31a036606b8bd2351ab43d4373d09fa7f16edfc407a1038178134b6eee947543
|
File details
Details for the file twat_cache-1.6.0-py3-none-any.whl.
File metadata
- Download URL: twat_cache-1.6.0-py3-none-any.whl
- Upload date:
- Size: 10.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
236e1d332c8cbdd867ac4bf592fd0820c2d769fd73eb9d1388e897ca59ead69e
|
|
| MD5 |
484af58ba9c47ea8eb27acb5f9cb6ff4
|
|
| BLAKE2b-256 |
6415a33cd51f52d0cc3e1278c880f68ee00f5400f988cbbbe108a70208534bf3
|