From 75127095e8be0b11d9e7b346bb6a66c0811079cc Mon Sep 17 00:00:00 2001 From: Mike Rivnak <7389355+rivnakm@users.noreply.github.com> Date: Tue, 14 Jan 2025 19:31:54 -0500 Subject: [PATCH] chore(key): set key generic type at cache level (#28) --- src/lib.rs | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 1803f31..82188e6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,9 +11,10 @@ use serde::Serialize; pub use rusqlite::Error; /// Pond cache struct -pub struct Cache { +pub struct Cache { path: PathBuf, ttl: Duration, + key: std::marker::PhantomData, data: std::marker::PhantomData, } @@ -27,7 +28,7 @@ where expiration: DateTime, } -impl Cache { +impl Cache { /// Create a new cache with a default time-to-live of 10 minutes /// /// # Arguments @@ -44,7 +45,7 @@ impl Cache { /// use pond_cache::Cache; /// use std::path::PathBuf; /// - /// let cache: Cache = Cache::new(PathBuf::from("cache.db")).expect("Failed to create cache"); + /// let cache: Cache<&str, String> = Cache::new(PathBuf::from("cache.db")).expect("Failed to create cache"); /// ``` pub fn new(path: PathBuf) -> Result { Self::with_time_to_live(path, Duration::minutes(10)) @@ -68,7 +69,7 @@ impl Cache { /// use std::path::PathBuf; /// use chrono::Duration; /// - /// let cache: Cache = Cache::with_time_to_live(PathBuf::from("cache.db"), Duration::minutes(5)).expect("Failed to create cache"); + /// let cache: Cache<&str, String> = Cache::with_time_to_live(PathBuf::from("cache.db"), Duration::minutes(5)).expect("Failed to create cache"); /// ``` pub fn with_time_to_live(path: PathBuf, ttl: Duration) -> Result { let db = Connection::open(path.as_path())?; @@ -87,6 +88,7 @@ impl Cache { Ok(Self { path, ttl, + key: std::marker::PhantomData, data: std::marker::PhantomData, }) } @@ -108,11 +110,11 @@ impl Cache { /// use pond_cache::Cache; /// use std::path::PathBuf; /// - /// let cache: Cache = Cache::new(PathBuf::from("cache.db")).expect("Failed to create cache"); + /// let cache: Cache<&str, String> = Cache::new(PathBuf::from("cache.db")).expect("Failed to create cache"); /// let key = "key"; /// let value: Option = cache.get(key).expect("Failed to get value"); /// ``` - pub fn get(&self, key: K) -> Result, Error> { + pub fn get(&self, key: K) -> Result, Error> { let db = Connection::open(self.path.as_path())?; let mut stmt = db.prepare( @@ -175,12 +177,12 @@ impl Cache { /// use pond_cache::Cache; /// use std::path::PathBuf; /// - /// let cache: Cache = Cache::new(PathBuf::from("cache.db")).expect("Failed to create cache"); + /// 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"); /// ``` - pub fn store(&self, key: K, value: T) -> Result<(), Error> { + pub fn store(&self, key: K, value: T) -> Result<(), Error> { self.store_with_expiration(key, value, Utc::now() + self.ttl) } @@ -205,14 +207,14 @@ impl Cache { /// use std::path::PathBuf; /// use chrono::{Duration, Utc}; /// - /// let cache: Cache = Cache::new(PathBuf::from("cache.db")).expect("Failed to create cache"); + /// 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"); /// ``` - pub fn store_with_expiration( + pub fn store_with_expiration( &self, key: K, value: T, @@ -261,7 +263,7 @@ impl Cache { /// use pond_cache::Cache; /// use std::path::PathBuf; /// - /// let cache: Cache = Cache::new(PathBuf::from("cache.db")).expect("Failed to create cache"); + /// let cache: Cache<&str, String> = Cache::new(PathBuf::from("cache.db")).expect("Failed to create cache"); /// cache.clean().expect("Failed to clean cache"); /// ``` pub fn clean(&self) -> Result<(), Error> { @@ -370,7 +372,7 @@ mod tests { Uuid::new_v4(), rand::random::() )); - let cache: Cache = Cache::new(filename.clone()).unwrap(); + let cache: Cache = Cache::new(filename.clone()).unwrap(); assert_eq!(cache.path, filename); assert_eq!(cache.ttl, Duration::minutes(10)); } @@ -382,8 +384,8 @@ mod tests { Uuid::new_v4(), rand::random::() )); - let _: Cache = Cache::new(filename.clone()).unwrap(); - let _: Cache = Cache::new(filename).unwrap(); + let _: Cache = Cache::new(filename.clone()).unwrap(); + let _: Cache = Cache::new(filename).unwrap(); } #[test] @@ -393,7 +395,7 @@ mod tests { Uuid::new_v4(), rand::random::() )); - let cache: Cache = + let cache: Cache = Cache::with_time_to_live(filename.clone(), Duration::minutes(5)).unwrap(); assert_eq!(cache.path, filename); assert_eq!(cache.ttl, Duration::minutes(5)); @@ -506,7 +508,7 @@ mod tests { #[test] fn test_invalid_path() { - let cache: Result, Error> = + let cache: Result, Error> = Cache::new(PathBuf::from("invalid/path/db.sqlite")); assert!(cache.is_err()); @@ -520,7 +522,7 @@ mod tests { rand::random::() )); - let cache: Cache = + let cache: Cache = Cache::with_time_to_live(filename.clone(), Duration::minutes(5)).unwrap(); let key = Uuid::new_v4().to_string(); @@ -554,7 +556,7 @@ mod tests { rand::random::() )); - let cache: Cache = + let cache: Cache = Cache::with_time_to_live(filename.clone(), Duration::minutes(5)).unwrap(); let key = Uuid::new_v4().to_string();