Skip to content

Commit

Permalink
timed cache refresh to midnight
Browse files Browse the repository at this point in the history
  • Loading branch information
shufps committed Jul 8, 2024
1 parent 3bb48f2 commit f62bb55
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/bin/inx-chronicle/api/explorer/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// SPDX-License-Identifier: Apache-2.0

use std::str::FromStr;
use std::time::SystemTime;

use tracing::info;

use axum::{extract::Path, routing::get, Extension};
use chronicle::{
Expand Down Expand Up @@ -333,6 +336,13 @@ struct TokenCacheData {
data: TokenDistributionResponse,
}

fn calculate_seconds_until_midnight() -> u64 {
let now = SystemTime::now();
let since_epoch = now.duration_since(SystemTime::UNIX_EPOCH).expect("Time went backwards");
let seconds_today = since_epoch.as_secs() % 86400;
86400 - seconds_today
}

static RICHEST_ADDRESSES_CACHE: Lazy<RwLock<Option<RichestCacheData>>> = Lazy::new(|| RwLock::new(None));
static TOKEN_DISTRIBUTION_CACHE: Lazy<RwLock<Option<TokenCacheData>>> = Lazy::new(|| RwLock::new(None));

Expand All @@ -342,6 +352,7 @@ async fn richest_addresses_ledger_analytics(
) -> ApiResult<RichestAddressesResponse> {
let ledger_index = resolve_ledger_index(&database, ledger_index).await?;
let cache = RICHEST_ADDRESSES_CACHE.read().await;
let seconds_until_midnight = calculate_seconds_until_midnight();

if let Some(cached_data) = &*cache {
if cached_data.last_updated.elapsed() < Duration::from_secs(86400) {
Expand All @@ -351,6 +362,9 @@ async fn richest_addresses_ledger_analytics(

drop(cache); // release the read lock

info!("refreshing richest-addresses cache ...");
let refresh_start = SystemTime::now();

let res = database
.collection::<OutputCollection>()
.get_richest_addresses(ledger_index, top)
Expand Down Expand Up @@ -385,6 +399,10 @@ async fn richest_addresses_ledger_analytics(
data: response.clone(),
});

let refresh_elapsed = refresh_start.elapsed().unwrap();
info!("refreshing richest-addresses cache done. Took {:?}", refresh_elapsed);
info!("next refresh in {} seconds", seconds_until_midnight);

Ok(response)
}

Expand All @@ -395,6 +413,7 @@ async fn token_distribution_ledger_analytics(
let ledger_index = resolve_ledger_index(&database, ledger_index).await?;
let cache = TOKEN_DISTRIBUTION_CACHE.read().await;

let seconds_until_midnight = calculate_seconds_until_midnight();
if let Some(cached_data) = &*cache {
if cached_data.last_updated.elapsed() < Duration::from_secs(86400) {
return Ok(cached_data.data.clone());
Expand All @@ -403,6 +422,9 @@ async fn token_distribution_ledger_analytics(

drop(cache); // release the read lock

info!("refreshing token-distribution cache ...");
let refresh_start = SystemTime::now();

let res = database
.collection::<OutputCollection>()
.get_token_distribution(ledger_index)
Expand All @@ -419,6 +441,10 @@ async fn token_distribution_ledger_analytics(
data: response.clone(),
});

let refresh_elapsed = refresh_start.elapsed().unwrap();
info!("refreshing token-distribution cache done. Took {:?}", refresh_elapsed);
info!("next refresh in {} seconds", seconds_until_midnight);

Ok(response)
}

Expand Down

0 comments on commit f62bb55

Please sign in to comment.