- add: Refresh Session call to actually refresh the cache session.

This commit is contained in:
2025-03-21 23:23:38 -04:00
parent 4046f56191
commit 9e984d2aa8
5 changed files with 68 additions and 6 deletions

View File

@@ -2,7 +2,7 @@ use crate::grpc::database_service::MyDatabaseService;
use crate::grpc::session_service_server::SessionService;
use tonic::{Request, Response, Status};
use tracing::debug;
use crate::grpc::{GetSessionRequest, GetSessionResponse};
use crate::grpc::{GetSessionRequest, GetSessionResponse, RefreshSessionRequest, RefreshSessionResponse};
#[tonic::async_trait]
impl SessionService for MyDatabaseService {
@@ -22,4 +22,19 @@ impl SessionService for MyDatabaseService {
user_id: session.user_id,
}))
}
async fn refresh_session(&self, request: Request<RefreshSessionRequest>) -> Result<Response<RefreshSessionResponse>, Status> {
let req = request.into_inner();
debug!("get_session: {:?}", req);
let session = self.db.session_repo.refresh_session(&req.session_id).await
.map_err(|_| Status::not_found("Session not found"))?;
let valid = true;
debug!("session: {:?}", session);
Ok(Response::new(RefreshSessionResponse {
valid
}))
}
}

View File

@@ -42,7 +42,34 @@ impl SessionRepository {
debug!("session: {:?}", session);
self.cache.lock().await
.set(&cache_key, &session, 0).await
.set(&cache_key, &session, 300).await
.map_err(|_| sqlx::Error::RowNotFound)?;
Ok(session)
}
pub async fn refresh_session(&self, session_id: &str) -> Result<Session, sqlx::Error> {
let cache_key = format!("session:{}", session_id);
if let Some(session) = self.cache.lock().await
.get::<Session>(&cache_key).await
.map_err(|_| sqlx::Error::RowNotFound)?
{
self.cache.lock().await
.refresh(&cache_key, 300).await
.map_err(|_| sqlx::Error::RowNotFound)?;
return Ok(session);
}
// Check to make sure the session is still valid
let session = sqlx::query_as::<_, Session>(
"SELECT id, \"userId\" as user_id FROM session WHERE id = $1",
)
.bind(session_id)
.fetch_one(&self.pool)
.await?;
self.cache.lock().await
.set(&cache_key, &session, 300).await
.map_err(|_| sqlx::Error::RowNotFound)?;
Ok(session)
}