chore (backend-rs): use Arc for antenna cache
This commit is contained in:
parent
4a1ca48538
commit
9f16c00f0c
3 changed files with 19 additions and 23 deletions
|
@ -2,24 +2,23 @@
|
||||||
|
|
||||||
use crate::{database::db_conn, model::entity::antenna};
|
use crate::{database::db_conn, model::entity::antenna};
|
||||||
use sea_orm::prelude::*;
|
use sea_orm::prelude::*;
|
||||||
use std::sync::Mutex;
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
static CACHE: Mutex<Option<Vec<antenna::Model>>> = Mutex::new(None);
|
static CACHE: Mutex<Option<Arc<[antenna::Model]>>> = Mutex::new(None);
|
||||||
|
|
||||||
fn set(antennas: &[antenna::Model]) {
|
fn set(antennas: Arc<[antenna::Model]>) {
|
||||||
let _ = CACHE
|
let _ = CACHE.lock().map(|mut cache| *cache = Some(antennas));
|
||||||
.lock()
|
|
||||||
.map(|mut cache| *cache = Some(antennas.to_owned()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) async fn update() -> Result<Vec<antenna::Model>, DbErr> {
|
pub(super) async fn update() -> Result<Arc<[antenna::Model]>, DbErr> {
|
||||||
tracing::debug!("updating cache");
|
tracing::debug!("updating cache");
|
||||||
let antennas = antenna::Entity::find().all(db_conn().await?).await?;
|
let antennas: Arc<[antenna::Model]> =
|
||||||
set(&antennas);
|
antenna::Entity::find().all(db_conn().await?).await?.into();
|
||||||
|
set(antennas.clone());
|
||||||
Ok(antennas)
|
Ok(antennas)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) async fn get() -> Result<Vec<antenna::Model>, DbErr> {
|
pub(super) async fn get() -> Result<Arc<[antenna::Model]>, DbErr> {
|
||||||
if let Some(cache) = CACHE.lock().ok().and_then(|cache| cache.clone()) {
|
if let Some(cache) = CACHE.lock().ok().and_then(|cache| cache.clone()) {
|
||||||
return Ok(cache);
|
return Ok(cache);
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,8 +12,6 @@ pub enum AntennaCheckError {
|
||||||
Db(#[from] DbErr),
|
Db(#[from] DbErr),
|
||||||
#[error("Cache error: {0}")]
|
#[error("Cache error: {0}")]
|
||||||
Cache(#[from] cache::Error),
|
Cache(#[from] cache::Error),
|
||||||
#[error("User profile not found: {0}")]
|
|
||||||
UserProfileNotFound(String),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn match_all(space_separated_words: &str, text: &str, case_sensitive: bool) -> bool {
|
fn match_all(space_separated_words: &str, text: &str, case_sensitive: bool) -> bool {
|
||||||
|
@ -29,7 +27,7 @@ fn match_all(space_separated_words: &str, text: &str, case_sensitive: bool) -> b
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn check_hit_antenna(
|
pub(super) async fn check_hit_antenna(
|
||||||
antenna: &antenna::Model,
|
antenna: &antenna::Model,
|
||||||
note: ¬e::Model,
|
note: ¬e::Model,
|
||||||
note_all_texts: &[String],
|
note_all_texts: &[String],
|
||||||
|
|
|
@ -37,19 +37,18 @@ type Note = note::Model;
|
||||||
|
|
||||||
#[crate::export]
|
#[crate::export]
|
||||||
pub async fn update_antennas_on_new_note(
|
pub async fn update_antennas_on_new_note(
|
||||||
note: Note,
|
note: &Note,
|
||||||
note_author: &Acct,
|
note_author: &Acct,
|
||||||
note_muted_users: &[String],
|
note_muted_users: &[String],
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
let note_cloned = note.clone();
|
|
||||||
let note_all_texts = all_texts(
|
let note_all_texts = all_texts(
|
||||||
PartialNoteToElaborate {
|
PartialNoteToElaborate {
|
||||||
file_ids: note.file_ids,
|
file_ids: note.file_ids.to_owned(),
|
||||||
user_id: note.user_id,
|
user_id: note.user_id.to_owned(),
|
||||||
text: note.text,
|
text: note.text.to_owned(),
|
||||||
cw: note.cw,
|
cw: note.cw.to_owned(),
|
||||||
renote_id: note.renote_id,
|
renote_id: note.renote_id.to_owned(),
|
||||||
reply_id: note.reply_id,
|
reply_id: note.reply_id.to_owned(),
|
||||||
},
|
},
|
||||||
false,
|
false,
|
||||||
)
|
)
|
||||||
|
@ -60,8 +59,8 @@ pub async fn update_antennas_on_new_note(
|
||||||
if note_muted_users.contains(&antenna.user_id) {
|
if note_muted_users.contains(&antenna.user_id) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if check_hit_antenna(antenna, ¬e_cloned, ¬e_all_texts, note_author).await? {
|
if check_hit_antenna(antenna, note, ¬e_all_texts, note_author).await? {
|
||||||
add_note_to_antenna(&antenna.id, ¬e_cloned).await?;
|
add_note_to_antenna(&antenna.id, note).await?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue