Skip to content

Commit 8ef635c

Browse files
committed
docs: add full MkDocs site, API reference, and Pages workflow
1 parent 6c38b18 commit 8ef635c

22 files changed

+689
-0
lines changed

.github/workflows/docs.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: Deploy Docs
2+
3+
on:
4+
push:
5+
branches: [ main, master ]
6+
workflow_dispatch:
7+
8+
permissions:
9+
contents: write
10+
11+
jobs:
12+
build-deploy:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout
16+
uses: actions/checkout@v4
17+
18+
- name: Set up Python
19+
uses: actions/setup-python@v5
20+
with:
21+
python-version: '3.11'
22+
23+
- name: Install docs dependencies
24+
run: |
25+
python -m pip install --upgrade pip
26+
python -m pip install -e '.[docs]'
27+
28+
- name: Build site
29+
run: mkdocs build --strict
30+
31+
- name: Deploy to GitHub Pages (gh-pages branch)
32+
env:
33+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
34+
run: mkdocs gh-deploy --force --clean --message "docs: deploy ${GITHUB_SHA}"

docs/api/index.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# API Reference
2+
3+
## Public API
4+
5+
::: yokedcache
6+
7+
## Core
8+
9+
### Cache
10+
11+
::: yokedcache.cache
12+
13+
### Decorators
14+
15+
::: yokedcache.decorators
16+
17+
### Configuration
18+
19+
::: yokedcache.config
20+
21+
### Models
22+
23+
::: yokedcache.models
24+
25+
### Utils
26+
27+
::: yokedcache.utils

docs/architecture.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Architecture
2+
3+
```mermaid
4+
graph TB
5+
A[FastAPI App] --> B[YokedCache Wrapper]
6+
B --> C{Cache Hit?}
7+
C -->|Yes| D[Return Cached Data]
8+
C -->|No| E[Query Database]
9+
E --> F[Store in Redis]
10+
F --> G[Return Data]
11+
H[Database Write] --> I[Auto-Invalidation]
12+
I --> J[Clear Related Cache]
13+
```

docs/auto-invalidation.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Auto-Invalidation
2+
3+
YokedCache invalidates relevant cache entries on database writes so you don't serve stale data.
4+
5+
## How it works
6+
7+
- Wrap your DB dependency with `cached_dependency(...)`.
8+
- Reads (`query`, `get`, `first`, `all`) are cached with tags like `table:<name>`.
9+
- Writes (`execute`, `exec`) are tracked; on `commit()`, tags for affected tables are invalidated.
10+
11+
```python
12+
from yokedcache import YokedCache
13+
from yokedcache.decorators import cached_dependency
14+
15+
cache = YokedCache()
16+
cached_get_db = cached_dependency(get_db, cache=cache, ttl=300, table_name="users")
17+
18+
# In your route handlers, use db=Depends(cached_get_db)
19+
```
20+
21+
Under the hood (`yokedcache.decorators.CachedDatabaseWrapper`):
22+
23+
- Extracts table with `extract_table_from_query()` for simple SQL patterns.
24+
- Adds tag `table:<table>` on cached reads.
25+
- On commit, calls `cache.invalidate_tags([f"table:{table}"])`.
26+
27+
## Best practices
28+
29+
- Pass `table_name` when you know the target table.
30+
- Use predictable SQL (or ORM) so table extraction works well.
31+
- For multi-table writes, consider multiple tags.
32+
- For complex cases, call `await cache.invalidate_pattern("users:*")` directly.
33+

docs/cli-cookbook.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# CLI Cookbook
2+
3+
## Monitor stats continuously
4+
5+
```bash
6+
yokedcache stats --watch
7+
```
8+
9+
## Export config
10+
11+
```bash
12+
yokedcache export-config --output config.yaml
13+
```
14+
15+
## List keys by prefix
16+
17+
```bash
18+
yokedcache list --prefix users:
19+
```
20+
21+
## Delete by pattern
22+
23+
```bash
24+
yokedcache flush --pattern "users:*" --force
25+
```
26+
27+
## Invalidate by tags
28+
29+
```bash
30+
yokedcache flush --tags "user_data" --force
31+
```

docs/cli.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# CLI
2+
3+
```bash
4+
# View cache statistics
5+
$ yokedcache stats --watch
6+
7+
# Test Redis connection
8+
$ yokedcache ping
9+
10+
# List cached keys
11+
$ yokedcache list --pattern "user:*"
12+
13+
# Flush specific caches
14+
$ yokedcache flush --tags "user_data"
15+
16+
# Search cache contents
17+
$ yokedcache search "alice" --threshold 80
18+
19+
# Monitor in real-time (JSON output)
20+
$ yokedcache stats --format json
21+
22+
# Export current configuration
23+
$ yokedcache export-config --output config.yaml
24+
25+
# Warm cache with predefined data
26+
$ yokedcache warm --config-file cache_config.yaml
27+
```

