-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: sucessfully set and retrieve basic data from redis
- Loading branch information
1 parent
e9a4f5b
commit ad1cc85
Showing
4 changed files
with
95 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,6 +1,7 @@ | ||
use redis_macros::{FromRedisValue, ToRedisArgs}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
#[derive(Serialize, Deserialize,FromRedisValue,ToRedisArgs,PartialEq,Eq,Debug)] | ||
pub struct RateLimitInfo { | ||
pub limit: i32, | ||
} |
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,67 @@ | ||
use std::net::SocketAddr; | ||
|
||
use redis::{aio::MultiplexedConnection, AsyncCommands, Client}; | ||
|
||
use super::{RateLimitInfo, Result}; | ||
|
||
pub trait RedisInteractor{ | ||
async fn new(redis_url:String)->Result<Self> where Self:Sized; | ||
async fn get_data(&mut self,ip_addr:SocketAddr)-> Option<RateLimitInfo> ; | ||
async fn set_data(&mut self,ip_addr:SocketAddr,rate_limit_info:&RateLimitInfo); | ||
} | ||
|
||
pub struct RedisRateLimiterDb{ | ||
pub client:Client, | ||
pub connection:MultiplexedConnection | ||
|
||
} | ||
|
||
|
||
impl RedisInteractor for RedisRateLimiterDb{ | ||
async fn new(redis_url:String)->Result<Self> { | ||
let client = Client::open(redis_url)?; | ||
let connection = client.get_multiplexed_async_connection().await?; | ||
Ok(Self { client, connection }) | ||
} | ||
|
||
async fn get_data(&mut self, ip_addr: SocketAddr) -> Option<RateLimitInfo> { | ||
let key = ip_addr.to_string(); | ||
self.connection.get::<String,Option<RateLimitInfo>>(key).await.unwrap() | ||
} | ||
|
||
async fn set_data(&mut self, ip_addr: SocketAddr,rate_limit_info:&RateLimitInfo) { | ||
let key = ip_addr.to_string(); | ||
self.connection.set::<String, &RateLimitInfo, ()>(key, rate_limit_info).await.unwrap(); // TODO, why ,,() ? | ||
|
||
} | ||
} | ||
|
||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use std::str::FromStr; | ||
|
||
async fn setup_test_db() -> RedisRateLimiterDb { | ||
let redis_url = "redis://localhost:6379/15"; // using database 15 for testing | ||
RedisRateLimiterDb::new(redis_url.to_string()).await.expect("Failed to create test Redis client") | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_new() { | ||
let _db = setup_test_db().await; | ||
// If no panic and no error, assume successful connection and client creation | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_set_and_get_data() { | ||
let mut db = setup_test_db().await; | ||
let test_ip = SocketAddr::from_str("127.0.0.1:8080").unwrap(); | ||
let rate_limit_info = RateLimitInfo { limit: 10 }; | ||
|
||
db.set_data(test_ip, &rate_limit_info).await; | ||
let retrieved_data = db.get_data(test_ip).await; | ||
|
||
assert_eq!(Some(rate_limit_info), retrieved_data, "Retrieved data does not match the set data."); | ||
} | ||
} |