ping redis

This commit is contained in:
Aravinth Manivannan 2021-06-12 13:25:18 +05:30
parent 1c6e9dd01d
commit 3db009977a
Signed by untrusted user: realaravinth
GPG Key ID: AD9F0F08E855ED88
2 changed files with 32 additions and 0 deletions

View File

@ -110,6 +110,11 @@ impl MCaptchaRedisConnection {
Ok(())
}
/// Ping redis
pub async fn ping(&self) -> bool {
self.0.ping().await
}
/// Add visitor
pub async fn add_visitor(&self, msg: AddVisitor) -> CaptchaResult<Option<AddVisitorResult>> {
let res: String = self.0.exec(redis::cmd(ADD_VISITOR).arg(&[msg.0])).await?;
@ -328,6 +333,8 @@ pub mod tests {
r.add_token(&add_challenge_msg).await.unwrap();
assert!(r.delete_token(&challenge_msg).await.is_ok());
assert!(r.ping().await);
assert!(r.delete_captcha(CAPTCHA_NAME).await.is_ok());
}
}

View File

@ -76,6 +76,18 @@ impl RedisConnection {
RedisConnection::Cluster(con) => cmd.query(&mut *con.borrow_mut()),
}
}
pub async fn ping(&self) -> bool {
if let Ok(redis::Value::Status(v)) = self.exec(&mut redis::cmd("PING")).await {
if v == "PONG" {
true
} else {
false
}
} else {
false
}
}
}
#[derive(Clone)]
@ -126,3 +138,16 @@ impl Redis {
(redis, client)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[actix_rt::test]
async fn ping_works() {
let r = Redis::new(RedisConfig::Single("redis://127.0.0.1".into()))
.await
.unwrap();
assert!(r.get_client().ping().await);
}
}