Cache LOCAL_INSTANCE_USER once

This commit is contained in:
Kitaiti Makoto 2021-12-05 20:57:10 +09:00
parent e01539ef16
commit c67b702425

View File

@ -11,7 +11,6 @@ use diesel::{self, result::Error::NotFound, ExpressionMethods, QueryDsl, RunQuer
use once_cell::sync::OnceCell; use once_cell::sync::OnceCell;
use plume_common::utils::md_to_html; use plume_common::utils::md_to_html;
use std::sync::RwLock; use std::sync::RwLock;
use tracing::error;
#[derive(Clone, Identifiable, Queryable)] #[derive(Clone, Identifiable, Queryable)]
pub struct Instance { pub struct Instance {
@ -110,16 +109,11 @@ impl Instance {
} }
pub fn cache_local_instance_user(conn: &Connection) { pub fn cache_local_instance_user(conn: &Connection) {
let res = Self::get_local_instance_user_uncached(conn) let _ = LOCAL_INSTANCE_USER.get_or_init(|| {
.or_else(|_| Self::create_local_instance_user(conn)); Self::get_local_instance_user_uncached(conn)
if res.is_err() { .or_else(|_| Self::create_local_instance_user(conn))
error!("Failed to cache local instance user: {:?}", res); .expect("Failed to cache local instance user")
return; });
}
let user = res.expect("Unreachable");
LOCAL_INSTANCE_USER
.set(user)
.expect("Failed to set local instance user");
} }
pub fn page(conn: &Connection, (min, max): (i32, i32)) -> Result<Vec<Instance>> { pub fn page(conn: &Connection, (min, max): (i32, i32)) -> Result<Vec<Instance>> {