pub struct ModelCache { /* private fields */ }Expand description
A second-level cache for models.
Cheap to clone; every clone shares one backend and one set of counters.
Implementations§
Source§impl ModelCache
impl ModelCache
Sourcepub fn new(store: impl Cache) -> ModelCache
pub fn new(store: impl Cache) -> ModelCache
A cache over any Cache backend — memory, file, or Redis.
The store the application already built, shared rather than duplicated.
Sourcepub fn region<M: Model>(self, region: Region) -> ModelCache
pub fn region<M: Model>(self, region: Region) -> ModelCache
Register a model, with settings.
Panics on settings that contradict each other — a read-only region with no expiry, say. That is a startup failure by design: the alternative is a stale row six weeks from now with nothing to trace it to.
Sourcepub fn cache_everything(self, region: Region) -> ModelCache
pub fn cache_everything(self, region: Region) -> ModelCache
Cache every model, registered or not, with these settings.
For an application that has decided it wants this everywhere. A model
with its own region still uses that.
pub fn stats(&self) -> &Stats
Sourcepub async fn find<M: Model + Send>(
&self,
db: &Database,
key: M::Key,
) -> Result<Option<M>>
pub async fn find<M: Model + Send>( &self, db: &Database, key: M::Key, ) -> Result<Option<M>>
A model by primary key, from the cache when it is there.
Sourcepub async fn get<M: Model + Send>(
&self,
db: &Database,
query: QueryBuilder,
) -> Result<Vec<M>>
pub async fn get<M: Model + Send>( &self, db: &Database, query: QueryBuilder, ) -> Result<Vec<M>>
A query’s results, from the cache when they are there and still current.
Sourcepub async fn first<M: Model + Send>(
&self,
db: &Database,
query: QueryBuilder,
) -> Result<Option<M>>
pub async fn first<M: Model + Send>( &self, db: &Database, query: QueryBuilder, ) -> Result<Option<M>>
The first result, from the cache when it is there.
Sourcepub async fn insert<M: Model + Send + Sync>(
&self,
db: &Database,
model: &mut M,
) -> Result<()>
pub async fn insert<M: Model + Send + Sync>( &self, db: &Database, model: &mut M, ) -> Result<()>
Insert, and invalidate what the insert makes stale.
Sourcepub async fn update<M: Model + Send + Sync>(
&self,
db: &Database,
model: &M,
) -> Result<u64>
pub async fn update<M: Model + Send + Sync>( &self, db: &Database, model: &M, ) -> Result<u64>
Update, and invalidate the entity and the table’s cached queries.
Sourcepub async fn delete<M: Model + Send + Sync>(
&self,
db: &Database,
model: &M,
) -> Result<u64>
pub async fn delete<M: Model + Send + Sync>( &self, db: &Database, model: &M, ) -> Result<u64>
Delete, and the same.
Sourcepub async fn forget<M: Model>(&self, key: M::Key) -> Result<()>
pub async fn forget<M: Model>(&self, key: M::Key) -> Result<()>
Drop one entity from the cache. The table’s queries are left alone.
For a write this cache did not make — a raw UPDATE, a migration, a
change from another process. Almost always wanted together with
invalidate_table.
Sourcepub async fn invalidate_table<M: Model>(&self) -> Result<()>
pub async fn invalidate_table<M: Model>(&self) -> Result<()>
Make every cached query for this table a miss.
One counter, bumped. There is no way to enumerate the result sets a
write invalidates — a cached WHERE role = 'admin' is invalidated by
an insert whose shape this cache never sees — so the generation is what
stands in for that enumeration.
Sourcepub async fn invalidate_all(&self) -> Result<()>
pub async fn invalidate_all(&self) -> Result<()>
Everything this cache put in the store, for every table.
Bumps each registered table’s generation rather than flushing the backend, because the backend is shared: flushing it would take the session store and the rate limiter with it.
Trait Implementations§
Source§impl Clone for ModelCache
impl Clone for ModelCache
Source§fn clone(&self) -> ModelCache
fn clone(&self) -> ModelCache
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more