# model-cache

This fixture drives the model/cache generator and documents runtime cache usage.

```bash
rzcli model gen -s examples/model-cache/schema.sql -d target/generated --with-sqlx --with-redis-cache
```

The generated crate contains:

- `src/entity.rs` with serde DTO entities and optional `sqlx::FromRow` derives.
- `src/repository.rs` with repository traits, SQLx repositories, and optional cache-aside repositories.
- `src/cache.rs` with primary/unique cache key helpers plus Redis/L1/L2 cache aliases.

The generator does not own credentials, migrations, transaction boundaries, or Redis topology. Application code wires the generated repository with runtime stores.

```rust
use rs_zero::cache::{CacheAside, CacheAsideConfig, LruCacheStore, TwoLevelCacheStore};
use rs_zero::cache::redis::{RedisCacheConfig, RedisCacheStore};
use user_model::repository::{CachedUserRepository, SqlxUserRepository};

let sql_repo = SqlxUserRepository::new(pool);
let l1 = LruCacheStore::new(10_000)?;
let l2 = RedisCacheStore::new(RedisCacheConfig::default())?;
let cache = CacheAside::new(TwoLevelCacheStore::new(l1, l2), CacheAsideConfig::default());
let repo = CachedUserRepository::new(sql_repo, cache, "user-service");
```

See [Model/cache 手册](../../docs/manual/model-cache.md) and [Cache 手册](../../docs/manual/cache.md).
