added cache to prevent the server from blocking itself if too many requests

This commit is contained in:
2026-05-04 20:26:31 +02:00
parent 23d24d81b1
commit 33eb3e4f3f
3 changed files with 162 additions and 146 deletions

View File

@@ -1,13 +1,17 @@
use axum::{Router, routing::get};
use clap::Parser;
use lazy_static::lazy_static;
use std::sync::Arc;
use std::{
sync::Arc,
time::{Duration, Instant},
};
use tera::Tera;
use tokio::sync::RwLock;
mod handlers;
use handlers::home_handler;
use crate::handlers::{analytics_handler, report_details_handler};
use crate::handlers::{Report, analytics_handler, report_details_handler};
lazy_static! {
pub static ref TEMPLATES: Tera = {
@@ -43,8 +47,15 @@ pub struct Cli {
verbose: bool,
}
pub struct Cache<T> {
pub data: T,
pub updated_at: Instant,
}
struct AppState {
app_name: String,
pub reports_cache: RwLock<Option<Cache<Vec<Report>>>>,
pub cache_duration: Duration,
}
#[tokio::main]
@@ -57,6 +68,8 @@ async fn main() -> anyhow::Result<()> {
let shared_state = Arc::new(AppState {
app_name: "Slimes Leaderboards".to_string(),
reports_cache: RwLock::new(None),
cache_duration: Duration::from_secs(30),
});
let app = Router::new()