Skip to main content

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
del cache["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

Release files for cachetout 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cachetout 0.2.0
File Size Uploaded
cachetout-0.2.0.tar.gz 3.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cachetout 0.2.0
File Interpreter ABI Platform
cachetout-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 9.6 kB

Release files / cachetout-0.2.0.tar.gz

Download URL cachetout-0.2.0.tar.gz
Size 3.6 kB
Tags Source
SHA-256 checksum
How to use checksums
eee1c049fde5dd06c3a09360b52741f687cb806023dc7cc3fff8beda228e6b28
BLAKE2b-256 checksum
How to use checksums
645c4e23d82471d635cdd96a7b048d7301ff86d3a309929a35b6185ef7514c2c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.18 {"installer":{"name":"uv","version":"0.12.18","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}

Release files / cachetout-0.2.0-py3-none-any.whl

Download URL cachetout-0.2.0-py3-none-any.whl
Size 5.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1a48cb54ab929cd3eb1a5224b4a69fe9f91f164c471cb162db1434e0c67eafc1
BLAKE2b-256 checksum
How to use checksums
47e268a5b0a0711731ca36b5bceb9c6a8067a073e3a3e11c9337a72627571cb2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.18 {"installer":{"name":"uv","version":"0.12.18","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}

Release history Release notifications | RSS feed

0.3.1

2 release files

0.3.0

2 release files

This release

0.2.0 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page