From ed4e07d2e63af1584a262037781729a5144a5502 Mon Sep 17 00:00:00 2001 From: Ellie Huxtable Date: Thu, 21 Apr 2022 08:03:39 +0100 Subject: Use the count cache (#312) * Use the count cache By default read from the count cache - if there is no value there, then do a full COUNT. The cache will be filled when the user posts up some more history * clean up server db error handling Co-authored-by: Conrad Ludgate --- atuin-server/src/handlers/history.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'atuin-server/src/handlers/history.rs') diff --git a/atuin-server/src/handlers/history.rs b/atuin-server/src/handlers/history.rs index fde7cf2d..4fa2a962 100644 --- a/atuin-server/src/handlers/history.rs +++ b/atuin-server/src/handlers/history.rs @@ -13,10 +13,17 @@ pub async fn count( user: User, db: Extension, ) -> Result, ErrorResponseStatus<'static>> { - match db.count_history(&user).await { + match db.count_history_cached(&user).await { + // By default read out the cached value Ok(count) => Ok(Json(CountResponse { count })), - Err(_) => Err(ErrorResponse::reply("failed to query history count") - .with_status(StatusCode::INTERNAL_SERVER_ERROR)), + + // If that fails, fallback on a full COUNT. Cache is built on a POST + // only + Err(_) => match db.count_history(&user).await { + Ok(count) => Ok(Json(CountResponse { count })), + Err(_) => Err(ErrorResponse::reply("failed to query history count") + .with_status(StatusCode::INTERNAL_SERVER_ERROR)), + }, } } -- cgit v1.3.1