From 1beb83dcb7fb6922f353c7fdf8048106906a2376 Mon Sep 17 00:00:00 2001 From: FiveMovesAhead Date: Fri, 7 Jun 2024 01:47:18 +1000 Subject: [PATCH] Add cache for benchmarker requests. --- tig-benchmarker/src/benchmarker/query_data.rs | 60 +++++++++++-------- 1 file changed, 36 insertions(+), 24 deletions(-) diff --git a/tig-benchmarker/src/benchmarker/query_data.rs b/tig-benchmarker/src/benchmarker/query_data.rs index 7d797406..5c7de858 100644 --- a/tig-benchmarker/src/benchmarker/query_data.rs +++ b/tig-benchmarker/src/benchmarker/query_data.rs @@ -1,33 +1,45 @@ -use std::collections::HashMap; - use super::{api, player_id, QueryData, Result}; -use crate::future_utils; +use crate::future_utils::{join, Mutex}; +use once_cell::sync::OnceCell; +use std::collections::HashMap; use tig_api::*; use tig_structs::core::*; +static CACHE: OnceCell>> = OnceCell::new(); + pub async fn execute() -> Result { + let cache = CACHE.get_or_init(|| Mutex::new(HashMap::new())); let latest_block = query_latest_block().await?; - let results = future_utils::join( - query_algorithms(latest_block.id.clone()), - query_player_data(latest_block.id.clone()), - query_benchmarks(latest_block.id.clone()), - query_challenges(latest_block.id.clone()), - ) - .await?; - let (algorithms_by_challenge, download_urls) = results.0?; - let player_data = results.1?; - let (benchmarks, proofs, frauds) = results.2?; - let challenges = results.3?; - Ok(QueryData { - latest_block, - algorithms_by_challenge, - player_data, - download_urls, - benchmarks, - proofs, - frauds, - challenges, - }) + let latest_block_id = latest_block.id.clone(); + let mut cache = cache.lock().await; + if !cache.contains_key(&latest_block.id) { + cache.clear(); + let results = join( + query_algorithms(latest_block.id.clone()), + query_player_data(latest_block.id.clone()), + query_benchmarks(latest_block.id.clone()), + query_challenges(latest_block.id.clone()), + ) + .await?; + let (algorithms_by_challenge, download_urls) = results.0?; + let player_data = results.1?; + let (benchmarks, proofs, frauds) = results.2?; + let challenges = results.3?; + cache.insert( + latest_block.id.clone(), + QueryData { + latest_block, + algorithms_by_challenge, + player_data, + download_urls, + benchmarks, + proofs, + frauds, + challenges, + }, + ); + } + Ok(cache.get(&latest_block_id).unwrap().clone()) } async fn query_latest_block() -> Result {