You've already forked AstralRinth
forked from didirus/AstralRinth
Tests (#719)
* computer switch * some fixes; github action * added pr to master * sqlx database setup * switched intial GHA test db * removed sqlx database setup * unfinished patch route * bug fixes + tests * more tests, more fixes, cargo fmt * merge fixes * more tests, full reorganization * fmt, clippy * sqlx-data * revs * removed comments * delete revs
This commit is contained in:
@@ -1,13 +1,12 @@
|
||||
use super::ids::*;
|
||||
use crate::database::models;
|
||||
use crate::database::models::DatabaseError;
|
||||
use crate::database::redis::RedisPool;
|
||||
use crate::models::collections::CollectionStatus;
|
||||
use chrono::{DateTime, Utc};
|
||||
use redis::cmd;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
const COLLECTIONS_NAMESPACE: &str = "collections";
|
||||
const DEFAULT_EXPIRY: i64 = 1800; // 30 minutes
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct CollectionBuilder {
|
||||
@@ -102,7 +101,7 @@ impl Collection {
|
||||
pub async fn remove(
|
||||
id: CollectionId,
|
||||
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
|
||||
redis: &deadpool_redis::Pool,
|
||||
redis: &RedisPool,
|
||||
) -> Result<Option<()>, DatabaseError> {
|
||||
let collection = Self::get(id, &mut *transaction, redis).await?;
|
||||
|
||||
@@ -138,7 +137,7 @@ impl Collection {
|
||||
pub async fn get<'a, 'b, E>(
|
||||
id: CollectionId,
|
||||
executor: E,
|
||||
redis: &deadpool_redis::Pool,
|
||||
redis: &RedisPool,
|
||||
) -> Result<Option<Collection>, DatabaseError>
|
||||
where
|
||||
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
|
||||
@@ -151,7 +150,7 @@ impl Collection {
|
||||
pub async fn get_many<'a, E>(
|
||||
collection_ids: &[CollectionId],
|
||||
exec: E,
|
||||
redis: &deadpool_redis::Pool,
|
||||
redis: &RedisPool,
|
||||
) -> Result<Vec<Collection>, DatabaseError>
|
||||
where
|
||||
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
|
||||
@@ -162,20 +161,12 @@ impl Collection {
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
|
||||
let mut redis = redis.get().await?;
|
||||
|
||||
let mut found_collections = Vec::new();
|
||||
let mut remaining_collections: Vec<CollectionId> = collection_ids.to_vec();
|
||||
|
||||
if !collection_ids.is_empty() {
|
||||
let collections = cmd("MGET")
|
||||
.arg(
|
||||
collection_ids
|
||||
.iter()
|
||||
.map(|x| format!("{}:{}", COLLECTIONS_NAMESPACE, x.0))
|
||||
.collect::<Vec<_>>(),
|
||||
)
|
||||
.query_async::<_, Vec<Option<String>>>(&mut redis)
|
||||
let collections = redis
|
||||
.multi_get::<String, _>(COLLECTIONS_NAMESPACE, collection_ids.iter().map(|x| x.0))
|
||||
.await?;
|
||||
|
||||
for collection in collections {
|
||||
@@ -233,14 +224,14 @@ impl Collection {
|
||||
.await?;
|
||||
|
||||
for collection in db_collections {
|
||||
cmd("SET")
|
||||
.arg(format!("{}:{}", COLLECTIONS_NAMESPACE, collection.id.0))
|
||||
.arg(serde_json::to_string(&collection)?)
|
||||
.arg("EX")
|
||||
.arg(DEFAULT_EXPIRY)
|
||||
.query_async::<_, ()>(&mut redis)
|
||||
redis
|
||||
.set(
|
||||
COLLECTIONS_NAMESPACE,
|
||||
collection.id.0,
|
||||
serde_json::to_string(&collection)?,
|
||||
None,
|
||||
)
|
||||
.await?;
|
||||
|
||||
found_collections.push(collection);
|
||||
}
|
||||
}
|
||||
@@ -248,16 +239,8 @@ impl Collection {
|
||||
Ok(found_collections)
|
||||
}
|
||||
|
||||
pub async fn clear_cache(
|
||||
id: CollectionId,
|
||||
redis: &deadpool_redis::Pool,
|
||||
) -> Result<(), DatabaseError> {
|
||||
let mut redis = redis.get().await?;
|
||||
let mut cmd = cmd("DEL");
|
||||
|
||||
cmd.arg(format!("{}:{}", COLLECTIONS_NAMESPACE, id.0));
|
||||
cmd.query_async::<_, ()>(&mut redis).await?;
|
||||
|
||||
pub async fn clear_cache(id: CollectionId, redis: &RedisPool) -> Result<(), DatabaseError> {
|
||||
redis.delete(COLLECTIONS_NAMESPACE, id.0).await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user