A persistent, type-safe caching library for Python
Project description
Cachetout
A persistent, type-safe caching library for Python.
Installation
pip install cachetout
Usage
Cache class
from cachetout import Cache
# Create a cache (uses SQLite backend by default)
cache = Cache("my_app_cache")
# Store a value
cache.set("user:123", {"name": "Alice", "age": 30})
# Retrieve a value
user = cache.get("user:123", type=dict)
print(user) # {'name': 'Alice', 'age': 30}
# Delete a value
cache.delete("user:123")
cache decorator
from datetime import timedelta
from cachetout import cache
@cache(expires_in=timedelta(hours=1))
def fetch_user_data(user_id: int) -> dict:
# This function will only be called once per user_id per hour
print("Fetching data from API...")
return {"id": user_id, "data": "..."}
# First call: fetches from source
result1 = fetch_user_data(123)
# Second call with same args: returns cached result
result2 = fetch_user_data(123)
Serialisation
The library uses msgspec for serialisation, which respects the Python type hints:
from dataclasses import dataclass
from cachetout import Cache
@dataclass
class User:
id: int
name: str
email: str
cache = Cache("user_cache")
# Store a dataclass instance
user = User(id=1, name="Alice", email="alice@example.com")
cache.set("user_1", user)
# Retrieve with type hint
retrieved: User = cache.get("user_1", type=User)
print(retrieved.name) # "Alice"
Expiration
Set expiration when storing values:
from datetime import datetime, timedelta, UTC
from cachetout import Cache
cache = Cache("temp_cache")
# Expire in 1 hour
cache.set("temp_data", "value", expires_at=datetime.now(tz=UTC) + timedelta(hours=1))
# Or use timedelta with decorator
@cache(expires_in=timedelta(minutes=30))
def get_stock_price(symbol: str) -> float:
return fetch_from_api(symbol)
Development
Tests
$ uv run pytest
Linting
$ uv run ruff format
$ uv run ruff check
$ uv run ty check
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
cachetout-0.1.0.tar.gz
(3.4 kB
view details)
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 cachetout-0.1.0.tar.gz.
File metadata
- Download URL: cachetout-0.1.0.tar.gz
- Upload date:
- Size: 3.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.32 {"installer":{"name":"uv","version":"0.11.32","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
009126eddbd5b3fa21028b96564f1925aa809337a32e41fa7e59793e5b079f13
|
|
| MD5 |
f2b569b30cb9636d6d9aa01b28157195
|
|
| BLAKE2b-256 |
02afd6d9c05c4289c37d0ba44f8744520306b0bc0479f196646c8b9dea6d9fa0
|
File details
Details for the file cachetout-0.1.0-py3-none-any.whl.
File metadata
- Download URL: cachetout-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.32 {"installer":{"name":"uv","version":"0.11.32","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
18f4e98038f2ff5efccb98c7b3153aea64aa8b7c6c9e8e51bbc996500db24e48
|
|
| MD5 |
1cfd2e876ded3fc082acec4cb2e54850
|
|
| BLAKE2b-256 |
82e7fbf006e31d35a0145bb2ef62463647bfd3cf0663ffc0801e97a39dc348c3
|