Struct Cache

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

Implementations§

Source§

impl Cache

Source

pub fn drop()

Drops the global cache instance and clears all in-memory entries, locks, and file locks.

After calling this, Cache::instance will return an error until Cache::new is called again.

Source

pub fn instance() -> Result<Self, Box<dyn Error>>

Returns the globally initialized Cache instance if it exists.

§Errors

Returns an error if Cache::new has not been called yet.

§Example
let cache = cache_ro::Cache::instance().unwrap();
Source

pub fn new(config: CacheConfig) -> Result<Self, Box<dyn Error>>

Creates and initializes a new global Cache instance.

If persistence is enabled in the provided CacheConfig, the directory will be created and any persisted entries will be loaded into memory.

This will also start a background thread to periodically clean up expired entries.

§Errors

Returns an error if:

  • A cache instance already exists.
  • The persistence directory cannot be created.
§Example
let cache = cache_ro::Cache::new(Default::default()).unwrap();
Source

pub fn set<V: Serialize>( &self, key: &str, value: V, ttl: Duration, ) -> Result<(), Box<dyn Error>>

Stores a value in the cache with a specified TTL (time-to-live).

If persistence is enabled, the value will also be saved to disk.

§Arguments
  • key - Cache key.
  • value - Serializable value to store.
  • ttl - Expiration duration.
§Errors

Returns an error if serialization or persistence fails.

Source

pub fn get<V: for<'de> Deserialize<'de>>(&self, key: &str) -> Option<V>

Retrieves and deserializes a value from the cache if it has not expired.

§Type Parameters
  • V - Type to deserialize into.
§Arguments
  • key - Cache key.
§Returns

Some(value) if the entry exists and is valid, otherwise None.

Source

pub fn expire(&self, key: &str) -> Option<Duration>

Returns the remaining TTL for a given cache key.

§Returns

Some(duration) if the entry exists and is not expired, otherwise None.

Source

pub fn remove(&self, key: &str) -> Result<(), Box<dyn Error>>

Removes an entry from the cache.

If persistence is enabled, the removal is also reflected on disk.

§Errors

Returns an error if persistence fails.

Source

pub fn clear(&self) -> Result<(), Box<dyn Error>>

Clears all entries from the cache (in memory and on disk if persistent).

§Errors

Returns an error if persistent files cannot be deleted.

Source

pub fn len(&self) -> usize

Returns the number of entries currently stored in memory.

Source

pub fn is_empty(&self) -> bool

Checks whether the cache contains no entries.

Source

pub fn memory_usage(&self) -> usize

Trait Implementations§

Source§

impl Clone for Cache

Source§

fn clone(&self) -> Cache

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§

§

impl Freeze for Cache

§

impl RefUnwindSafe for Cache

§

impl Send for Cache

§

impl Sync for Cache

§

impl Unpin for Cache

§

impl UnwindSafe for Cache

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> Same for T

Source§

type Output = T

Should always be Self
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.