From e5cdb84c52ef5540b805c145ff26a63c3e8bf68e Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Sun, 12 Mar 2023 04:34:50 -0400 Subject: [PATCH] correct cache key --- .../lib/redis_common/src/redis_cache_keys.rs | 4 ++++ .../users/src/utils/session_checker.rs | 19 ++++++++++++------- .../service/web/storyteller_web/src/main.rs | 5 ++++- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/crates/lib/redis_common/src/redis_cache_keys.rs b/crates/lib/redis_common/src/redis_cache_keys.rs index 9962ccfaff..5ec9e77a5f 100644 --- a/crates/lib/redis_common/src/redis_cache_keys.rs +++ b/crates/lib/redis_common/src/redis_cache_keys.rs @@ -12,4 +12,8 @@ impl RedisCacheKeys { pub fn session_record_light(session_token: &str) -> String { format!("ses_light:{}", session_token) } + + pub fn session_record_user(session_token: &str) -> String { + format!("ses_user:{}", session_token) + } } diff --git a/crates/service/plugins/users/src/utils/session_checker.rs b/crates/service/plugins/users/src/utils/session_checker.rs index 1a1a19164d..60297a875d 100644 --- a/crates/service/plugins/users/src/utils/session_checker.rs +++ b/crates/service/plugins/users/src/utils/session_checker.rs @@ -1,7 +1,7 @@ // NB: Incrementally getting rid of build warnings... -//#![forbid(unused_imports)] -//#![forbid(unused_mut)] -//#![forbid(unused_variables)] +#![forbid(unused_imports)] +#![forbid(unused_mut)] +#![forbid(unused_variables)] use actix_web::HttpRequest; use container_common::anyhow_result::AnyhowResult; @@ -14,8 +14,7 @@ use log::warn; use redis_caching::redis_ttl_cache::{RedisTtlCache, RedisTtlCacheConnection}; use redis_common::redis_cache_keys::RedisCacheKeys; use sqlx::pool::PoolConnection; -use sqlx::{MySqlPool, MySql, Acquire, Executor}; -use sqlx_mysql_helpers::any_connection::MySqlHandleMixedRef; +use sqlx::{MySqlPool, MySql, Executor}; #[derive(Clone)] pub struct SessionChecker { @@ -42,7 +41,7 @@ impl SessionChecker { // ==================== SessionRecord ==================== - #[deprecated = "Use the PoolConnection method instead of the MySqlPool one."] + //#[deprecated = "Use the PoolConnection method instead of the MySqlPool one."] pub async fn maybe_get_session_light( &self, request: &HttpRequest, @@ -52,6 +51,7 @@ impl SessionChecker { self.do_session_light_lookup_and_cookie_decode(request, pool).await } + pub async fn maybe_get_session_light_from_connection( &self, request: &HttpRequest, @@ -61,6 +61,7 @@ impl SessionChecker { self.do_session_light_lookup_and_cookie_decode(request, mysql_connection).await } + async fn do_session_light_lookup_and_cookie_decode<'e, 'c : 'e, E>( &self, request: &HttpRequest, @@ -76,6 +77,7 @@ impl SessionChecker { self.do_session_light_lookup(mysql_executor, &session_token).await } + async fn do_session_light_lookup<'e, 'c : 'e, E>( &self, mysql_executor: E, @@ -109,6 +111,7 @@ impl SessionChecker { self.do_user_session_lookup_and_cookie_decode(request, pool).await } + pub async fn maybe_get_user_session_from_connection( &self, request: &HttpRequest, @@ -118,6 +121,7 @@ impl SessionChecker { self.do_user_session_lookup_and_cookie_decode(request, mysql_connection).await } + async fn do_user_session_lookup_and_cookie_decode<'e, 'c : 'e, E>( &self, request: &HttpRequest, @@ -133,6 +137,7 @@ impl SessionChecker { self.do_user_session_lookup(mysql_executor, &session_token).await } + async fn do_user_session_lookup<'e, 'c : 'e, E>( &self, mysql_executor: E, @@ -145,7 +150,7 @@ impl SessionChecker { get_user_session_by_token(mysql_executor, session_token).await } Some(mut redis_ttl_cache) => { - let cache_key = RedisCacheKeys::session_record_light(session_token); + let cache_key = RedisCacheKeys::session_record_user(session_token); redis_ttl_cache.lazy_load_if_not_cached(&cache_key, move || { get_user_session_by_token(mysql_executor, session_token) }).await diff --git a/crates/service/web/storyteller_web/src/main.rs b/crates/service/web/storyteller_web/src/main.rs index b599d4dd5e..229502d47b 100644 --- a/crates/service/web/storyteller_web/src/main.rs +++ b/crates/service/web/storyteller_web/src/main.rs @@ -233,7 +233,10 @@ async fn main() -> AnyhowResult<()> { easyenv::get_env_string_or_default("WEBSITE_HOMEPAGE_REDIRECT", "https://vo.codes/"); let cookie_manager = SessionCookieManager::new(&cookie_domain, &hmac_secret); - let session_checker = SessionChecker::new(&cookie_manager); + let session_checker = SessionChecker::new_with_cache( + &cookie_manager, + redis_ttl_cache.clone(), + ); let access_key = easyenv::get_env_string_required(ENV_ACCESS_KEY)?; let secret_key = easyenv::get_env_string_required(ENV_SECRET_KEY)?;