From a2510dc4af1088776b6f162e91c3a24ffade03b3 Mon Sep 17 00:00:00 2001 From: ":NoBody:" Date: Tue, 31 Mar 2026 21:40:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86SledDB=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=EF=BC=8C=E4=BD=BF=E5=BE=97=E6=89=93=E5=BC=80=E6=9B=B4?= =?UTF-8?q?=E4=BC=98=E9=9B=85=EF=BC=8C=E6=80=A7=E8=83=BD=E6=9B=B4=E5=BF=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 22 +++++++++++++++++++++ Cargo.toml | 2 ++ src/bot/bot_handler.rs | 4 ++++ src/db/mod.rs | 1 + src/db/sled_db.rs | 29 ++++++++++++++++++++++++---- src/db/user_state.rs | 44 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 98 insertions(+), 4 deletions(-) create mode 100644 src/db/user_state.rs diff --git a/Cargo.lock b/Cargo.lock index ad9faf4..47f09be 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -641,6 +641,8 @@ version = "0.1.0" dependencies = [ "anyhow", "async-trait", + "chrono", + "dashmap", "env_logger", "log", "matrix-sdk", @@ -653,6 +655,20 @@ dependencies = [ "toml 1.0.7+spec-1.1.0", ] +[[package]] +name = "dashmap" +version = "6.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5041cc499144891f3790297212f32a74fb938e5136a14943f338ef9e0ae276cf" +dependencies = [ + "cfg-if", + "crossbeam-utils", + "hashbrown 0.14.5", + "lock_api", + "once_cell", + "parking_lot_core 0.9.12", +] + [[package]] name = "date_header" version = "1.0.5" @@ -1226,6 +1242,12 @@ dependencies = [ "tracing", ] +[[package]] +name = "hashbrown" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" + [[package]] name = "hashbrown" version = "0.15.5" diff --git a/Cargo.toml b/Cargo.toml index d93bbb9..b9643d2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,8 @@ edition = "2024" [dependencies] anyhow = "1.0.102" async-trait = "0.1.89" +chrono = "0.4.44" +dashmap = "6.1.0" env_logger = "0.11.9" log = "0.4.29" reqwest = "0.13.2" diff --git a/src/bot/bot_handler.rs b/src/bot/bot_handler.rs index 9e0693d..0780bab 100644 --- a/src/bot/bot_handler.rs +++ b/src/bot/bot_handler.rs @@ -61,6 +61,10 @@ async fn start_bot_command_processor( }; bot.send_raw_message(&outbound.channel_id, &outbound) .await?; + } + "checkin"=>{ + + } _ => { return Err(anyhow!("unknown command: {}", command)); diff --git a/src/db/mod.rs b/src/db/mod.rs index 926da96..33a2e9a 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -1,2 +1,3 @@ pub mod define; pub mod sled_db; +pub mod user_state; diff --git a/src/db/sled_db.rs b/src/db/sled_db.rs index 61257bf..dcf96f4 100644 --- a/src/db/sled_db.rs +++ b/src/db/sled_db.rs @@ -1,9 +1,26 @@ -use std::path::Path; +use std::{ + path::{Path, PathBuf}, + sync::OnceLock, +}; use super::define::StorageEngine; use anyhow::Result; use async_trait::async_trait; - +use dashmap::DashMap; +pub static SLED_DB_POOL: OnceLock> = OnceLock::new(); +pub fn get_sled_db(path: &Path) -> Result { + let pool = SLED_DB_POOL.get_or_init(|| DashMap::new()); + if let Some(db) = pool.get(path) { + Ok(db.clone()) + } else { + let db = sled::open(path)?; + pool.insert(path.to_path_buf(), db.clone()); + Ok(db) + } +} +pub fn get_pool() -> &'static DashMap { + SLED_DB_POOL.get_or_init(|| DashMap::new()) +} pub struct SledDB { pub sled_db: sled::Db, } @@ -11,6 +28,7 @@ pub struct SledDB { impl SledDB { pub fn new(path: &Path) -> Result { let db = sled::open(path)?; + Ok(Self { sled_db: db }) } } @@ -18,7 +36,11 @@ impl SledDB { #[async_trait] impl StorageEngine for SledDB { async fn open(path: &Path) -> Result { - Self::new(path) + let entry = get_pool().entry(path.to_path_buf()); + let sled_db = entry + .or_insert_with(|| sled::open(path).expect("Failed to open sled database")) + .clone(); + Ok(SledDB { sled_db }) } async fn set_raw(&self, key: &[u8], value: &[u8]) -> Result<()> { @@ -39,7 +61,6 @@ impl StorageEngine for SledDB { } } - //INFO: AI写的集成测试 #[cfg(test)] mod tests { diff --git a/src/db/user_state.rs b/src/db/user_state.rs new file mode 100644 index 0000000..48b4a52 --- /dev/null +++ b/src/db/user_state.rs @@ -0,0 +1,44 @@ +use chrono::Datelike; +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Clone, Default, Deserialize, Serialize)] +pub struct UserState { + pub is_checkin_today: bool, + pub checkin_unix_second: u64, + pub checkin_times_in_month: usize, + pub checkin_days: Vec, //每月在哪天签到了 +} + +/* +key:用户id +value:UserState的json字符串 + + +*/ + +impl UserState { + pub fn new() -> Self { + Self { + is_checkin_today: false, + checkin_unix_second: 0, + checkin_times_in_month: 0, + checkin_days: Vec::new(), + } + } + + pub fn checkin(&mut self) { + self.is_checkin_today = true; + let unix_time = tokio::time::Instant::now().elapsed().as_secs(); + + self.checkin_unix_second = unix_time; + self.checkin_times_in_month += 1; + let day = chrono::Utc::now().day(); + if !self.checkin_days.contains(&day) { + self.checkin_days.push(day); + } + } + + pub fn reset_daily(&mut self) { + self.is_checkin_today = false; + } +}