diff --git a/src/database/models/organization_item.rs b/src/database/models/organization_item.rs index 5a52558a..64d0d2ba 100644 --- a/src/database/models/organization_item.rs +++ b/src/database/models/organization_item.rs @@ -1,14 +1,11 @@ -use crate::models::ids::base62_impl::{parse_base62, to_base62}; +use crate::{models::ids::base62_impl::{parse_base62, to_base62}, database::redis::RedisPool}; use super::{ids::*, TeamMember}; -use redis::cmd; use serde::{Deserialize, Serialize}; const ORGANIZATIONS_NAMESPACE: &str = "organizations"; const ORGANIZATIONS_TITLES_NAMESPACE: &str = "organizations_titles"; -const DEFAULT_EXPIRY: i64 = 1800; - #[derive(Deserialize, Serialize, Clone, Debug)] /// An organization of users who together control one or more projects and organizations. pub struct Organization { @@ -55,7 +52,7 @@ impl Organization { pub async fn get<'a, E>( string: &str, exec: E, - redis: &deadpool_redis::Pool, + redis: &RedisPool, ) -> Result, super::DatabaseError> where E: sqlx::Executor<'a, Database = sqlx::Postgres>, @@ -68,7 +65,7 @@ impl Organization { pub async fn get_id<'a, 'b, E>( id: OrganizationId, exec: E, - redis: &deadpool_redis::Pool, + redis: &RedisPool, ) -> Result, super::DatabaseError> where E: sqlx::Executor<'a, Database = sqlx::Postgres>, @@ -81,7 +78,7 @@ impl Organization { pub async fn get_many_ids<'a, 'b, E>( organization_ids: &[OrganizationId], exec: E, - redis: &deadpool_redis::Pool, + redis: &RedisPool, ) -> Result, super::DatabaseError> where E: sqlx::Executor<'a, Database = sqlx::Postgres>, @@ -96,7 +93,7 @@ impl Organization { pub async fn get_many<'a, E, T: ToString>( organization_strings: &[T], exec: E, - redis: &deadpool_redis::Pool, + redis: &RedisPool, ) -> Result, super::DatabaseError> where E: sqlx::Executor<'a, Database = sqlx::Postgres>, @@ -107,8 +104,6 @@ impl Organization { return Ok(Vec::new()); } - let mut redis = redis.get().await?; - let mut found_organizations = Vec::new(); let mut remaining_strings = organization_strings .iter() @@ -121,36 +116,24 @@ impl Organization { .collect::>(); organization_ids.append( - &mut cmd("MGET") - .arg( - organization_strings - .iter() - .map(|x| { - format!( - "{}:{}", - ORGANIZATIONS_TITLES_NAMESPACE, - x.to_string().to_lowercase() - ) - }) - .collect::>(), - ) - .query_async::<_, Vec>>(&mut redis) - .await? - .into_iter() - .flatten() - .collect(), + &mut redis + .multi_get::( + ORGANIZATIONS_TITLES_NAMESPACE, + organization_strings + .iter() + .map(|x| x.to_string().to_lowercase()) + .collect(), + ) + .await? + .into_iter() + .flatten() + .collect() ); if !organization_ids.is_empty() { - let organizations = cmd("MGET") - .arg( - organization_ids - .iter() - .map(|x| format!("{}:{}", ORGANIZATIONS_NAMESPACE, x)) - .collect::>(), - ) - .query_async::<_, Vec>>(&mut redis) - .await?; + let organizations = redis + .multi_get::(ORGANIZATIONS_NAMESPACE, organization_ids) + .await?; for organization in organizations { if let Some(organization) = @@ -201,25 +184,23 @@ impl Organization { .await?; for organization in organizations { - cmd("SET") - .arg(format!("{}:{}", ORGANIZATIONS_NAMESPACE, organization.id.0)) - .arg(serde_json::to_string(&organization)?) - .arg("EX") - .arg(DEFAULT_EXPIRY) - .query_async::<_, ()>(&mut redis) - .await?; - - cmd("SET") - .arg(format!( - "{}:{}", + redis + .set( + ORGANIZATIONS_NAMESPACE, + organization.id.0, + serde_json::to_string(&organization)?, + None, + ) + .await?; + redis + .set( ORGANIZATIONS_TITLES_NAMESPACE, - organization.title.to_lowercase() - )) - .arg(organization.id.0) - .arg("EX") - .arg(DEFAULT_EXPIRY) - .query_async::<_, ()>(&mut redis) + organization.title.to_lowercase(), + organization.id.0, + None, + ) .await?; + found_organizations.push(organization); } } @@ -265,7 +246,7 @@ impl Organization { pub async fn remove( id: OrganizationId, transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>, - redis: &deadpool_redis::Pool, + redis: &RedisPool, ) -> Result, super::DatabaseError> { use futures::TryStreamExt; @@ -333,19 +314,18 @@ impl Organization { pub async fn clear_cache( id: OrganizationId, title: Option, - redis: &deadpool_redis::Pool, + redis: &RedisPool, ) -> Result<(), super::DatabaseError> { - let mut redis = redis.get().await?; - let mut cmd = cmd("DEL"); - cmd.arg(format!("{}:{}", ORGANIZATIONS_NAMESPACE, id.0)); + + redis.delete(ORGANIZATIONS_NAMESPACE, id.0).await?; if let Some(title) = title { - cmd.arg(format!( - "{}:{}", - ORGANIZATIONS_TITLES_NAMESPACE, - title.to_lowercase() - )); + redis + .delete( + ORGANIZATIONS_TITLES_NAMESPACE, + title.to_lowercase(), + ) + .await?; } - cmd.query_async::<_, ()>(&mut redis).await?; Ok(()) } diff --git a/src/routes/v2/organizations.rs b/src/routes/v2/organizations.rs index 427fe5ee..fe0235d3 100644 --- a/src/routes/v2/organizations.rs +++ b/src/routes/v2/organizations.rs @@ -4,6 +4,7 @@ use std::sync::Arc; use crate::auth::{filter_authorized_projects, get_user_from_headers}; use crate::database::models::team_item::TeamMember; use crate::database::models::{generate_organization_id, team_item, Organization}; +use crate::database::redis::RedisPool; use crate::file_hosting::FileHost; use crate::models::ids::base62_impl::parse_base62; use crate::models::organizations::OrganizationId; @@ -56,7 +57,7 @@ pub async fn organization_create( req: HttpRequest, new_organization: web::Json, pool: web::Data, - redis: web::Data, + redis: web::Data, session_queue: web::Data, ) -> Result { let current_user = get_user_from_headers( @@ -143,7 +144,7 @@ pub async fn organization_get( req: HttpRequest, info: web::Path<(String,)>, pool: web::Data, - redis: web::Data, + redis: web::Data, session_queue: web::Data, ) -> Result { let id = info.into_inner().0; @@ -208,7 +209,7 @@ pub async fn organizations_get( req: HttpRequest, web::Query(ids): web::Query, pool: web::Data, - redis: web::Data, + redis: web::Data, session_queue: web::Data, ) -> Result { let ids = serde_json::from_str::>(&ids.ids)?; @@ -298,7 +299,7 @@ pub async fn organizations_edit( info: web::Path<(String,)>, new_organization: web::Json, pool: web::Data, - redis: web::Data, + redis: web::Data, session_queue: web::Data, ) -> Result { let user = get_user_from_headers( @@ -434,7 +435,7 @@ pub async fn organization_delete( req: HttpRequest, info: web::Path<(String,)>, pool: web::Data, - redis: web::Data, + redis: web::Data, session_queue: web::Data, ) -> Result { let user = get_user_from_headers( @@ -498,7 +499,7 @@ pub async fn organization_projects_get( req: HttpRequest, info: web::Path<(String,)>, pool: web::Data, - redis: web::Data, + redis: web::Data, session_queue: web::Data, ) -> Result { let info = info.into_inner().0; @@ -547,7 +548,7 @@ pub async fn organization_projects_add( info: web::Path<(String,)>, project_info: web::Json, pool: web::Data, - redis: web::Data, + redis: web::Data, session_queue: web::Data, ) -> Result { let info = info.into_inner().0; @@ -649,7 +650,7 @@ pub async fn organization_projects_remove( req: HttpRequest, info: web::Path<(String, String)>, pool: web::Data, - redis: web::Data, + redis: web::Data, session_queue: web::Data, ) -> Result { let (organization_id, project_id) = info.into_inner(); @@ -743,7 +744,7 @@ pub async fn organization_icon_edit( req: HttpRequest, info: web::Path<(String,)>, pool: web::Data, - redis: web::Data, + redis: web::Data, file_host: web::Data>, mut payload: web::Payload, session_queue: web::Data, @@ -848,7 +849,7 @@ pub async fn delete_organization_icon( req: HttpRequest, info: web::Path<(String,)>, pool: web::Data, - redis: web::Data, + redis: web::Data, file_host: web::Data>, session_queue: web::Data, ) -> Result { diff --git a/src/routes/v2/teams.rs b/src/routes/v2/teams.rs index 0240fe44..d16cc314 100644 --- a/src/routes/v2/teams.rs +++ b/src/routes/v2/teams.rs @@ -117,7 +117,7 @@ pub async fn team_members_get_organization( req: HttpRequest, info: web::Path<(String,)>, pool: web::Data, - redis: web::Data, + redis: web::Data, session_queue: web::Data, ) -> Result { let string = info.into_inner().0;