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::{ pub async fn add_jwt(p: &Pool, token: &str) -> Result<(), Error> {
Client, AsyncCommands, let conn = p.get_conn().await?;
RedisResult let q = "INSERT INTO jwt (token) VALUES (:tk)";
}; let _ = conn.drop_exec(q, params!{"tk" => token}).await?;
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?;
Ok(()) Ok(())
} }
pub async fn active_jwt(id: UBigInt, token: &str) -> RedisResult<bool> { pub async fn listed_jwt(p: &Pool, id: crate::UBigInt, token_given: &str) -> Result<bool, Error> {
let client = Client::open(REDIS_URL.as_ref())?; // only checks if the given token is listed somewhere in the db
let mut conn = client.get_tokio_connection().await?; let conn = p.get_conn().await?;
let q = "SELECT token FROM jwt WHERE id = :id";
let val: String = conn.get(id).await?; // if id.token == return true
Ok(val == token) 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)
};
} }