Skip to main content

Backend

Trait Backend 

Source
pub trait Backend:
    Debug
    + Send
    + Sync
    + 'static {
    type Store<K, V>: Store<K, V>
       where K: Eq + Hash + Copy + Send + Sync + 'static,
             V: Send + Sync + 'static;

    // Required method
    fn store<K, V>(&self, resource: Resource, limit: Limit) -> Self::Store<K, V>
       where K: Eq + Hash + Copy + Send + Sync + 'static,
             V: Send + Sync + 'static;
}
Expand description

Creates the Store of each Resource.

A backend is chosen once, as the type parameter of Cache, so every store call is a static call (D-155). The default is DashMapBackend.

discord.js counterpart: the makeCache client option, called once per manager.

§Examples

A backend creating the MutexStore of the Store example:

use djs_cache::{Backend, Cache, CacheConfig, Limit, Resource};

#[derive(Debug)]
struct MutexBackend;

impl Backend for MutexBackend {
    type Store<K, V>
        = MutexStore<K, V>
    where
        K: Eq + Hash + Copy + Send + Sync + 'static,
        V: Send + Sync + 'static;

    fn store<K, V>(&self, _resource: Resource, _limit: Limit) -> MutexStore<K, V>
    where
        K: Eq + Hash + Copy + Send + Sync + 'static,
        V: Send + Sync + 'static,
    {
        MutexStore(Mutex::new(HashMap::new()))
    }
}

let cache = Cache::with_backend(CacheConfig::default(), MutexBackend);

Required Associated Types§

Source

type Store<K, V>: Store<K, V> where K: Eq + Hash + Copy + Send + Sync + 'static, V: Send + Sync + 'static

The store this backend creates for values of type V under keys of type K.

Required Methods§

Source

fn store<K, V>(&self, resource: Resource, limit: Limit) -> Self::Store<K, V>
where K: Eq + Hash + Copy + Send + Sync + 'static, V: Send + Sync + 'static,

Creates the store of resource.

Called once per enabled resource when the Cache is created. limit is the resource’s configured limit, for information: it counts in the resource’s scope (per guild for members, D-159) and the cache enforces it, so a store does not evict anything itself.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl Backend for DashMapBackend

Source§

type Store<K, V> = DashMapStore<K, V> where K: Eq + Hash + Copy + Send + Sync + 'static, V: Send + Sync + 'static