From 65f17ba36372f166695692230bfa484c19187e92 Mon Sep 17 00:00:00 2001 From: JacobLinCool Date: Fri, 2 Jun 2023 18:17:12 +0800 Subject: [PATCH] feat: support moka in-memory cache --- Cargo.lock | 168 +++++++++++++++++++++++++++++++++++++- Cargo.toml | 2 +- src/judger/io_fast/mod.rs | 24 +++--- 3 files changed, 178 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 611a31e..0a053e9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -400,6 +400,12 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "bytecount" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c676a478f63e9fa2dd5368a42f28bba0d6c560b775f38583c8bbaa7fcd67c9c" + [[package]] name = "byteorder" version = "1.4.3" @@ -441,6 +447,37 @@ dependencies = [ "walkdir", ] +[[package]] +name = "camino" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c530edf18f37068ac2d977409ed5cd50d53d73bc653c7647b48eb78976ac9ae2" +dependencies = [ + "serde", +] + +[[package]] +name = "cargo-platform" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cbdb825da8a5df079a43676dbe042702f1707b1109f713a01420fbb4cc71fa27" +dependencies = [ + "serde", +] + +[[package]] +name = "cargo_metadata" +version = "0.14.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4acbb09d9ee8e23699b9634375c72795d095bf268439da88562cf9b501f181fa" +dependencies = [ + "camino", + "cargo-platform", + "semver 1.0.17", + "serde", + "serde_json", +] + [[package]] name = "cc" version = "1.0.79" @@ -936,6 +973,15 @@ dependencies = [ "libc", ] +[[package]] +name = "error-chain" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d2f06b9cac1506ece98fe3231e3cc9c4410ec3d5b1f24ae1c8946f0742cdefc" +dependencies = [ + "version_check", +] + [[package]] name = "event-listener" version = "2.5.3" @@ -1339,6 +1385,7 @@ dependencies = [ "http", "http-cache-semantics", "httpdate", + "moka", "serde", "url", ] @@ -1652,6 +1699,15 @@ dependencies = [ "libc", ] +[[package]] +name = "mach2" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d0d1830bcd151a6fc4aea1369af235b36c1528fe976b8ff678683c9995eade8" +dependencies = [ + "libc", +] + [[package]] name = "matchers" version = "0.1.0" @@ -1754,6 +1810,32 @@ dependencies = [ "windows-sys 0.45.0", ] +[[package]] +name = "moka" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0d3b8e76a2e4b17de765db9432e377a171c42fbe0512b8bc860ff1bfe2e273b" +dependencies = [ + "async-io", + "async-lock", + "crossbeam-channel", + "crossbeam-epoch", + "crossbeam-utils", + "futures-util", + "num_cpus", + "once_cell", + "parking_lot", + "quanta", + "rustc_version 0.4.0", + "scheduled-thread-pool", + "skeptic", + "smallvec", + "tagptr", + "thiserror", + "triomphe", + "uuid", +] + [[package]] name = "more-asserts" version = "0.2.2" @@ -2169,6 +2251,33 @@ dependencies = [ "unicase", ] +[[package]] +name = "pulldown-cmark" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77a1a2f1f0a7ecff9c31abbe177637be0e97a0aef46cf8738ece09327985d998" +dependencies = [ + "bitflags 1.3.2", + "memchr", + "unicase", +] + +[[package]] +name = "quanta" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a17e662a7a8291a865152364c20c7abc5e60486ab2001e8ec10b24862de0b9ab" +dependencies = [ + "crossbeam-utils", + "libc", + "mach2", + "once_cell", + "raw-cpuid", + "wasi", + "web-sys", + "winapi", +] + [[package]] name = "quote" version = "1.0.27" @@ -2208,6 +2317,15 @@ dependencies = [ "getrandom", ] +[[package]] +name = "raw-cpuid" +version = "10.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c297679cb867470fa8c9f67dbba74a78d78e3e98d7cf2b08d6d71540f797332" +dependencies = [ + "bitflags 1.3.2", +] + [[package]] name = "rayon" version = "1.7.0" @@ -2631,6 +2749,15 @@ dependencies = [ "windows-sys 0.42.0", ] +[[package]] +name = "scheduled-thread-pool" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3cbc66816425a074528352f5789333ecff06ca41b36b0b0efdfbb29edc391a19" +dependencies = [ + "parking_lot", +] + [[package]] name = "scoped-tls" version = "1.0.1" @@ -2696,6 +2823,9 @@ name = "semver" version = "1.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bebd363326d05ec3e2f532ab7660680f3b02130d780c299bca73469d521bc0ed" +dependencies = [ + "serde", +] [[package]] name = "semver-parser" @@ -2892,6 +3022,21 @@ dependencies = [ "time 0.3.21", ] +[[package]] +name = "skeptic" +version = "0.13.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16d23b015676c90a0f01c197bfdc786c20342c73a0afdda9025adb0bc42940a8" +dependencies = [ + "bytecount", + "cargo_metadata", + "error-chain", + "glob", + "pulldown-cmark 0.9.3", + "tempfile", + "walkdir", +] + [[package]] name = "slab" version = "0.4.8" @@ -3079,6 +3224,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "tagptr" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b2093cf4c8eb1e67749a6762251bc9cd836b6fc171623bd0a9d324d37af2417" + [[package]] name = "target-lexicon" version = "0.12.7" @@ -3405,6 +3556,12 @@ dependencies = [ "tracing-log", ] +[[package]] +name = "triomphe" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1ee9bd9239c339d714d657fac840c6d2a4f9c45f4f9ec7b0975113458be78db" + [[package]] name = "try-lock" version = "0.2.4" @@ -3514,6 +3671,15 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" +[[package]] +name = "uuid" +version = "1.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "345444e32442451b267fc254ae85a209c64be56d2890e601a0c37ff0c3c5ecd2" +dependencies = [ + "getrandom", +] + [[package]] name = "valuable" version = "0.1.0" @@ -3692,7 +3858,7 @@ checksum = "9bd0acb6d70885ea0c343749019ba74f015f64a9d30542e66db69b49b7e28186" dependencies = [ "anyhow", "id-arena", - "pulldown-cmark", + "pulldown-cmark 0.8.0", "unicode-normalization", "unicode-xid", ] diff --git a/Cargo.toml b/Cargo.toml index 4a0663f..309eb3f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,7 @@ async-compression = { version = "0.4.0", features = ["tokio", "gzip"] } async-trait = "0.1.68" base64 = "0.21.0" clap = { version = "4.2.7", features = ["derive", "string"] } -http-cache-reqwest = "0.9.0" +http-cache-reqwest = { version = "0.9.0", features = ["manager-cacache", "manager-moka"] } jsonwebtoken = "8.3.0" reqwest = { version = "0.11.18", default-features = false, features = ["rustls", "tokio-rustls", "json"] } reqwest-middleware = "0.2.2" diff --git a/src/judger/io_fast/mod.rs b/src/judger/io_fast/mod.rs index 5d00a69..5a89db7 100644 --- a/src/judger/io_fast/mod.rs +++ b/src/judger/io_fast/mod.rs @@ -1,6 +1,6 @@ use super::{Input, Judger, Output}; use async_trait::async_trait; -use http_cache_reqwest::{CACacheManager, Cache, CacheMode, HttpCache}; +use http_cache_reqwest::{CACacheManager, Cache, CacheMode, HttpCache, MokaManager}; use reqwest::Client; use reqwest_middleware::ClientBuilder; use serde::{Deserialize, Serialize}; @@ -59,32 +59,26 @@ impl Judger for FastIOJudgeSpec { match env::var("REMOTE_INPUT_CACHE") { Ok(val) => match val.as_str() { - "true" | "1" => { + "true" | "1" | "cacache" => { client = client.with(Cache(HttpCache { mode: CacheMode::Default, manager: CACacheManager::default(), options: None, })); } - "false" | "0" => { + "mem" | "moka" => { client = client.with(Cache(HttpCache { - mode: CacheMode::NoStore, - manager: CACacheManager::default(), - options: None, - })); - } - _ => { - client = client.with(Cache(HttpCache { - mode: CacheMode::NoStore, - manager: CACacheManager::default(), + mode: CacheMode::Default, + manager: MokaManager::default(), options: None, })); } + _ => {} }, Err(_) => { client = client.with(Cache(HttpCache { - mode: CacheMode::NoStore, - manager: CACacheManager::default(), + mode: CacheMode::Default, + manager: MokaManager::default(), options: None, })); } @@ -97,6 +91,7 @@ impl Judger for FastIOJudgeSpec { req = req.header("Authorization", format!("Bearer {}", auth)); } + info!("Fetching input from {}", input_url); let res = req .send() .await @@ -106,6 +101,7 @@ impl Judger for FastIOJudgeSpec { .text() .await .map_err(|e| format!("Error reading input: {}", e))?; + info!("Fetched input from {}", input_url); return Ok(Input { stdin: input }); }