From 776d81cda288aeef2d9f669d01cc47eb9e29b954 Mon Sep 17 00:00:00 2001 From: liyunjia Date: Tue, 16 Apr 2024 21:56:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=9D=E8=AF=95=E5=BC=80=E5=A7=8B=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E6=9C=AC=E5=9C=B0=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 170 +++++++++++++++++++++++++++++++- library/Cargo.toml | 5 +- library/src/core/local_cache.rs | 39 ++++++++ library/src/core/mod.rs | 1 + 4 files changed, 213 insertions(+), 2 deletions(-) create mode 100644 library/src/core/local_cache.rs diff --git a/Cargo.lock b/Cargo.lock index 4b59f04..967f3f5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -98,6 +98,17 @@ version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711" +[[package]] +name = "async-lock" +version = "3.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d034b430882f8381900d3fe6f0aaa3ad94f2cb4ac519b429692a1bc2dda4ae7b" +dependencies = [ + "event-listener 4.0.3", + "event-listener-strategy", + "pin-project-lite", +] + [[package]] name = "async-stream" version = "0.3.5" @@ -413,6 +424,15 @@ dependencies = [ "tokio", ] +[[package]] +name = "concurrent-queue" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d16048cd947b08fa32c24458a22f5dc5e835264f689f4f5653210c69fd107363" +dependencies = [ + "crossbeam-utils", +] + [[package]] name = "const-oid" version = "0.9.6" @@ -458,6 +478,15 @@ dependencies = [ "crossbeam-utils", ] +[[package]] +name = "crossbeam-epoch" +version = "0.9.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" +dependencies = [ + "crossbeam-utils", +] + [[package]] name = "crossbeam-queue" version = "0.3.11" @@ -619,6 +648,38 @@ version = "2.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0" +[[package]] +name = "event-listener" +version = "4.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67b215c49b2b248c855fb73579eb1f4f26c38ffdc12973e20e07b91d78d5646e" +dependencies = [ + "concurrent-queue", + "parking", + "pin-project-lite", +] + +[[package]] +name = "event-listener" +version = "5.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d9944b8ca13534cdfb2800775f8dd4902ff3fc75a50101466decadfdf322a24" +dependencies = [ + "concurrent-queue", + "parking", + "pin-project-lite", +] + +[[package]] +name = "event-listener-strategy" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "958e4d70b6d5e81971bebec42271ec641e7ff4e170a6fa605f2b8a8b65cb97d3" +dependencies = [ + "event-listener 4.0.3", + "pin-project-lite", +] + [[package]] name = "fastrand" version = "2.0.1" @@ -721,6 +782,17 @@ version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1" +[[package]] +name = "futures-macro" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.52", +] + [[package]] name = "futures-sink" version = "0.3.30" @@ -742,6 +814,7 @@ dependencies = [ "futures-channel", "futures-core", "futures-io", + "futures-macro", "futures-sink", "futures-task", "memchr", @@ -1068,14 +1141,17 @@ dependencies = [ "axum", "axum-extra", "chrono", + "futures-util", "http", "http-body", "http-body-util", + "moka", "once_cell", "sea-orm", "serde", "serde_json", "thiserror", + "tokio", "toml", "tracing", "tracing-appender", @@ -1170,6 +1246,30 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "moka" +version = "0.12.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87bfd249f570638bfb0b4f9d258e6b8cddd2a5a7d0ed47e8bb8b176bfc0e7a17" +dependencies = [ + "async-lock", + "async-trait", + "crossbeam-channel", + "crossbeam-epoch", + "crossbeam-utils", + "event-listener 5.3.0", + "futures-util", + "once_cell", + "parking_lot", + "quanta", + "rustc_version", + "smallvec", + "tagptr", + "thiserror", + "triomphe", + "uuid", +] + [[package]] name = "nix" version = "0.28.0" @@ -1330,6 +1430,12 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" +[[package]] +name = "parking" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb813b8af86854136c6922af0598d719255ecb2179515e6e7730d468f05c9cae" + [[package]] name = "parking_lot" version = "0.12.1" @@ -1507,6 +1613,21 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "quanta" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e5167a477619228a0b284fac2674e3c388cba90631d7b7de620e6f1fcd08da5" +dependencies = [ + "crossbeam-utils", + "libc", + "once_cell", + "raw-cpuid", + "wasi", + "web-sys", + "winapi", +] + [[package]] name = "quote" version = "1.0.35" @@ -1552,6 +1673,15 @@ dependencies = [ "getrandom", ] +[[package]] +name = "raw-cpuid" +version = "11.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d86a7c4638d42c44551f4791a20e687dbb4c3de1f33c43dd71e355cd429def1" +dependencies = [ + "bitflags 2.4.2", +] + [[package]] name = "redox_syscall" version = "0.4.1" @@ -1685,6 +1815,15 @@ version = "0.1.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" +[[package]] +name = "rustc_version" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" +dependencies = [ + "semver", +] + [[package]] name = "rustix" version = "0.38.31" @@ -1850,6 +1989,12 @@ version = "4.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1c107b6f4780854c8b126e228ea8869f4d7b71260f962fefb57b996b8959ba6b" +[[package]] +name = "semver" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92d43fe69e652f3df9bdc2b85b2854a0825b86e4fb76bc44d945137d053639ca" + [[package]] name = "serde" version = "1.0.197" @@ -2069,7 +2214,7 @@ dependencies = [ "crossbeam-queue", "dotenvy", "either", - "event-listener", + "event-listener 2.5.3", "futures-channel", "futures-core", "futures-intrusive", @@ -2334,6 +2479,12 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" +[[package]] +name = "tagptr" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b2093cf4c8eb1e67749a6762251bc9cd836b6fc171623bd0a9d324d37af2417" + [[package]] name = "tap" version = "1.0.1" @@ -2656,6 +2807,12 @@ dependencies = [ "tracing-serde", ] +[[package]] +name = "triomphe" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "859eb650cfee7434994602c3a68b25d77ad9e68c8a6cd491616ef86661382eb3" + [[package]] name = "typenum" version = "1.17.0" @@ -2735,6 +2892,7 @@ version = "1.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f00cc9702ca12d3c81455259621e676d0f7251cec66a21e98fe2e9a37db93b2a" dependencies = [ + "getrandom", "serde", ] @@ -2852,6 +3010,16 @@ version = "0.2.92" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96" +[[package]] +name = "web-sys" +version = "0.3.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77afa9a11836342370f4817622a2f0f418b134426d91a82dfb48f532d2ec13ef" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + [[package]] name = "web-time" version = "1.1.0" diff --git a/library/Cargo.toml b/library/Cargo.toml index 322cae1..44ad272 100644 --- a/library/Cargo.toml +++ b/library/Cargo.toml @@ -19,4 +19,7 @@ ulid = "1.1.2" serde_json = "1.0.114" http = "1.1.0" http-body = "1.0.0" -http-body-util = "0.1.0" \ No newline at end of file +http-body-util = "0.1.0" +moka = { version = "0.12", features = ["future"] } +tokio = { version = "1", features = ["rt-multi-thread", "macros" ] } +futures-util = "0.3" \ No newline at end of file diff --git a/library/src/core/local_cache.rs b/library/src/core/local_cache.rs new file mode 100644 index 0000000..68a9a12 --- /dev/null +++ b/library/src/core/local_cache.rs @@ -0,0 +1,39 @@ +use std::collections::HashMap; +use moka::future::Cache; + + +// use std::any::{Any, TypeId}; +// use std::collections::HashMap; +// use std::lazy::SyncLazy; +// use moka::sync::Cache; +// +// type CacheKey = String; +// +// // 全局缓存,键为 String,值为 Box +// static GLOBAL_CACHE: SyncLazy>>> = SyncLazy::new(|| { +// HashMap::new() +// }); +// +// fn insert(key: CacheKey, value: T) { +// let cache_type_id = TypeId::of::(); +// let mut cache = GLOBAL_CACHE.entry(cache_type_id).or_insert_with(|| { +// let config = moka::Config::builder().max_capacity(1000).build(); +// Cache::with_config(config) +// }); +// cache.insert(key, Box::new(value)); +// } +// +// fn get(key: CacheKey) -> Option { +// let cache_type_id = TypeId::of::(); +// GLOBAL_CACHE.get(&cache_type_id)?.get(&key)?.downcast::().ok() +// } +// +// fn main() { +// insert("int_key".into(), 42); +// let int_value: Option = get("int_key".into()); +// println!("Integer value: {:?}", int_value); +// +// insert("string_key".into(), "hello".to_string()); +// let string_value: Option = get("string_key".into()); +// println!("String value: {:?}", string_value); +// } \ No newline at end of file diff --git a/library/src/core/mod.rs b/library/src/core/mod.rs index 170293b..c8911b9 100644 --- a/library/src/core/mod.rs +++ b/library/src/core/mod.rs @@ -1,3 +1,4 @@ pub mod config; pub mod logger; pub mod db; +pub mod local_cache;