Skip to main content

ModelCache

Struct ModelCache 

Source
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

Source

pub fn new(store: impl Cache) -> ModelCache

A cache over any Cache backend — memory, file, or Redis.

Source

pub fn shared(store: &CacheStore) -> ModelCache

The store the application already built, shared rather than duplicated.

Source

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.

Source

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.

Source

pub fn stats(&self) -> &Stats

Source

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.

Source

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.

Source

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.

Source

pub async fn insert<M: Model + Send + Sync>( &self, db: &Database, model: &mut M, ) -> Result<()>

Insert, and invalidate what the insert makes stale.

Source

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.

Source

pub async fn delete<M: Model + Send + Sync>( &self, db: &Database, model: &M, ) -> Result<u64>

Delete, and the same.

Source

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.

Source

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.

Source

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.

Source§

impl ModelCache

The store, for a caller that wants to reach past this.

Source

pub fn store(&self) -> &Arc<dyn Cache> ⓘ

Trait Implementations§

Source§

impl Clone for ModelCache

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ModelCache

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.