Skip to main content

Cache

Struct Cache 

Source
pub struct Cache { /* private fields */ }
Expand description

A connected cache, shared by every worker.

Cloning is cheap: the underlying redis::aio::ConnectionManager is reference-counted and reconnects on its own, so a clone shares one connection rather than opening another.

Implementations§

Source§

impl Cache

Source

pub async fn connect(config: &CacheConfig) -> Result<Option<Cache>, CacheError>

Connect the cache an app’s [cache] section describes.

Ok(None) means the app configured none, which is the default and not an error. Err means it asked for one that can’t be reached — worth failing the boot over, because a function written against a cache and silently given none is a bug that only shows up as a load spike.

Source

pub async fn execute(&self, request: &str) -> Result<Value, CacheError>

Run one operation, given as the JSON a function sent across the ABI, and return the JSON reply.

This is the single entry point the host bridge uses, which is what keeps the ABI to one cache call instead of one per verb — a new operation costs a variant of Op, not a change to the contract every compiled function was built against.

Source

pub async fn get(&self, key: &str) -> Result<Option<Value>, CacheError>

Read a value, or None when the key is absent or expired.

A value that isn’t valid JSON (something else wrote it) comes back as a JSON string rather than as an error — the caller asked for whatever is there.

Source

pub async fn set( &self, key: &str, value: &Value, ttl: Option<u64>, ) -> Result<(), CacheError>

Write a value. ttl of None uses [cache] default_ttl_secs; 0 means “no expiry” in both places.

Source

pub async fn delete(&self, key: &str) -> Result<bool, CacheError>

Remove a key. true when it was there.

Source

pub async fn exists(&self, key: &str) -> Result<bool, CacheError>

Whether a key is present and unexpired.

Source

pub async fn incr( &self, key: &str, by: i64, ttl: Option<u64>, ) -> Result<i64, CacheError>

Add to a counter and return its new value, creating it at zero first.

Atomic on the server, which is what makes it usable for rate limiting from several workers (or several hosts) at once — a read-modify-write through get and set would not be.

Source

pub async fn ttl(&self, key: &str) -> Result<Option<i64>, CacheError>

Seconds until a key expires: None when it is absent or has no expiry.

Trait Implementations§

Source§

impl Clone for Cache

Source§

fn clone(&self) -> Cache

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 Cache

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Cache

§

impl !UnwindSafe for Cache

§

impl Freeze for Cache

§

impl Send for Cache

§

impl Sync for Cache

§

impl Unpin for Cache

§

impl UnsafeUnpin for Cache

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more