-
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.
- Loading branch information
Showing
13 changed files
with
280 additions
and
57 deletions.
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
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
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
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,66 @@ | ||
use crate::prelude::*; | ||
|
||
pub struct Expire; | ||
|
||
#[async_trait] | ||
impl CommandTrait for Expire { | ||
fn name(&self) -> &str { | ||
"EXPIRE" | ||
} | ||
|
||
async fn handle_command( | ||
&self, | ||
writer: &mut WriteHalf, | ||
args: &mut VecDeque<Value>, | ||
context: ContextRef, | ||
) -> Result<()> { | ||
if args.len() != 2 { | ||
return value_error!("Invalid number of arguments") | ||
.to_resp(writer) | ||
.await; | ||
} | ||
|
||
let key = match args.pop_front() { | ||
Some(Value::String(key)) => key, | ||
Some(_) => { | ||
return value_error!("Invalid key").to_resp(writer).await; | ||
} | ||
None => { | ||
return value_error!("Missing key").to_resp(writer).await; | ||
} | ||
}; | ||
|
||
let seconds = match args.pop_front() { | ||
Some(Value::Integer(seconds)) => seconds, | ||
Some(Value::String(seconds)) => seconds.parse::<i64>().unwrap_or(-1), | ||
Some(_) => { | ||
return value_error!("Invalid seconds").to_resp(writer).await; | ||
} | ||
None => { | ||
return value_error!("Missing seconds").to_resp(writer).await; | ||
} | ||
}; | ||
|
||
if seconds < 0 { | ||
return value_error!("Invalid seconds").to_resp(writer).await; | ||
} | ||
|
||
let mut store = context.store.write().await; | ||
|
||
let value = match store.get_mut(&key) { | ||
Some(Value::Expire((inner, _))) => inner, | ||
Some(inner) => inner, | ||
_ => { | ||
return value_error!("Key not found").to_resp(writer).await; | ||
} | ||
}; | ||
|
||
*value = Value::Expire(( | ||
Box::new(value.to_owned()), | ||
tokio::time::Instant::now() + tokio::time::Duration::from_secs(seconds as u64), | ||
)); | ||
context.expire_keys.write().await.insert(key); | ||
|
||
Value::Ok.to_resp(writer).await | ||
} | ||
} |
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,7 @@ | ||
mod expire; | ||
mod setex; | ||
mod ttl; | ||
|
||
pub use expire::Expire; | ||
pub use setex::SetEx; | ||
pub use ttl::Ttl; |
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,66 @@ | ||
use crate::prelude::*; | ||
|
||
pub struct SetEx; | ||
|
||
#[async_trait] | ||
impl CommandTrait for SetEx { | ||
fn name(&self) -> &str { | ||
"SETEX" | ||
} | ||
|
||
async fn handle_command( | ||
&self, | ||
writer: &mut WriteHalf, | ||
args: &mut VecDeque<Value>, | ||
context: ContextRef, | ||
) -> Result<()> { | ||
if args.len() != 3 { | ||
return value_error!("Invalid number of arguments") | ||
.to_resp(writer) | ||
.await; | ||
} | ||
|
||
let key = match args.pop_front() { | ||
Some(Value::String(key)) => key, | ||
Some(_) => { | ||
return value_error!("Invalid key").to_resp(writer).await; | ||
} | ||
None => { | ||
return value_error!("Missing key").to_resp(writer).await; | ||
} | ||
}; | ||
|
||
let seconds = match args.pop_front() { | ||
Some(Value::Integer(seconds)) => seconds, | ||
Some(Value::String(seconds)) => seconds.parse::<i64>().unwrap_or(-1), | ||
Some(_) => { | ||
return value_error!("Invalid seconds").to_resp(writer).await; | ||
} | ||
None => { | ||
return value_error!("Missing seconds").to_resp(writer).await; | ||
} | ||
}; | ||
|
||
if seconds < 0 { | ||
return value_error!("Invalid seconds").to_resp(writer).await; | ||
} | ||
|
||
let value = match args.pop_front() { | ||
Some(value) => value, | ||
_ => { | ||
return value_error!("Missing value").to_resp(writer).await; | ||
} | ||
}; | ||
|
||
context.store.write().await.insert( | ||
key.clone(), | ||
Value::Expire(( | ||
Box::new(value), | ||
tokio::time::Instant::now() + tokio::time::Duration::from_secs(seconds as u64), | ||
)), | ||
); | ||
context.expire_keys.write().await.insert(key); | ||
|
||
Value::Ok.to_resp(writer).await | ||
} | ||
} |
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,55 @@ | ||
use crate::prelude::*; | ||
|
||
pub struct Ttl; | ||
|
||
#[async_trait] | ||
impl CommandTrait for Ttl { | ||
fn name(&self) -> &str { | ||
"TTL" | ||
} | ||
|
||
async fn handle_command( | ||
&self, | ||
writer: &mut WriteHalf, | ||
args: &mut VecDeque<Value>, | ||
context: ContextRef, | ||
) -> Result<()> { | ||
if args.len() != 1 { | ||
return value_error!("Invalid number of arguments") | ||
.to_resp(writer) | ||
.await; | ||
} | ||
|
||
let key = match args.pop_front() { | ||
Some(Value::String(key)) => key, | ||
Some(_) => { | ||
return value_error!("Invalid key").to_resp(writer).await; | ||
} | ||
None => { | ||
return value_error!("Missing key").to_resp(writer).await; | ||
} | ||
}; | ||
|
||
let store = context.store.read().await; | ||
|
||
let duration = match store.get(&key) { | ||
Some(Value::Expire((_, ttl))) => { | ||
let duration = ttl.duration_since(tokio::time::Instant::now()).as_secs() as i64; | ||
|
||
if duration != 0 { | ||
duration | ||
} else { | ||
-2 | ||
} | ||
} | ||
|
||
// non-expire keys should return -1 | ||
Some(_) => -1, | ||
|
||
// not found keys should return -2 | ||
None => -2, | ||
}; | ||
|
||
Value::Integer(duration).to_resp(writer).await | ||
} | ||
} |
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.