[][src]Struct blip::service::cache::Cache

pub struct Cache<S: ?Sized = dyn Source>(_);
This is supported on feature="cache" only.

A distributed binary cache. May be used standalone, or added to a Mesh to operate in networked mode.

Examples

use blip::service::Cache;
use std::sync::atomic::{AtomicUsize, Ordering::SeqCst};

let loads = AtomicUsize::new(0);
let c = Cache::from_fn(32, move |key| {
    assert_eq!(0, loads.swap(1, SeqCst));
    key.into()
});

let val = c.get("test key").await?;
assert_eq!(&*val, b"test key");
let val = c.get("test key").await?;
assert_eq!(&*val, b"test key");

Implementations

impl Cache[src]

pub fn new<S: Source>(max_keys: usize, source: S) -> Self[src]

This is supported on feature="cache" only.

Create a new cache from a Source. At most max_keys + (max_keys / 8) keys will be cached locally at any point in time.

Panics

Panics if max_keys == 0.

Examples

use blip::service::{cache::Source, Cache};
use tonic::Status;

struct Echo;

#[blip::async_trait]
impl Source for Echo {
    async fn get(&self, key: &[u8]) -> Result<Vec<u8>, Status> {
        Ok(key.into())
    }
}

let cache = Cache::new(1024, Echo);

pub fn from_fn<F>(max_keys: usize, source: F) -> Self where
    F: Sync + Send + 'static + Fn(&[u8]) -> Vec<u8>, 
[src]

This is supported on feature="cache" only.

Create a new cache from a source Fn. At most max_keys + (max_keys / 8) keys will be cached locally at any point in time.

Panics

Panics if max_keys == 0.

Examples

use blip::service::Cache;

let cache = Cache::from_fn(1024, |key| key.into());

pub async fn get<'_, K: Into<Bytes>>(&'_ self, key: K) -> Result<Bytes, Status>[src]

This is supported on feature="cache" only.

Retrieve the value associated with key.

Trait Implementations

impl<S: ?Sized> Clone for Cache<S>[src]

impl ExposedService for Cache[src]

type Service = CacheServer<Self>

The service implementation.

impl MeshService for Cache[src]

Auto Trait Implementations

impl<S = dyn Source + 'static> !RefUnwindSafe for Cache<S>

impl<S: ?Sized> Send for Cache<S> where
    S: Send + Sync

impl<S: ?Sized> Sync for Cache<S> where
    S: Send + Sync

impl<S: ?Sized> Unpin for Cache<S>

impl<S = dyn Source + 'static> !UnwindSafe for Cache<S>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T> Instrument for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> IntoRequest<T> for T[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>, 

impl<T> WithSubscriber for T[src]