add user_count in systemlog

This commit is contained in:
2023-05-04 03:58:13 +08:00
parent 959e6caa1d
commit bbed041253
4 changed files with 33 additions and 3 deletions

View File

@@ -1,4 +1,5 @@
use crate::api::CurrentUser;
use crate::api::{Api, CurrentUser};
use crate::db_conn::Db;
use crate::models::{Comment, Post, User};
use crate::rds_conn::RdsConn;
use crate::rds_models::{clear_all, init, BlockedUsers};
@@ -10,6 +11,9 @@ use futures_util::stream::StreamExt;
use rocket::futures::future;
use std::collections::HashMap;
const KEY_USER_COUNT: &str = "hole_v2:cache:user_count";
const USER_COUNT_EXPIRE_TIME: usize = 5 * 60;
const INSTANCE_EXPIRE_TIME: usize = 60 * 60;
const MIN_LENGTH: isize = 200;
@@ -363,3 +367,16 @@ impl BlockDictCache {
self.rconn.del(&self.key).await
}
}
pub async fn cached_user_count(db: &Db, rconn: &mut RdsConn) -> Api<i64> {
let cnt: Option<i64> = rconn.get(KEY_USER_COUNT).await?;
if let Some(x) = cnt {
Ok(x)
} else {
let x = User::get_count(db).await?;
rconn
.set_ex(KEY_USER_COUNT, x, USER_COUNT_EXPIRE_TIME)
.await?;
Ok(x)
}
}