Skip to content

Commit b3f466e

Browse files
committed
docs: add documentation for built-in storage options and usage examples
1 parent 355910c commit b3f466e

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

docs/api/storages.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Storages
2+
3+
Storages keep cached values and provide the per-key locks used by the decorator to prevent dog-piling.
4+
5+
Built-in storages:
6+
- `TTLMapStorage`: synchronous in-memory storage with TTL expiration and LRU eviction.
7+
- `TTLMapAsyncStorage`: asynchronous counterpart for `async def` functions.
8+
9+
Usage:
10+
11+
```python
12+
from datetime import timedelta
13+
from cachium import cache
14+
from cachium.storages.ttl_map import TTLMapStorage
15+
16+
@cache(storage=TTLMapStorage.create_with(max_size=1000, ttl=timedelta(minutes=5)))
17+
def get_value(key: str) -> str:
18+
return f"value:{key}"
19+
```
20+
21+
Important details:
22+
- `cache` expects a storage factory, not a storage instance.
23+
- The factory is called once at decoration time, giving each decorated function its own storage.
24+
- Use `TTLMapStorage` for regular functions and `TTLMapAsyncStorage` for async functions.
25+
- `max_size` limits entry count; `ttl` controls age-based expiration. Use `ttl=None` to disable age-based expiration.
26+
- Built-in storages are process-local. Use a custom storage backend for shared multi-process or cross-machine caching.
27+
28+
Full reference below.
29+
30+
::: cachium.storages._abc.Result
31+
32+
---
33+
34+
::: cachium.storages._abc.BaseStorage
35+
36+
---
37+
38+
::: cachium.storages._abc.BaseAsyncStorage
39+
40+
---
41+
42+
::: cachium.storages.ttl_map.TTLMapStorage
43+
44+
---
45+
46+
::: cachium.storages.ttl_map.TTLMapAsyncStorage

0 commit comments

Comments
 (0)