pub struct Cache { /* private fields */ }Expand description
SQLite-backed TTL key-value cache reading and writing the slabs_cache table.
Requires slabs_db::Db::init_system_tables (or slabs_db::Db::migrate) before use.
Implementations§
Source§impl Cache
impl Cache
Sourcepub async fn set(
&self,
key: &str,
value: &str,
ttl_seconds: i64,
) -> SlabResult<()>
pub async fn set( &self, key: &str, value: &str, ttl_seconds: i64, ) -> SlabResult<()>
Upserts value under key in slabs_cache with a UTC expiration of now + ttl_seconds.
Overwrites any existing value and TTL for key. Returns SlabError::BadRequest if
ttl_seconds <= 0 or if computing the expiration timestamp overflows.
Sourcepub async fn get(&self, key: &str) -> SlabResult<Option<String>>
pub async fn get(&self, key: &str) -> SlabResult<Option<String>>
Fetches the string value for key from slabs_cache when expires_at > Utc::now().
Returns Ok(None) if key is absent or expired. Does not delete expired rows.
Sourcepub async fn set_json<T: Serialize>(
&self,
key: &str,
value: &T,
ttl_seconds: i64,
) -> SlabResult<()>
pub async fn set_json<T: Serialize>( &self, key: &str, value: &T, ttl_seconds: i64, ) -> SlabResult<()>
Serializes value with serde_json and upserts it under key for ttl_seconds.
Returns SlabError::Internal if serialization fails.
Sourcepub async fn get_json<T: DeserializeOwned>(
&self,
key: &str,
) -> SlabResult<Option<T>>
pub async fn get_json<T: DeserializeOwned>( &self, key: &str, ) -> SlabResult<Option<T>>
Fetches a non-expired entry for key and deserializes it from JSON.
Returns Ok(None) if key is absent or expired, and SlabError::Internal if JSON decoding fails.
Sourcepub async fn delete(&self, key: &str) -> SlabResult<()>
pub async fn delete(&self, key: &str) -> SlabResult<()>
Deletes the slabs_cache row for key if it exists.
Sourcepub async fn purge_expired(&self) -> SlabResult<u64>
pub async fn purge_expired(&self) -> SlabResult<u64>
Deletes all slabs_cache rows where expires_at <= Utc::now() and returns the deleted row count.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Cache
impl !UnwindSafe for Cache
impl Forget for Cache
impl Freeze for Cache
impl Move for Cache
impl Send for Cache
impl Sync for Cache
impl Unpin for Cache
impl UnsafeUnpin for Cachewhere
*const ArcInner<PoolInner<Sqlite>> is !null: UnsafeUnpin,
PhantomData<ArcInner<PoolInner<Sqlite>>>: UnsafeUnpin,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more