Cache

Struct Cache 

Source
pub struct Cache<K, V> { /* private fields */ }
Expand description

An in-memory key-value cache that is automatically synced to disk.

The goal is simplicity, ease of use, and ease of distribution. All data is stored in a single file, which is automatically loaded into memory when using the cache.

§Warning

§No Edits

The biggest constraint for the cache is that values should never change for a given key. There is no update possible; if a value is reinserted a second time with a different value but the same key, an error will arise.

This is important to keep the file format simple: there is no metadata, no headers, just a plain separator between each cache entry. Therefore, it isn’t possible to edit previously saved content.

§No Big Files

The cache isn’t optimized for space; use it for small caches.

Implementations§

Source§

impl<K, V> Cache<K, V>
where K: CacheKey, V: CacheValue,

Source

pub fn new<P>(path: P, option: CacheOption) -> Cache<K, V>
where P: AsRef<Path>,

Create a new cache and load the data from the provided path if it exists.

Source

pub fn for_each<F>(&mut self, func: F)
where F: FnMut(&K, &V),

Iterate over all values of the cache.

Source

pub fn get(&self, key: &K) -> Option<&V>

Fetch an item from the cache.

Source

pub fn len(&self) -> usize

The size of the cache.

Source

pub fn is_empty(&self) -> bool

If the cache is empty.

Source

pub fn insert(&mut self, key: K, value: V) -> Result<(), CacheError<K, V>>

Insert a new item to the cache.

Panic if an item with a different value exists in the cache.

Trait Implementations§

Source§

impl<K, V> Debug for Cache<K, V>
where K: Debug, V: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<K, V> Display for Cache<K, V>
where K: CacheKey, V: CacheValue,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<K, V> Freeze for Cache<K, V>

§

impl<K, V> RefUnwindSafe for Cache<K, V>

§

impl<K, V> Send for Cache<K, V>
where K: Send, V: Send,

§

impl<K, V> Sync for Cache<K, V>
where K: Sync, V: Sync,

§

impl<K, V> Unpin for Cache<K, V>
where K: Unpin, V: Unpin,

§

impl<K, V> UnwindSafe for Cache<K, V>
where K: UnwindSafe, V: UnwindSafe,

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

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
§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> ToString for T
where T: Display + ?Sized,

§

fn to_string(&self) -> String

Converts the given value to a String. Read more
§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
§

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