File-based caching for Python
Project description
Cachetta for Python
File-based caching for Python. Part of the Cachetta project, which provides the same caching API in Python and TypeScript -- learn it once, use it in either language.
Three doc layers: this README (overview), the docs/ folder bundled with this package, and the hosted docs site. Each ## below mirrors a section in docs/python.md.
Requires Python 3.12+.
Install
uv add cachetta
Basic Usage
from datetime import timedelta
from cachetta import Cachetta, read_cache, write_cache
cache = Cachetta(path='./cache.json', duration=timedelta(days=1))
with read_cache(cache) as data:
if data is None:
data = fetch_data()
write_cache(cache, data)
Decorators
@Cachetta(path='/my-cache.json')
def get_data():
return compute_expensive_value()
Async Support
@Cachetta(path='./async-cache.json')
async def get_async_data():
return await fetch_data()
I/O for decorated async functions runs via asyncio.to_thread(). Explicit primitives are async_read_cache / async_write_cache.
Function Wrapper
cache = Cachetta(path='./my-cache.json')
cached_get_data = cache(get_data)
result = cached_get_data()
Auto Cache Keys
Decorated functions hash their arguments to generate unique paths automatically.
In-Memory LRU
cache = Cachetta(path='./cache.json', lru_size=100)
Thread-safe for concurrent async access.
Conditional Caching
cache = Cachetta(path='./cache.json', condition=lambda r: r is not None)
Stale-While-Revalidate
cache = Cachetta(
path='./cache.json',
duration=timedelta(hours=1),
stale_duration=timedelta(minutes=30),
)
Cache Invalidation
cache.invalidate() # or cache.clear()
await cache.ainvalidate()
Cache Inspection
cache.exists() # bool
cache.age() # timedelta | None
cache.info() # dict
Async variants: aexists, aage, ainfo.
Path Operator
cache = Cachetta(path='./cache')
with read_cache(cache / 'my-data.json') as data:
...
Dynamic Cache Paths
@Cachetta(path=lambda n: f"./cache/{n}.json")
def foo(n: int):
...
Specifying Paths
new_cache = cache.copy(read=False, duration=timedelta(days=2))
Method Decorators
class DataService:
@Cachetta(path='./cache.json', skip_self=True)
def get_data(self, user_id):
...
Pickle Security
A restricted unpickler blocks arbitrary code execution from tampered cache files. Add custom types to allowed_pickle_types to whitelist them.
cache = Cachetta(path='./cache.dat', allowed_pickle_types={UserProfile})
Error Handling
read_cache yields None for missing or corrupt files.
Logging
import logging
logging.getLogger("cachetta").setLevel(logging.DEBUG)
Configuration Reference
| Option | Type | Default |
|---|---|---|
path |
str | Callable |
required |
read / write |
bool |
True |
duration |
timedelta |
7 days |
lru_size |
int |
None |
condition |
Callable |
None |
stale_duration |
timedelta |
None |
skip_self |
bool |
False |
allowed_pickle_types |
set[type] |
None |
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file cachetta-0.6.3.tar.gz.
File metadata
- Download URL: cachetta-0.6.3.tar.gz
- Upload date:
- Size: 31.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7a8a166f44e8c4374aceec39957508dbe657f1615f2774e31844f71886b04004
|
|
| MD5 |
ced7ace5a0221c36e56f16d8c4a02ee8
|
|
| BLAKE2b-256 |
6f02a7779a4feedc8c222e717a0ca5036f4b26ffbc654c1e00c7dfbf78a8cd3d
|
Provenance
The following attestation bundles were made for cachetta-0.6.3.tar.gz:
Publisher:
release.yaml on thekevinscott/cachetta
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cachetta-0.6.3.tar.gz -
Subject digest:
7a8a166f44e8c4374aceec39957508dbe657f1615f2774e31844f71886b04004 - Sigstore transparency entry: 1396990272
- Sigstore integration time:
-
Permalink:
thekevinscott/cachetta@89733ffe99a1e55244a3d7e2f14db59984301419 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/thekevinscott
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@89733ffe99a1e55244a3d7e2f14db59984301419 -
Trigger Event:
push
-
Statement type: