# 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
```bash
pip install twat-cache
```
## Usage
Basic usage with default memory caching:
```python
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:
```python
@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:
```python
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:
1. If `platformdirs` is available, uses the platform-specific user cache directory
2. Otherwise, falls back to `~/.cache`
You can get the cache path programmatically:
```python
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 directories
- `diskcache`: For SQL-based disk caching
- `joblib`: For efficient array caching
## License
MIT License
.
Raw data
{
"_id": null,
"home_page": null,
"name": "twat-cache",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": null,
"keywords": "cache, decorator, disk-cache, joblib, memory-cache",
"author": null,
"author_email": "Adam Twardoch <adam+github@twardoch.com>",
"download_url": "https://files.pythonhosted.org/packages/1e/28/51e865e486503f67924cb9936d9183e677c20640ee07daf08789d4c69116/twat_cache-1.8.1.tar.gz",
"platform": null,
"description": "# twat-cache\n\n(work in progress)\n\nA flexible caching utility package for Python functions that provides a unified interface for caching function results using various backends (memory, disk, SQL).\n\n## Features\n\n- Simple decorator interface for caching function results\n- Multiple caching backends:\n - Memory-based LRU cache (always available)\n - SQL-based disk cache using `diskcache` (optional)\n - Efficient array caching using `joblib` (optional)\n- Automatic cache directory management\n- Type hints and modern Python features\n\n## Installation\n\n```bash\npip install twat-cache\n```\n\n## Usage\n\nBasic usage with default memory caching:\n\n```python\nfrom twat_cache import ucache\n\n@ucache()\ndef expensive_computation(x):\n # Results will be cached automatically\n return x * x\n\nresult = expensive_computation(42) # Computed\nresult = expensive_computation(42) # Retrieved from cache\n```\n\nUsing SQL-based disk cache:\n\n```python\n@ucache(folder_name=\"my_cache\", use_sql=True)\ndef process_data(data):\n # Results will be cached to disk using SQL backend\n return data.process()\n```\n\nUsing joblib for efficient array caching:\n\n```python\nimport numpy as np\nfrom twat_cache import ucache\n\n@ucache(folder_name=\"array_cache\")\ndef matrix_operation(arr):\n # Large array operations will be efficiently cached\n return np.dot(arr, arr.T)\n```\n\n## Cache Location\n\nThe package automatically manages cache directories using the following strategy:\n\n1. If `platformdirs` is available, uses the platform-specific user cache directory\n2. Otherwise, falls back to `~/.cache`\n\nYou can get the cache path programmatically:\n\n```python\nfrom twat_cache import get_cache_path\n\ncache_dir = get_cache_path(\"my_cache\")\n```\n\n## Dependencies\n\n- Required: None (basic memory caching works without dependencies)\n- Optional:\n - `platformdirs`: For platform-specific cache directories\n - `diskcache`: For SQL-based disk caching\n - `joblib`: For efficient array caching\n\n## License\n\nMIT License \n.\n",
"bugtrack_url": null,
"license": null,
"summary": "Flexible caching utilities for Python functions",
"version": "1.8.1",
"project_urls": {
"Documentation": "https://github.com/twardoch/twat-cache#readme",
"Issues": "https://github.com/twardoch/twat-cache/issues",
"Source": "https://github.com/twardoch/twat-cache"
},
"split_keywords": [
"cache",
" decorator",
" disk-cache",
" joblib",
" memory-cache"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "0750d40d996cf9a6c683377ad91289fd97dcf0c34df5c25bad72c1f0cb033dc4",
"md5": "245473e7538372d13fbef261aac90784",
"sha256": "c3a2f79f53265e94966a5e9683a3a273ff976a737a084b7670ca593302648a66"
},
"downloads": -1,
"filename": "twat_cache-1.8.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "245473e7538372d13fbef261aac90784",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10",
"size": 10303,
"upload_time": "2025-02-15T05:50:16",
"upload_time_iso_8601": "2025-02-15T05:50:16.711960Z",
"url": "https://files.pythonhosted.org/packages/07/50/d40d996cf9a6c683377ad91289fd97dcf0c34df5c25bad72c1f0cb033dc4/twat_cache-1.8.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "1e2851e865e486503f67924cb9936d9183e677c20640ee07daf08789d4c69116",
"md5": "91ccad5f1a7b4d3428ed310443e1f943",
"sha256": "239b7cc4e1e8fd512cd720c0f62395121999e6e36b358ae5ff18410b4ccb973c"
},
"downloads": -1,
"filename": "twat_cache-1.8.1.tar.gz",
"has_sig": false,
"md5_digest": "91ccad5f1a7b4d3428ed310443e1f943",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 10868,
"upload_time": "2025-02-15T05:50:22",
"upload_time_iso_8601": "2025-02-15T05:50:22.388991Z",
"url": "https://files.pythonhosted.org/packages/1e/28/51e865e486503f67924cb9936d9183e677c20640ee07daf08789d4c69116/twat_cache-1.8.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-02-15 05:50:22",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "twardoch",
"github_project": "twat-cache#readme",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "twat-cache"
}