feat: make database access more uniform and stop locking as many pool connections
All checks were successful
ci/woodpecker/push/build-and-publish Pipeline was successful
All checks were successful
ci/woodpecker/push/build-and-publish Pipeline was successful
This commit is contained in:
parent
f5d4211fad
commit
fa52412b43
30 changed files with 516 additions and 373 deletions
|
@ -4,7 +4,7 @@ use diesel_async::RunQueryDsl;
|
|||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{AppState, Conn, error::Error, objects::Me, schema::users};
|
||||
use crate::{Conn, error::Error, objects::Me, schema::users, utils::CacheFns};
|
||||
|
||||
use super::load_or_empty;
|
||||
|
||||
|
@ -46,23 +46,25 @@ pub struct User {
|
|||
}
|
||||
|
||||
impl User {
|
||||
pub async fn fetch_one(app_state: &AppState, user_uuid: Uuid) -> Result<Self, Error> {
|
||||
let mut conn = app_state.pool.get().await?;
|
||||
|
||||
if let Ok(cache_hit) = app_state.get_cache_key(user_uuid.to_string()).await {
|
||||
return Ok(serde_json::from_str(&cache_hit)?);
|
||||
pub async fn fetch_one(
|
||||
conn: &mut Conn,
|
||||
cache_pool: &redis::Client,
|
||||
user_uuid: Uuid,
|
||||
) -> Result<Self, Error> {
|
||||
if let Ok(cache_hit) = cache_pool.get_cache_key(user_uuid.to_string()).await {
|
||||
return Ok(cache_hit);
|
||||
}
|
||||
|
||||
use users::dsl;
|
||||
let user_builder: UserBuilder = dsl::users
|
||||
.filter(dsl::uuid.eq(user_uuid))
|
||||
.select(UserBuilder::as_select())
|
||||
.get_result(&mut conn)
|
||||
.get_result(conn)
|
||||
.await?;
|
||||
|
||||
let user = user_builder.build();
|
||||
|
||||
app_state
|
||||
cache_pool
|
||||
.set_cache_key(user_uuid.to_string(), user.clone(), 1800)
|
||||
.await?;
|
||||
|
||||
|
@ -70,15 +72,14 @@ impl User {
|
|||
}
|
||||
|
||||
pub async fn fetch_one_with_friendship(
|
||||
app_state: &AppState,
|
||||
conn: &mut Conn,
|
||||
cache_pool: &redis::Client,
|
||||
me: &Me,
|
||||
user_uuid: Uuid,
|
||||
) -> Result<Self, Error> {
|
||||
let mut conn = app_state.pool.get().await?;
|
||||
let mut user = Self::fetch_one(conn, cache_pool, user_uuid).await?;
|
||||
|
||||
let mut user = Self::fetch_one(app_state, user_uuid).await?;
|
||||
|
||||
if let Some(friend) = me.friends_with(&mut conn, user_uuid).await? {
|
||||
if let Some(friend) = me.friends_with(conn, user_uuid).await? {
|
||||
user.friends_since = Some(friend.accepted_at);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue