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.10+.
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()
Per-Argument Cache Files
Pass a callable path to vary the cache file by argument. A str/Path path is used verbatim regardless of arguments.
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() # delete the resolved cache file unconditionally
await cache.ainvalidate()
Clearing the Cache
cache.clear() # sweep dead entries (past duration + stale_duration)
cache.clear(force=True) # remove the whole path, folder and all
await cache.aclear()
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:
...
# Subfolder for auto-hashed entries
sub = cache / 'llm-calls'
# Callable for custom layouts (resolved at call time)
custom = cache / (lambda kind, ident: f'{kind}/{ident}.pkl')
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
Methods work as-is; the receiver (self/cls) is not part of the cache key.
class DataService:
@Cachetta(path=lambda user_id: f'./cache/{user_id}.json')
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 |
condition |
Callable |
None |
stale_duration |
timedelta |
None |
allowed_pickle_types |
set[type] |
None |
Metadata
Release files for cachetta 0.7.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| cachetta-0.7.0.tar.gz | 26.7 kB | With provenance | Details |
Release files / cachetta-0.7.0.tar.gz
| Download URL | cachetta-0.7.0.tar.gz |
|---|---|
| Size | 26.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c14a41245343d27921029e2df523e4250c56690f153b552425cb16f891aa563b
|
|
BLAKE2b-256 checksum How to use checksums |
26a67eb353e8a2eb17f821e9dc61c3b053309c8ce757a5a4538f8965975f3120
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 4, 2026.
Transparency logSecurity for cachetta 0.7.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 1 file in this release has been uploaded with provenance metadata.
Attestation for cachetta-0.7.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 4, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.