Cache

Struct Cache 

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

Pond cache struct

Implementations§

Source§

impl<K: Hash, T: Serialize + DeserializeOwned + Clone> Cache<K, T>

Source

pub fn new(path: PathBuf) -> Result<Self, Error>

Create a new cache with a default time-to-live of 10 minutes

§Arguments
  • path - Path to the SQLite database file
§Returns

A new cache instance

§Errors

Returns an error if the database connection cannot be established

§Example
use pond_cache::Cache;
use std::path::PathBuf;

let cache: Cache<&str, String> = Cache::new(PathBuf::from("cache.db")).expect("Failed to create cache");
Source

pub fn with_time_to_live(path: PathBuf, ttl: Duration) -> Result<Self, Error>

Create a new cache with a custom time-to-live

§Arguments
  • path - Path to the SQLite database file
  • ttl - Time-to-live for cache entries
§Returns

A new cache instance

§Errors

Returns an error if the database connection cannot be established

§Example
use pond_cache::Cache;
use std::path::PathBuf;
use chrono::Duration;

let cache: Cache<&str, String> = Cache::with_time_to_live(PathBuf::from("cache.db"), Duration::minutes(5)).expect("Failed to create cache");
Source

pub fn get(&self, key: K) -> Result<Option<T>, Error>

Retrieve a value from the cache

§Arguments
  • key - Key to retrieve the value for
§Returns

The value associated with the key, if it exists and has not expired If the value does not exist or has expired, returns None

§Errors

Returns an error if the database connection cannot be established

§Example
use pond_cache::Cache;
use std::path::PathBuf;

let cache: Cache<&str, String> = Cache::new(PathBuf::from("cache.db")).expect("Failed to create cache");
let key = "key";
let value: Option<String> = cache.get(key).expect("Failed to get value");
Source

pub fn store(&self, key: K, value: T) -> Result<(), Error>

Store a value in the cache The value will be stored with the cache’s time-to-live If the value already exists, it will be replaced

§Arguments
  • key - Key to store the value under
  • value - Value to store
§Returns

Ok if the value was stored successfully Err if the value could not be stored

§Errors

Returns an error if the database connection cannot be established

§Example
use pond_cache::Cache;
use std::path::PathBuf;

let cache: Cache<&str, String> = Cache::new(PathBuf::from("cache.db")).expect("Failed to create cache");
let key = "key";
let value = String::from("value");
cache.store(key, value).expect("Failed to store value");
Source

pub fn store_with_expiration( &self, key: K, value: T, expiration: DateTime<Utc>, ) -> Result<(), Error>

Store a value in the cache with a custom expiration time If the value already exists, it will be replaced

§Arguments
  • key - Key to store the value under
  • value - Value to store
  • expiration - Expiration time for the value
§Returns

Ok if the value was stored successfully Err if the value could not be stored

§Errors

Returns an error if the database connection cannot be established

§Example
use pond_cache::Cache;
use std::path::PathBuf;
use chrono::{Duration, Utc};

let cache: Cache<&str, String> = Cache::new(PathBuf::from("cache.db")).expect("Failed to create cache");
let key = "key";
let value = String::from("value");
let expiration = Utc::now() + Duration::minutes(5);

cache.store_with_expiration(key, value, expiration).expect("Failed to store value");
Source

pub fn clean(&self) -> Result<(), Error>

Clean up the cache by removing expired entries This method should be called periodically to prevent the cache from growing indefinitely

§Returns

Ok if the cache was cleaned successfully Err if the cache could not be cleaned

§Errors

Returns an error if the database connection cannot be established

§Example
use pond_cache::Cache;
use std::path::PathBuf;

let cache: Cache<&str, String> = Cache::new(PathBuf::from("cache.db")).expect("Failed to create cache");
cache.clean().expect("Failed to clean cache");

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

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

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> 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, 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.