Commit 83ec117
fix(config): add cache_config field to SQLAlchemy configs (#731)
Closes #730.
## Summary
- `SQLAlchemyAsyncConfig` and `SQLAlchemySyncConfig` now accept
`cache_config: Optional[CacheConfig]` and `cache_manager:
Optional[CacheManager]` kwargs, closing the documentation/implementation
gap from #636.
- When `cache_config` is set, `__post_init__` instantiates a
`CacheManager` (unless one was passed explicitly) and publishes it to
sessions via `session_config.info["cache_manager"]`. Repositories pick
it up automatically through the existing
`session.info.get("cache_manager")` lookup in `repository/_async.py` and
`repository/_sync.py`.
- `session_config` and its `info` dict are shallow-copied during
`__post_init__`, so two configs constructed with the same
`session_config` instance no longer alias each other's `cache_manager`
or `file_object_raise_on_error` flag.
- `__hash__` now includes `id(cache_manager)` so configs that differ
only by cache region remain distinguishable in the class-level
engine/session registries.
- `CacheConfig` / `CacheManager` stay behind `TYPE_CHECKING` at module
load and are imported at runtime only when `cache_config` is set, so
dogpile.cache probing is still avoided for users who don't opt in.
After this change the "Quick Start" and "Repository Integration"
examples in `docs/usage/caching.rst` work as documented:
```python
db_config = SQLAlchemyAsyncConfig(
connection_string="sqlite+aiosqlite:///app.db",
cache_config=CacheConfig(
backend="dogpile.cache.memory",
expiration_time=300,
),
)
async with db_config.get_session() as session:
repo = UserRepository(session=session, auto_expunge=True)
user = await repo.get(user_id) # DB + cache populate
user = await repo.get(user_id) # cache hit (fresh deserialized instance)
```
> [!NOTE]
> `auto_expunge=True` on the repository is required for cache hits to
round-trip cleanly across consecutive calls — without it, a cached
entity attached to a closed session triggers `MissingGreenlet` on
attribute refresh in async usage. This matches the existing "Recommended
with caching" guidance in the caching guide; the Quick Start example in
the docs should likely be updated to show it inline (follow-up).
## Test plan
- [x] Repro from the issue body runs without raising on both async and
sync configs.
- [x] New unit tests in
`tests/unit/test_config/test_{async,sync}_config.py`:
- `cache_config` builds a manager and propagates it to
`session_config.info`.
- An explicit `cache_manager` overrides `cache_config` and is preserved
by identity.
- Without `cache_config`, no `cache_manager` key is published.
- Two configs sharing a `session_config` do not clobber each other's
`cache_manager` / `file_object_raise_on_error` and keep user-supplied
`info` keys.
- `__hash__` distinguishes configs that differ only by `cache_manager`.
- [x] New integration tests in
`tests/integration/test_cache_repository.py` covering both sync and
async paths:
- `config.get_session()` + repository, populate cache, delete the
underlying DB row, second `repo.get()` in a fresh session succeeds —
proving the served value comes from the cache region rather than the
database.
- Documented Quick Start flow: two consecutive `repo.get()` calls in one
session, second call is served from cache (fresh deserialized instance,
identity differs).
- [x] Existing cache/config/routing suites: 253 passed, 1 skipped
(pre-existing `dogpile.cache`-absent gate).
<!-- docs-preview -->
<hr>
📚 Documentation preview: <a
href="https://litestar-org.github.io/advanced-alchemy-docs-preview/731">https://litestar-org.github.io/advanced-alchemy-docs-preview/731</a>
---------
Co-authored-by: Cody Fincher <cody@litestar.dev>1 parent ed8c46b commit 83ec117
5 files changed
Lines changed: 451 additions & 8 deletions
File tree
- advanced_alchemy/config
- docs
- tests
- integration
- unit/test_config
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
1 | 2 | | |
2 | 3 | | |
3 | 4 | | |
| |||
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
| 20 | + | |
19 | 21 | | |
20 | 22 | | |
21 | 23 | | |
| |||
187 | 189 | | |
188 | 190 | | |
189 | 191 | | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
190 | 211 | | |
191 | 212 | | |
192 | 213 | | |
| |||
203 | 224 | | |
204 | 225 | | |
205 | 226 | | |
206 | | - | |
207 | | - | |
208 | | - | |
209 | | - | |
210 | | - | |
211 | | - | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
212 | 245 | | |
213 | 246 | | |
214 | 247 | | |
| |||
217 | 250 | | |
218 | 251 | | |
219 | 252 | | |
| 253 | + | |
220 | 254 | | |
221 | 255 | | |
222 | 256 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
44 | 55 | | |
45 | 56 | | |
46 | 57 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
553 | 553 | | |
554 | 554 | | |
555 | 555 | | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
0 commit comments