pub struct LocalCache { /* private fields */ }Implementations§
Source§impl LocalCache
impl LocalCache
Sourcepub fn initialize(capacity: usize, ttl: u64) -> Self
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")));pub fn new(capacity: usize, ttl: u64) -> Self
👎Deprecated since 0.4.0: Use initialize() instead
Sourcepub fn get_item(&self, key: &str) -> Option<Bytes>
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.
Sourcepub fn add_item(&self, key: &str, value: Bytes)
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 asBytes.
Sourcepub fn add_struct<T: Serialize>(&self, key: &str, value: T)
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.
Sourcepub fn get_struct<T: DeserializeOwned>(&self, key: &str) -> Option<T>
pub fn get_struct<T: DeserializeOwned>(&self, key: &str) -> Option<T>
Trait Implementations§
Source§impl Clone for LocalCache
impl Clone for LocalCache
Source§fn clone(&self) -> LocalCache
fn clone(&self) -> LocalCache
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for LocalCache
impl RefUnwindSafe for LocalCache
impl Send for LocalCache
impl Sync for LocalCache
impl Unpin for LocalCache
impl UnwindSafe for LocalCache
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more