pub struct Cache<K, T> { /* private fields */ }Expand description
Pond cache struct
Implementations§
Source§impl<K: Hash, T: Serialize + DeserializeOwned + Clone> Cache<K, T>
impl<K: Hash, T: Serialize + DeserializeOwned + Clone> Cache<K, T>
Sourcepub fn new(path: PathBuf) -> Result<Self, Error>
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");Sourcepub fn with_time_to_live(path: PathBuf, ttl: Duration) -> Result<Self, Error>
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 filettl- 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");Sourcepub fn get(&self, key: K) -> Result<Option<T>, Error>
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");Sourcepub fn store(&self, key: K, value: T) -> Result<(), Error>
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 undervalue- 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");Sourcepub fn store_with_expiration(
&self,
key: K,
value: T,
expiration: DateTime<Utc>,
) -> Result<(), Error>
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 undervalue- Value to storeexpiration- 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");Sourcepub fn clean(&self) -> Result<(), Error>
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");