From c67b702425906355b0a40d0a37b445c746e0ad0d Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Sun, 5 Dec 2021 20:57:10 +0900 Subject: [PATCH] Cache LOCAL_INSTANCE_USER once --- plume-models/src/instance.rs | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/plume-models/src/instance.rs b/plume-models/src/instance.rs index caf474b2..86cb3d46 100644 --- a/plume-models/src/instance.rs +++ b/plume-models/src/instance.rs @@ -11,7 +11,6 @@ use diesel::{self, result::Error::NotFound, ExpressionMethods, QueryDsl, RunQuer use once_cell::sync::OnceCell; use plume_common::utils::md_to_html; use std::sync::RwLock; -use tracing::error; #[derive(Clone, Identifiable, Queryable)] pub struct Instance { @@ -110,16 +109,11 @@ impl Instance { } pub fn cache_local_instance_user(conn: &Connection) { - let res = Self::get_local_instance_user_uncached(conn) - .or_else(|_| Self::create_local_instance_user(conn)); - if res.is_err() { - error!("Failed to cache local instance user: {:?}", res); - return; - } - let user = res.expect("Unreachable"); - LOCAL_INSTANCE_USER - .set(user) - .expect("Failed to set local instance user"); + let _ = LOCAL_INSTANCE_USER.get_or_init(|| { + Self::get_local_instance_user_uncached(conn) + .or_else(|_| Self::create_local_instance_user(conn)) + .expect("Failed to cache local instance user") + }); } pub fn page(conn: &Connection, (min, max): (i32, i32)) -> Result> {