- add: redis cache refresh function sets the ttl for a key
- update: session service refresh session function now just updates the ttl of the session instead of calling set
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
use crate::api::session_service_server::SessionService;
|
||||
use crate::api::{CreateSessionRequest, DeleteSessionRequest, GetSessionRequest, SessionResponse};
|
||||
use crate::api::{CreateSessionRequest, DeleteSessionRequest, GetSessionRequest, RefreshSessionResponse, SessionResponse};
|
||||
use crate::common::Empty;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::Mutex;
|
||||
use tonic::{Request, Response, Status};
|
||||
use tracing::debug;
|
||||
use utils::redis_cache::{Cache, RedisCache};
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
@@ -100,34 +101,25 @@ impl SessionService for SessionServiceImpl {
|
||||
|
||||
Ok(Response::new(Empty {}))
|
||||
}
|
||||
|
||||
async fn refresh_session(
|
||||
&self,
|
||||
request: Request<GetSessionRequest>,
|
||||
) -> Result<Response<SessionResponse>, Status> {
|
||||
) -> Result<Response<RefreshSessionResponse>, Status> {
|
||||
let req = request.into_inner();
|
||||
let conn = self.redis.lock().await;
|
||||
match conn.refresh(&req.session_id, 300).await {
|
||||
Ok(Response) => {
|
||||
let response = RefreshSessionResponse {
|
||||
valid: true
|
||||
};
|
||||
|
||||
if let Some(session_data) = conn
|
||||
.get::<String>(&req.session_id)
|
||||
.await
|
||||
.map_err(|_| Status::internal("Failed to fetch session from Redis"))?
|
||||
{
|
||||
let _ = conn.update(&req.session_id, Some(&session_data), Some(300));
|
||||
let session: Session = serde_json::from_str(&session_data)
|
||||
.map_err(|_| Status::internal("Failed to deserialize session"))?;
|
||||
|
||||
let response = SessionResponse {
|
||||
session_id: req.session_id,
|
||||
user_id: session.user_id,
|
||||
username: session.username,
|
||||
character_id: session.character_id,
|
||||
login_time: session.login_time,
|
||||
ip_address: session.ip_address,
|
||||
};
|
||||
|
||||
Ok(Response::new(response))
|
||||
} else {
|
||||
Err(Status::not_found("Session not found"))
|
||||
debug!("Session refreshed: {:?}", response);
|
||||
Ok(Response::new(response))
|
||||
}
|
||||
Err(e) => {
|
||||
Err(Status::unknown(e.to_string()))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user