forked from huytd/tokyo-rs
-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: implement redis caching actor #4
Open
haongo138
wants to merge
8
commits into
webuild-community:master
Choose a base branch
from
haongo138:feat/implement-redis-caching-actor
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7a13df6
chore: caching room session w/ redis
haongo138 a915c49
chore: cache room's status
haongo138 dd4e78a
chore: refactor based on lastest feat
haongo138 40bc5cb
chore: revert /client changes
haongo138 9125dd0
chore: initalize only single redis connection
haongo138 fe1638f
chore: update redis_actor to more abstraction name
haongo138 190ad90
chore: extract more of player info into scoreboard
haongo138 43f1268
chore: remove unnecessary comments
haongo138 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,4 @@ listenfd = "0.3" | |
failure = "0.1" | ||
futures = "0.1" | ||
url = "1.7" | ||
redis = "0.24.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
pub mod client_ws_actor; | ||
pub mod game_actor; | ||
pub mod store_actor; | ||
|
||
pub use client_ws_actor::ClientWsActor; | ||
pub use game_actor::GameActor; | ||
|
||
pub mod room_manager_actor; | ||
pub use room_manager_actor::{CreateRoom, JoinRoom, ListRooms, RoomManagerActor}; | ||
pub use store_actor::StoreActor; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
use std::collections::HashMap; | ||
|
||
use actix::prelude::*; | ||
use redis::{Client, Commands, Connection}; | ||
|
||
use crate::models::messages::{GetScoreboardCommand, SetPlayerInfoCommand, SetScoreboardCommand, GetMultiplePlayerInfo}; | ||
|
||
#[derive(Debug)] | ||
pub struct StoreActor { | ||
client: Client, | ||
} | ||
|
||
impl StoreActor { | ||
pub fn new(redis_url: String) -> StoreActor { | ||
let client = Client::open(redis_url).expect("Failed to create Redis client"); | ||
StoreActor { client } | ||
} | ||
} | ||
|
||
impl Actor for StoreActor { | ||
type Context = Context<Self>; | ||
} | ||
|
||
impl Handler<SetScoreboardCommand> for StoreActor { | ||
type Result = Result<(), redis::RedisError>; | ||
|
||
fn handle(&mut self, msg: SetScoreboardCommand, _: &mut Self::Context) -> Self::Result { | ||
let mut con: Connection = self.client.get_connection()?; | ||
let query_key = format!("room:{}:scoreboard", msg.room_token); | ||
for (player_id, points) in &msg.scoreboard { | ||
con.zadd(query_key.clone(), *points as f64, *player_id)?; | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl Handler<GetScoreboardCommand> for StoreActor { | ||
type Result = Result<HashMap<u32, u32>, redis::RedisError>; | ||
|
||
fn handle(&mut self, msg: GetScoreboardCommand, _: &mut Self::Context) -> Self::Result { | ||
let mut con: Connection = self.client.get_connection()?; | ||
let query_key = format!("room:{}:scoreboard", msg.0); | ||
let scoreboard: Vec<(String, String)> = con.zrevrange_withscores(query_key, 0, -1)?; | ||
let mut result = HashMap::new(); | ||
for (total_points_str, player_id_str) in scoreboard { | ||
let player_id = player_id_str.parse::<u32>().unwrap_or_default(); | ||
let total_points = total_points_str.parse::<f64>().unwrap_or_default() as u32; | ||
result.insert(player_id, total_points); | ||
} | ||
|
||
Ok(result) | ||
} | ||
} | ||
|
||
impl Handler<SetPlayerInfoCommand> for StoreActor { | ||
type Result = Result<String, redis::RedisError>; | ||
|
||
fn handle(&mut self, msg: SetPlayerInfoCommand, _: &mut Self::Context) -> Self::Result { | ||
let mut con: Connection = self.client.get_connection()?; | ||
|
||
// Use hset_multiple to set multiple fields at the same time | ||
let query_key = format!("player:{}:info", msg.player_id); | ||
let fields: Vec<(&str, &str)> = | ||
msg.fields.iter().map(|(k, v)| (k.as_str(), v.as_str())).collect(); | ||
let result: redis::RedisResult<()> = con.hset_multiple(query_key, &fields); | ||
|
||
result | ||
.map_err(|e| e.into()) | ||
.map(|_| format!("Fields are set for player_id {}", msg.player_id)) | ||
} | ||
} | ||
|
||
impl Handler<GetMultiplePlayerInfo> for StoreActor { | ||
type Result = Result<HashMap<u32, String>, redis::RedisError>; | ||
|
||
fn handle(&mut self, msg: GetMultiplePlayerInfo, _: &mut Self::Context) -> Self::Result { | ||
let mut con: Connection = self.client.get_connection()?; | ||
let mut results = HashMap::new(); | ||
for key in msg.player_ids { | ||
let hash_key: String = format!("player:{}:info", key); | ||
let player_info: HashMap<String, String> = con.hgetall(&hash_key)?; | ||
results.insert(key.clone(), serde_json::to_string(&player_info).unwrap()); | ||
} | ||
|
||
Ok(results) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we expose more data? how about name, sth like that. We can't show the data just the id
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I just pushed my refactor to include more info (
api_key
,team_name
) in scoreboard response.