docs/concepts.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Concepts
2+
3+
- **Cache keys**: Namespaced identifiers for cached values. Built from `key_prefix`, function/table context, and hashed args.
4+
- **TTL and jitter**: Each entry expires after TTL; jitter spreads expirations to avoid thundering herds.
5+
- **Tags**: Group keys for bulk invalidation, e.g., `table:users`.
6+
- **Auto-invalidation**: Write operations trigger targeted cache clears.
7+
- **Serialization**: Values are serialized (JSON by default) before storage.
8+
- **Metrics**: Hit/miss counts, latencies, memory usage via `get_stats()` and CLI.
9+
10+
## Where concepts live in code
11+
12+
- Keys: `yokedcache.utils.generate_cache_key`, `sanitize_key`
13+
- TTL jitter: `yokedcache.utils.calculate_ttl_with_jitter`
14+
- Tags: maintained via Redis sets, see `YokedCache._add_tags_to_key`
15+
- Auto-invalidation: `CachedDatabaseWrapper` in `yokedcache.decorators`
16+
- Serialization: `yokedcache.utils.serialize_data` / `deserialize_data`
17+
- Config: `yokedcache.config.CacheConfig`

docs/config-reference.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Configuration Reference
2+
3+
Key options from `CacheConfig`:
4+
5+
- `redis_url` (str): Redis connection string. Default `redis://localhost:6379/0`.
6+
- `default_ttl` (int): Default TTL seconds. Default `300`.
7+
- `key_prefix` (str): Prefix for all keys. Default `yokedcache`.
8+
- `enable_fuzzy` (bool): Enable fuzzy search. Default `False`.
9+
- `fuzzy_threshold` (int): 0-100 score cutoff. Default `80`.
10+
- `max_connections` (int): Redis pool size. Default `50`.
11+
- `log_level` (str): Logging level. Default `INFO`.
12+
13+
Table-level via `TableCacheConfig`:
14+
15+
- `ttl` (int): Per-table TTL.
16+
- `tags` (set[str]): Default tags applied to reads.
17+
- `enable_fuzzy` (bool), `fuzzy_threshold` (int)
18+
- `serialization_method`: JSON/PICKLE/MSGPACK
19+
- `query_specific_ttls` (dict[str,int]): per-query-hash TTL overrides.
20+
21+
Env overrides:
22+
23+
- `YOKEDCACHE_REDIS_URL`, `YOKEDCACHE_DEFAULT_TTL`, `YOKEDCACHE_KEY_PREFIX`,
24+
`YOKEDCACHE_ENABLE_FUZZY`, `YOKEDCACHE_FUZZY_THRESHOLD`, `YOKEDCACHE_MAX_CONNECTIONS`, `YOKEDCACHE_LOG_LEVEL`.

docs/configuration.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Configuration
2+
3+
YokedCache can be configured programmatically or via YAML.
4+
5+
## YAML example
6+
7+
```yaml
8+
# cache_config.yaml
9+
redis_url: redis://localhost:6379/0
10+
default_ttl: 300
11+
key_prefix: yokedcache
12+
enable_fuzzy: false
13+
14+
tables:
15+
users:
16+
ttl: 3600
17+
tags: ["user_data"]
18+
invalidation_rules:
19+
- table: users
20+
on: [update, delete]
21+
invalidate_tags: ["user_data"]
22+
```
23+
24+
## Programmatic
25+
26+
```python
27+
from yokedcache import CacheConfig, YokedCache
28+
29+
config = CacheConfig(default_ttl=600, key_prefix="myapp")
30+
cache = YokedCache(config=config)
31+
```
32+
33+
## Environment variables
34+
35+
- `YOKEDCACHE_REDIS_URL`
36+
- `YOKEDCACHE_DEFAULT_TTL`
37+
- `YOKEDCACHE_KEY_PREFIX`
38+
- `YOKEDCACHE_ENABLE_FUZZY`
39+
- `YOKEDCACHE_FUZZY_THRESHOLD`

docs/examples.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Examples
2+
3+
- Repository examples:
4+
- `examples/basic_usage.py`
5+
- `examples/fastapi_example.py`
6+
- `examples/cache_config.yaml`
7+
8+
## Quick snippets
9+
10+
### Decorator caching
11+
```python
12+
from yokedcache import cached
13+
14+
@cached(ttl=600, tags=["products"])
15+
async def get_expensive_data(q: str):
16+
return expensive_db_query(q)
17+
```
18+
19+
### Manual operations
20+
```python
21+
from yokedcache import YokedCache
22+
cache = YokedCache()
23+
await cache.set("key", {"data": 1}, ttl=120, tags=["demo"])
24+
value = await cache.get("key")
25+
await cache.invalidate_tags(["demo"])
26+
```

0 commit comments

Comments
 (0)