auto_invalidate
Automatic cache invalidation on mutable access using a guard pattern.
Overview
auto_invalidate provides a derive macro that wraps your structs with automatic cache invalidation. When you access the struct mutably through the as_mut() guard, all cache fields are automatically invalidated when the guard is dropped.
Usage
use cached;
use OnceCell;
Features
Invalidation Modes
#[invalidate]or#[invalidate(take)]- Calls.take()on the field (works withOption,OnceCell, etc.)#[invalidate(default)]- Replaces the field withDefault::default()#[invalidate(|c| expr)]- Custom invalidation logic
use RefCell;
use HashMap;
Multiple Cache Fields
Generated build() Constructor
The macro always generates a build() constructor that takes non-cache fields as arguments and initializes cache fields to their defaults:
// Generated: fn build(value: i32, multiplier: f64) -> Self
let e = build;
How It Works
The #[cached] macro transforms your struct:
- Renames your struct to
{Name}Inner - Creates a newtype wrapper
{Name}that holdsCached<{Name}Inner> - Implements
Derefso you can access fields and methods transparently - Provides
as_mut()which returns a guard that invalidates caches on drop
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.