Skip to content

Commit

Permalink
Merge pull request #85
Browse files Browse the repository at this point in the history
Caching: replaced seconds with core::time::Duraion
  • Loading branch information
gfusee authored Apr 8, 2024
2 parents c059658 + 0a04edc commit 02bc283
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 52 deletions.
15 changes: 8 additions & 7 deletions caching/src/date/get_current_timestamp.rs
Original file line number Diff line number Diff line change
@@ -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<u64, NovaXError> {
pub(crate) fn get_current_timestamp() -> Result<Duration, NovaXError> {
let start = SystemTime::now();
let Ok(timestamp) = start.duration_since(UNIX_EPOCH) else { return Err(DateError::UnableToGetCurrentTimestamp.into())};

Ok(timestamp.as_secs())
Ok(timestamp)
}
}

Expand All @@ -21,21 +21,22 @@ 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<u64> = RefCell::new(0);
static MOCK_TIME: RefCell<Duration> = RefCell::new(Duration::from_secs(0));
}

pub(crate) fn get_current_timestamp() -> Result<u64, NovaXError> {
let mut time: u64 = 0;
pub(crate) fn get_current_timestamp() -> Result<Duration, NovaXError> {
let mut time: Duration = Duration::from_secs(0);
MOCK_TIME.with(|value| {
time = *value.borrow();
});

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;
Expand Down
40 changes: 21 additions & 19 deletions caching/src/local/caching_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Mutex<HashMap<u64, Vec<u8>>>>,
expiration_timestamp_map: Arc<Mutex<HashMap<u64, u64>>>
expiration_timestamp_map: Arc<Mutex<HashMap<u64, Duration>>>
}

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()))
Expand All @@ -41,12 +42,12 @@ impl CachingLocal {
async fn set_value<T: Serialize + DeserializeOwned>(&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
};
Expand Down Expand Up @@ -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,
Expand All @@ -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()
Expand All @@ -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;
Expand Down Expand Up @@ -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::<String>(key).await?;
let expected = Some("test".to_string());
Expand All @@ -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::<String>(key).await?;
let expected = None;
Expand All @@ -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::<String>(key).await?;
let expected = Some("test".to_string());
Expand All @@ -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::<String>(key).await?;
let expected = Some("test".to_string());
Expand All @@ -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::<String>(key).await?;
let expected = None;
Expand Down Expand Up @@ -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,
Expand Down
29 changes: 15 additions & 14 deletions caching/src/locked/caching.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -80,7 +81,7 @@ impl<C: CachingStrategy> CachingStrategy for CachingLocked<C> {
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))
}

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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::<String>(key).await?;
let expected = Some("test".to_string());
Expand All @@ -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::<String>(key).await?;
let expected = None;
Expand All @@ -244,15 +245,15 @@ 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;
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::<String>(key).await?;
let expected = Some("test".to_string());
Expand All @@ -264,15 +265,15 @@ 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;
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::<String>(key).await?;
let expected = Some("test".to_string());
Expand All @@ -284,15 +285,15 @@ 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;
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::<String>(key).await?;
let expected = None;
Expand Down Expand Up @@ -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,
Expand Down
Loading

0 comments on commit 02bc283

Please sign in to comment.