diff --git a/caching/src/date/get_current_timestamp.rs b/caching/src/date/get_current_timestamp.rs index 8024c63..7ce27c1 100644 --- a/caching/src/date/get_current_timestamp.rs +++ b/caching/src/date/get_current_timestamp.rs @@ -1,14 +1,14 @@ #[cfg(not(test))] mod implementation { - use std::time::{SystemTime, UNIX_EPOCH}; + use std::time::{Duration, SystemTime, UNIX_EPOCH}; use novax::errors::NovaXError; use novax::errors::DateError; - pub(crate) fn get_current_timestamp() -> Result { + pub(crate) fn get_current_timestamp() -> Result { let start = SystemTime::now(); let Ok(timestamp) = start.duration_since(UNIX_EPOCH) else { return Err(DateError::UnableToGetCurrentTimestamp.into())}; - Ok(timestamp.as_secs()) + Ok(timestamp) } } @@ -21,13 +21,14 @@ pub(crate) use implementation::set_mock_time; mod implementation { use novax::errors::NovaXError; use std::cell::RefCell; + use core::time::Duration; thread_local! { - static MOCK_TIME: RefCell = RefCell::new(0); + static MOCK_TIME: RefCell = RefCell::new(Duration::from_secs(0)); } - pub(crate) fn get_current_timestamp() -> Result { - let mut time: u64 = 0; + pub(crate) fn get_current_timestamp() -> Result { + let mut time: Duration = Duration::from_secs(0); MOCK_TIME.with(|value| { time = *value.borrow(); }); @@ -35,7 +36,7 @@ mod implementation { Ok(time) } - pub(crate) fn set_mock_time(new_time: u64) { + pub(crate) fn set_mock_time(new_time: Duration) { MOCK_TIME.with(|value| { let mut mock_time = value.borrow_mut(); *mock_time = new_time; diff --git a/caching/src/local/caching_local.rs b/caching/src/local/caching_local.rs index fdca7e6..a293bd4 100644 --- a/caching/src/local/caching_local.rs +++ b/caching/src/local/caching_local.rs @@ -2,6 +2,7 @@ use async_trait::async_trait; use std::sync::Arc; use std::collections::HashMap; use std::future::Future; +use std::time::Duration; use serde::Serialize; use serde::de::DeserializeOwned; use tokio::sync::Mutex; @@ -12,16 +13,16 @@ use crate::date::get_current_timestamp::get_current_timestamp; #[derive(Clone, Debug)] pub struct CachingLocal { - duration: u64, + duration: Duration, until_next_block: bool, value_map: Arc>>>, - expiration_timestamp_map: Arc>> + expiration_timestamp_map: Arc>> } impl CachingLocal { pub fn empty() -> CachingLocal { CachingLocal { - duration: 0, + duration: Duration::from_secs(0), until_next_block: false, value_map: Arc::new(Mutex::new(HashMap::new())), expiration_timestamp_map: Arc::new(Mutex::new(HashMap::new())) @@ -41,12 +42,12 @@ impl CachingLocal { async fn set_value(&self, key: u64, value: &T) -> Result<(), NovaXError> { let current_timestamp = get_current_timestamp()?; let expiration_timestamp = if self.until_next_block { - let mut timestamp = current_timestamp + 1; + let mut timestamp = current_timestamp.as_secs() + 1; while timestamp % 6 != 5 { timestamp += 1 } - timestamp + Duration::from_secs(timestamp) } else { current_timestamp + self.duration }; @@ -100,7 +101,7 @@ impl CachingStrategy for CachingLocal { Ok(()) } - fn with_duration(&self, duration: u64) -> Self { + fn with_duration(&self, duration: Duration) -> Self { CachingLocal { duration, until_next_block: self.until_next_block, @@ -111,7 +112,7 @@ impl CachingStrategy for CachingLocal { fn until_next_block(&self) -> Self { CachingLocal { - duration: 0, + duration: Duration::from_secs(0), until_next_block: true, value_map: self.value_map.clone(), expiration_timestamp_map: self.expiration_timestamp_map.clone() @@ -121,6 +122,7 @@ impl CachingStrategy for CachingLocal { #[cfg(test)] mod test { + use std::time::Duration; use novax::caching::CachingStrategy; use novax::errors::NovaXError; use crate::date::get_current_timestamp::set_mock_time; @@ -156,13 +158,13 @@ mod test { #[tokio::test] async fn test_get_cache_before_expiration() -> Result<(), NovaXError> { - let caching = CachingLocal::empty().with_duration(10); + let caching = CachingLocal::empty().with_duration(Duration::from_secs(10)); let key = 1; let value = "test".to_string(); caching.set_cache(key, &value).await?; - set_mock_time(10); + set_mock_time(Duration::from_secs(10)); let result = caching.get_cache::(key).await?; let expected = Some("test".to_string()); @@ -175,13 +177,13 @@ mod test { #[tokio::test] async fn test_get_cache_after_expiration() -> Result<(), NovaXError> { - let caching = CachingLocal::empty().with_duration(10); + let caching = CachingLocal::empty().with_duration(Duration::from_secs(10)); let key = 1; let value = "test".to_string(); caching.set_cache(key, &value).await?; - set_mock_time(11); + set_mock_time(Duration::from_secs(11)); let result = caching.get_cache::(key).await?; let expected = None; @@ -193,14 +195,14 @@ mod test { #[tokio::test] async fn test_get_cache_start_of_block() -> Result<(), NovaXError> { - set_mock_time(0); + set_mock_time(Duration::from_secs(0)); let caching = CachingLocal::empty().until_next_block(); let key = 1; let value = "test".to_string(); caching.set_cache(key, &value).await?; - set_mock_time(5); + set_mock_time(Duration::from_secs(5)); let result = caching.get_cache::(key).await?; let expected = Some("test".to_string()); @@ -212,14 +214,14 @@ mod test { #[tokio::test] async fn test_get_cache_same_block() -> Result<(), NovaXError> { - set_mock_time(3); + set_mock_time(Duration::from_secs(3)); let caching = CachingLocal::empty().until_next_block(); let key = 1; let value = "test".to_string(); caching.set_cache(key, &value).await?; - set_mock_time(5); + set_mock_time(Duration::from_secs(5)); let result = caching.get_cache::(key).await?; let expected = Some("test".to_string()); @@ -231,14 +233,14 @@ mod test { #[tokio::test] async fn test_get_cache_next_block() -> Result<(), NovaXError> { - set_mock_time(3); + set_mock_time(Duration::from_secs(3)); let caching = CachingLocal::empty().until_next_block(); let key = 1; let value = "test".to_string(); caching.set_cache(key, &value).await?; - set_mock_time(6); + set_mock_time(Duration::from_secs(6)); let result = caching.get_cache::(key).await?; let expected = None; @@ -290,12 +292,12 @@ mod test { #[tokio::test] async fn test_get_or_set_cache_with_previous_value_after_expiration() -> Result<(), NovaXError> { - let caching = CachingLocal::empty().with_duration(10); + let caching = CachingLocal::empty().with_duration(Duration::from_secs(10)); let key = 1; caching.set_cache(key, &"old value".to_string()).await?; - set_mock_time(11); + set_mock_time(Duration::from_secs(11)); let result = caching.get_or_set_cache( key, diff --git a/caching/src/locked/caching.rs b/caching/src/locked/caching.rs index 86a8e58..8ad52f1 100644 --- a/caching/src/locked/caching.rs +++ b/caching/src/locked/caching.rs @@ -1,6 +1,7 @@ use std::collections::HashMap; use std::future::Future; use std::sync::Arc; +use std::time::Duration; use async_trait::async_trait; use serde::de::DeserializeOwned; use serde::Serialize; @@ -80,7 +81,7 @@ impl CachingStrategy for CachingLocked { self.caching.clear().await } - fn with_duration(&self, duration: u64) -> Self { + fn with_duration(&self, duration: Duration) -> Self { CachingLocked::new(self.caching.with_duration(duration)) } @@ -141,7 +142,7 @@ mod test { self.caching.clear().await } - fn with_duration(&self, duration: u64) -> Self { + fn with_duration(&self, duration: Duration) -> Self { CachingLocalDelayedSet { caching: self.caching.with_duration(duration) } @@ -205,14 +206,14 @@ mod test { #[tokio::test] async fn test_get_cache_before_expiration() -> Result<(), NovaXError> { - let caching_local = CachingLocal::empty().with_duration(10); + let caching_local = CachingLocal::empty().with_duration(Duration::from_secs(10)); let caching = CachingLocked::new(caching_local); let key = 1; let value = "test".to_string(); caching.set_cache(key, &value).await?; - set_mock_time(10); + set_mock_time(Duration::from_secs(10)); let result = caching.get_cache::(key).await?; let expected = Some("test".to_string()); @@ -225,14 +226,14 @@ mod test { #[tokio::test] async fn test_get_cache_after_expiration() -> Result<(), NovaXError> { - let caching_local = CachingLocal::empty().with_duration(10); + let caching_local = CachingLocal::empty().with_duration(Duration::from_secs(10)); let caching = CachingLocked::new(caching_local); let key = 1; let value = "test".to_string(); caching.set_cache(key, &value).await?; - set_mock_time(11); + set_mock_time(Duration::from_secs(11)); let result = caching.get_cache::(key).await?; let expected = None; @@ -244,7 +245,7 @@ mod test { #[tokio::test] async fn test_get_cache_start_of_block() -> Result<(), NovaXError> { - set_mock_time(0); + set_mock_time(Duration::from_secs(0)); let caching_local = CachingLocal::empty().until_next_block(); let caching = CachingLocked::new(caching_local); let key = 1; @@ -252,7 +253,7 @@ mod test { caching.set_cache(key, &value).await?; - set_mock_time(5); + set_mock_time(Duration::from_secs(5)); let result = caching.get_cache::(key).await?; let expected = Some("test".to_string()); @@ -264,7 +265,7 @@ mod test { #[tokio::test] async fn test_get_cache_same_block() -> Result<(), NovaXError> { - set_mock_time(3); + set_mock_time(Duration::from_secs(3)); let caching_local = CachingLocal::empty().until_next_block(); let caching = CachingLocked::new(caching_local); let key = 1; @@ -272,7 +273,7 @@ mod test { caching.set_cache(key, &value).await?; - set_mock_time(5); + set_mock_time(Duration::from_secs(5)); let result = caching.get_cache::(key).await?; let expected = Some("test".to_string()); @@ -284,7 +285,7 @@ mod test { #[tokio::test] async fn test_get_cache_next_block() -> Result<(), NovaXError> { - set_mock_time(3); + set_mock_time(Duration::from_secs(3)); let caching_local = CachingLocal::empty().until_next_block(); let caching = CachingLocked::new(caching_local); let key = 1; @@ -292,7 +293,7 @@ mod test { caching.set_cache(key, &value).await?; - set_mock_time(6); + set_mock_time(Duration::from_secs(6)); let result = caching.get_cache::(key).await?; let expected = None; @@ -346,13 +347,13 @@ mod test { #[tokio::test] async fn test_get_or_set_cache_with_previous_value_after_expiration() -> Result<(), NovaXError> { - let caching_local = CachingLocal::empty().with_duration(10); + let caching_local = CachingLocal::empty().with_duration(Duration::from_secs(10)); let caching = CachingLocked::new(caching_local); let key = 1; caching.set_cache(key, &"old value".to_string()).await?; - set_mock_time(11); + set_mock_time(Duration::from_secs(11)); let result = caching.get_or_set_cache( key, diff --git a/caching/src/multi/caching.rs b/caching/src/multi/caching.rs index ed02fb6..d70559e 100644 --- a/caching/src/multi/caching.rs +++ b/caching/src/multi/caching.rs @@ -1,4 +1,5 @@ use std::future::Future; +use std::time::Duration; use async_trait::async_trait; use serde::de::DeserializeOwned; use serde::Serialize; @@ -87,7 +88,7 @@ where Ok(()) } - fn with_duration(&self, duration: u64) -> Self { + fn with_duration(&self, duration: Duration) -> Self { CachingMulti::new( self.first.with_duration(duration), self.second.with_duration(duration) @@ -104,6 +105,7 @@ where #[cfg(test)] mod test { + use std::time::Duration; use novax::caching::CachingStrategy; use novax::errors::NovaXError; use crate::local::caching_local::CachingLocal; @@ -241,10 +243,10 @@ mod test { first_caching.clone(), second_caching.clone() ) - .with_duration(10); + .with_duration(Duration::from_secs(10)); caching.set_cache(key, &value).await?; - set_mock_time(10); + set_mock_time(Duration::from_secs(10)); let first_result = first_caching.get_cache::(key).await?; let second_result = second_caching.get_cache::(key).await?; @@ -268,10 +270,10 @@ mod test { first_caching.clone(), second_caching.clone() ) - .with_duration(10); + .with_duration(Duration::from_secs(10)); caching.set_cache(key, &value).await?; - set_mock_time(11); + set_mock_time(Duration::from_secs(11)); let first_result = first_caching.get_cache::(key).await?; let second_result = second_caching.get_cache::(key).await?; @@ -285,7 +287,7 @@ mod test { #[tokio::test] async fn test_until_next_block_current_block() -> Result<(), NovaXError> { - set_mock_time(3); + set_mock_time(Duration::from_secs(3)); let key = 1u64; let value = "test".to_string(); @@ -299,7 +301,7 @@ mod test { .until_next_block(); caching.set_cache(key, &value).await?; - set_mock_time(5); + set_mock_time(Duration::from_secs(5)); let first_result = first_caching.get_cache::(key).await?; let second_result = second_caching.get_cache::(key).await?; @@ -313,7 +315,7 @@ mod test { #[tokio::test] async fn test_until_next_block_next_block() -> Result<(), NovaXError> { - set_mock_time(3); + set_mock_time(Duration::from_secs(3)); let key = 1u64; let value = "test".to_string(); @@ -327,7 +329,7 @@ mod test { .until_next_block(); caching.set_cache(key, &value).await?; - set_mock_time(6); + set_mock_time(Duration::from_secs(6)); let first_result = first_caching.get_cache::(key).await?; let second_result = second_caching.get_cache::(key).await?; diff --git a/core/src/caching/caching_none.rs b/core/src/caching/caching_none.rs index a57b6af..fe6a18e 100644 --- a/core/src/caching/caching_none.rs +++ b/core/src/caching/caching_none.rs @@ -1,5 +1,6 @@ use async_trait::async_trait; use std::future::Future; +use std::time::Duration; use serde::Serialize; use serde::de::DeserializeOwned; use crate::caching::caching_strategy::CachingStrategy; @@ -44,7 +45,7 @@ impl CachingStrategy for CachingNone { /// Attempts to create a new `CachingNone` instance with a specified cache duration, /// but returns a new unchanged `CachingNone` instance since `CachingNone` does not support cache duration. - fn with_duration(&self, _duration: u64) -> Self { + fn with_duration(&self, _duration: Duration) -> Self { CachingNone } diff --git a/core/src/caching/caching_strategy.rs b/core/src/caching/caching_strategy.rs index 7707d72..88e0278 100644 --- a/core/src/caching/caching_strategy.rs +++ b/core/src/caching/caching_strategy.rs @@ -1,5 +1,6 @@ use std::fmt::Debug; use std::future::Future; +use std::time::Duration; use async_trait::async_trait; use serde::de::DeserializeOwned; use serde::Serialize; @@ -63,11 +64,11 @@ pub trait CachingStrategy: Clone + Send + Sync + Debug { /// Creates a new `CachingStrategy` instance with a specified cache duration. /// /// # Parameters - /// - `duration`: The duration for which cache entries should be kept, in seconds. + /// - `duration`: The duration for which cache entries should be kept. /// /// # Returns /// - A new `CachingStrategy` instance with the specified cache duration. - fn with_duration(&self, duration: u64) -> Self; + fn with_duration(&self, duration: Duration) -> Self; /// Creates a new `CachingStrategy` instance that caches values until the next blockchain block. ///