Movng back to mysql from redis due to tokio reactor errors

At some point we should try moving to redis for better cache performance but that change is massive and this works at least as a poc
This commit is contained in:
shockrah 2020-12-28 21:53:57 -08:00
parent 55ade005a2
commit 967782be5f

View File

@ -1,28 +1,25 @@
use crate::UBigInt;
use mysql_async::{params, Pool};
use mysql_async::prelude::Queryable;
use mysql_async::error::Error;
use redis::{
Client, AsyncCommands,
RedisResult
};
lazy_static! {
static ref REDIS_URL: String = {
std::env::var("REDIS_URL").unwrap()
};
}
pub async fn add_jwt(id: UBigInt, token: &str) -> RedisResult<()> {
let client = Client::open(REDIS_URL.as_ref())?;
let mut conn = client.get_tokio_connection().await?;
let _ = conn.set(id, token).await?;
pub async fn add_jwt(p: &Pool, token: &str) -> Result<(), Error> {
let conn = p.get_conn().await?;
let q = "INSERT INTO jwt (token) VALUES (:tk)";
let _ = conn.drop_exec(q, params!{"tk" => token}).await?;
Ok(())
}
pub async fn active_jwt(id: UBigInt, token: &str) -> RedisResult<bool> {
let client = Client::open(REDIS_URL.as_ref())?;
let mut conn = client.get_tokio_connection().await?;
let val: String = conn.get(id).await?;
Ok(val == token)
pub async fn listed_jwt(p: &Pool, id: crate::UBigInt, token_given: &str) -> Result<bool, Error> {
// only checks if the given token is listed somewhere in the db
let conn = p.get_conn().await?;
let q = "SELECT token FROM jwt WHERE id = :id";
// if id.token == return true
let (_, db_t): (_, Option<String>) =
conn.first_exec(q, params!{"id" => id}).await?;
return match db_t {
Some(token_db) => Ok(token_db == token_given), // probably pointless check but its not that expensive so its stays as a sanity check
None => Ok(false)
};
}