Skip to content

Commit

Permalink
Set first caching of CachingMulti if only second one has value
Browse files Browse the repository at this point in the history
  • Loading branch information
gfusee committed Oct 22, 2024
1 parent 355ec8c commit ebfd068
Showing 1 changed file with 76 additions and 2 deletions.
78 changes: 76 additions & 2 deletions caching/src/multi/caching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,11 @@ where
FutureGetter: Future<Output=Result<T, Error>> + Send,
Error: From<NovaXError>
{
if let Some(cached_value) = self.get_cache(key).await? {
Ok(cached_value)
if let Some(cached_value_from_first) = self.first.get_cache(key).await? {
Ok(cached_value_from_first)
} else if let Some(cached_value_from_second) = self.second.get_cache(key).await? {
self.first.set_cache(key, &cached_value_from_second).await?;
Ok(cached_value_from_second)
} else {
let value = getter.await?;
self.set_cache(key, &value).await?;
Expand Down Expand Up @@ -206,6 +209,77 @@ mod test {
Ok(())
}

#[tokio::test]
async fn test_get_or_set_cache_first_and_second_have_value() -> Result<(), NovaXError> {
let key = 1u64;
let first_value = "test1".to_string();
let first_caching = CachingLocal::empty(CachingDurationStrategy::EachBlock);
first_caching.set_cache(key, &first_value).await?;

let second_value = "test2".to_string();
let second_caching = CachingLocal::empty(CachingDurationStrategy::EachBlock);
second_caching.set_cache(key, &second_value).await?;

let caching = CachingMulti::new(first_caching, second_caching);

let result = caching.get_or_set_cache::<String, _, NovaXError>(key, async {
panic!()
}).await?;

let expected = "test1".to_string();

assert_eq!(result, expected);

Ok(())
}

#[tokio::test]
async fn test_get_or_set_cache_only_second_has_value() -> Result<(), NovaXError> {
let key = 1u64;
let first_caching = CachingLocal::empty(CachingDurationStrategy::EachBlock);

let second_value = "test2".to_string();
let second_caching = CachingLocal::empty(CachingDurationStrategy::EachBlock);
second_caching.set_cache(key, &second_value).await?;

let caching = CachingMulti::new(first_caching.clone(), second_caching);

let result = caching.get_or_set_cache::<String, _, NovaXError>(key, async {
panic!()
}).await?;

let first_cached_value = first_caching.get_cache::<String>(key).await?.unwrap();
let expected = "test2".to_string();

assert_eq!(first_cached_value, expected);
assert_eq!(result, expected);

Ok(())
}

#[tokio::test]
async fn test_get_or_set_cache_no_cached_value() -> Result<(), NovaXError> {
let key = 1u64;
let first_caching = CachingLocal::empty(CachingDurationStrategy::EachBlock);
let second_caching = CachingLocal::empty(CachingDurationStrategy::EachBlock);

let caching = CachingMulti::new(first_caching.clone(), second_caching.clone());

let result = caching.get_or_set_cache::<String, _, NovaXError>(key, async {
Ok("test".to_string())
}).await?;

let first_cached_value = first_caching.get_cache::<String>(key).await?.unwrap();
let second_cached_value = second_caching.get_cache::<String>(key).await?.unwrap();
let expected = "test".to_string();

assert_eq!(first_cached_value, expected);
assert_eq!(second_cached_value, expected);
assert_eq!(result, expected);

Ok(())
}

#[tokio::test]
async fn test_clear() -> Result<(), NovaXError> {
let first_caching = CachingLocal::empty(CachingDurationStrategy::EachBlock);
Expand Down

0 comments on commit ebfd068

Please sign in to comment.