LocalCache

Struct LocalCache 

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

Implementations§

Source§

impl LocalCache

Source

pub fn initialize(capacity: usize, ttl: u64) -> Self

Initilizalizes a new LocalCache with the given capacity and ttl. Note that this only initializes the parameters that set the cache capacity and ttl. The cache will be created with the given parameter only when a thread first accesses the cache with call to get_item or add_item. Subsequent calls to initialize simply modify the cache parameters, which will only effect threads that did not previously access the cache.

§Arguments
  • capacity - The maximum number of items the cache can hold before evicting the least recently used item.
  • ttl - The time-to-live for each item in seconds. anything less than 1 means no expiration.
§Example
use local_lru::LocalCache;  
use bytes::Bytes;
let cache = LocalCache::initialize(2, 60);
cache.add_item("key1", Bytes::from("value1"));
assert_eq!(cache.get_item("key1"), Some(Bytes::from("value1")));
Source

pub fn new(capacity: usize, ttl: u64) -> Self

👎Deprecated since 0.4.0: Use initialize() instead
Source

pub fn get_item(&self, key: &str) -> Option<Bytes>

Gets an item from the cache. In LRU cache fetching, the item is moved to the front of the list.

§Returns

An Option containing the item if it exists, or None if it does not.

Source

pub fn add_item(&self, key: &str, value: Bytes)

Adds an item to the cache.

§Arguments
  • key - The key to add the item for.
  • value - The value to add to the cache represented as Bytes.
Source

pub fn add_struct<T: Serialize>(&self, key: &str, value: T)

Wrapper function to add a struct to the cache. It simple uses bincode to serialize the struct and add it to the cache as a Bytes object.

§Arguments
  • key - The key to add the item for.
  • value - Any struct that implements Serialize.
Source

pub fn get_struct<T: DeserializeOwned>(&self, key: &str) -> Option<T>

Wrapper function to get a struct from the cache. It uses bincode to deserialize the Bytes object back into a struct.

§Arguments
  • key - The key to get the item for.
§Returns

An Option containing the item if it exists, or None if it does not.

Trait Implementations§

Source§

impl Clone for LocalCache

Source§

fn clone(&self) -> LocalCache

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. 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, 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> 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.