Pre-release
This release is a pre-release and may not be stable for production use.
cachium
Sync and async cache
Features
- Both synchronous and asynchronous API
- Type-safe with full typing support
- Customizable key builders and serializers
- Thread-safe and coroutine-safe with dog-piling prevention
Installation
pip install cachium
Quick Start
from cachium import cache
# Simple function caching
@cache()
def expensive_calculation(x: int, y: int) -> int:
print(f"Calculating {x} + {y}")
return x + y
# First call performs the calculation
result1 = expensive_calculation(1, 2) # Output: Calculating 1 + 2
print(result1) # Output: 3
# Second call uses cached result
result2 = expensive_calculation(1, 2) # No calculation performed
print(result2) # Output: 3
# Async function caching works too
@cache()
async def async_expensive_calculation(x: int, y: int) -> int:
print(f"Calculating {x} + {y} asynchronously")
return x + y
# Usage with async functions
import asyncio
async def main():
# First call performs the calculation
result1 = await async_expensive_calculation(1, 2)
# Second call uses cached result
result2 = await async_expensive_calculation(1, 2)
print(result1, result2) # Output: 3 3
asyncio.run(main())
Advanced Usage
Custom TTL and Cache Size
from datetime import timedelta
from cachium import cache
from cachium._storages import TTLMapStorage
@cache(storage=lambda: TTLMapStorage(max_size=100, ttl=timedelta(hours=1)))
def long_lived_cache_function(x):
return x * 2
Custom Key Builders
from cachium import cache, DefaultKeyBuilder
# Create a custom key builder
key_builder = DefaultKeyBuilder(
prefix="custom_prefix",
func=lambda x, y: x + y,
delimiter=":"
)
@cache(key_builder=key_builder)
def my_function(x, y):
return x + y
License
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
Metadata
Release files for cachium 0.0.1a1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| cachium-0.0.1a1.tar.gz | 83.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| cachium-0.0.1a1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 105.2 kB
Release files / cachium-0.0.1a1.tar.gz
| Download URL | cachium-0.0.1a1.tar.gz |
|---|---|
| Size | 83.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
96626cc3cac19922251b3353035b49143116260477f77eb17c9824a61763033a
|
|
BLAKE2b-256 checksum How to use checksums |
8a3a026c2f4f1d7206ed517245a283f44011c6761153972fd6cb5e266d45939a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.8.12
|
Release files / cachium-0.0.1a1-py3-none-any.whl
| Download URL | cachium-0.0.1a1-py3-none-any.whl |
|---|---|
| Size | 22.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6fac50cb9c96fc949c6328be09fdb6739e7cab05a8c71357a9457ce4220da5f0
|
|
BLAKE2b-256 checksum How to use checksums |
7a8a9fdaff8f6430e29a7049aa4275a884b7f3091429b0dd8a02d571c4aa4648
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.8.12
|