Skip to main content

LocalCache

Struct LocalCache 

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

Local in-memory cache with per-entry TTL support.

Uses Moka’s sync cache with a custom expiry policy to support per-operation TTL values, ensuring behavioral consistency with RedisCache.

§Performance

This implementation is optimized for hot-path usage:

  • Uses Bytes for keys and values (reference-counted, cheap clones)
  • Supports borrowed lookups via the equivalent trait for contains_sync
  • Minimizes allocations on repeated operations with the same key

§Example

use std::time::Duration;
use mx_cache::{LocalCache, Cache};

let cache = LocalCache::new(10_000, Duration::from_secs(300));

// Each operation can specify its own TTL
cache.set(b"short_lived", b"value1", Duration::from_secs(10)).await.unwrap();
cache.set(b"long_lived", b"value2", Duration::from_secs(3600)).await.unwrap();

Implementations§

Source§

impl LocalCache

Source

pub fn new(capacity: u64, default_ttl: Duration) -> Self

Creates a new local cache with the specified capacity and default TTL.

§Arguments
  • capacity - Maximum number of entries in the cache
  • default_ttl - Default TTL used as an upper bound for entries. Individual operations can specify shorter TTLs, but entries will never live longer than this default.
§Note

The default_ttl parameter sets a maximum lifetime for entries. Per-operation TTLs (passed to set() and set_nx_px()) take precedence and are fully respected, as long as they don’t exceed this default.

Source

pub fn contains_sync(&self, key: &[u8]) -> bool

Synchronous check for local cache presence - fast path for deduplication.

Returns true if the key exists in the cache and has not expired.

§Performance

This is a synchronous operation optimized for hot-path deduplication checks. Uses borrowed key lookup via the equivalent trait to avoid allocating a new Bytes on every call.

Source

pub fn get_sync(&self, key: &[u8]) -> Option<Bytes>

Synchronous get for local cache - fast path for hot data.

Returns the value if the key exists and has not expired.

§Performance

This is a synchronous operation that returns a Bytes reference to avoid copying the value data.

Trait Implementations§

Source§

impl Cache for LocalCache

Source§

fn set_nx_px( &self, key: &[u8], value: &[u8], ttl: Duration, ) -> impl Future<Output = Result<bool>> + Send

Sets a value only if the key does not exist (NX = Not eXists).

§Arguments
  • key - The cache key
  • value - The value to store
  • ttl - Time-to-live for this entry
§Returns
  • Ok(true) - The key was newly inserted
  • Ok(false) - The key already existed, no change made
Source§

fn set( &self, key: &[u8], value: &[u8], ttl: Duration, ) -> impl Future<Output = Result<()>> + Send

Sets a value with the specified TTL.

§Arguments
  • key - The cache key
  • value - The value to store
  • ttl - Time-to-live for this entry
Source§

fn get( &self, key: &[u8], ) -> impl Future<Output = Result<Option<Vec<u8>>>> + Send

Gets a value from the cache.

§Returns
  • Ok(Some(value)) - The value exists and has not expired
  • Ok(None) - The key does not exist or has expired
Source§

fn del(&self, key: &[u8]) -> impl Future<Output = Result<()>> + Send

Deletes a key from the cache.

Source§

impl Clone for LocalCache

Source§

fn clone(&self) -> LocalCache

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 LocalCache

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

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. 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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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