From 077c222a4e98ab87b484b6b6b378923e65ed6058 Mon Sep 17 00:00:00 2001 From: Robin <8597693+robrobinbin@users.noreply.github.com> Date: Sat, 23 Jan 2021 10:48:33 +0100 Subject: [PATCH 1/2] Experiment with caching --- Cargo.lock | 97 ++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 3 +- src/post.rs | 2 +- src/search.rs | 2 +- src/subreddit.rs | 4 +- src/user.rs | 2 +- src/utils.rs | 6 ++- 7 files changed, 108 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ec5d2a2..8ea6217 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -346,6 +346,15 @@ dependencies = [ "toml", ] +[[package]] +name = "async-mutex" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "479db852db25d9dbf6204e6cb6253698f175c15726470f78af0d918e99d6156e" +dependencies = [ + "event-listener", +] + [[package]] name = "async-recursion" version = "0.3.1" @@ -511,6 +520,40 @@ dependencies = [ "bytes 1.0.1", ] +[[package]] +name = "cached" +version = "0.23.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e2afe73808fbaac302e39c9754bfc3c4b4d0f99c9c240b9f4e4efc841ad1b74" +dependencies = [ + "async-mutex", + "async-trait", + "cached_proc_macro", + "cached_proc_macro_types", + "futures", + "hashbrown", + "once_cell", +] + +[[package]] +name = "cached_proc_macro" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf857ae42d910aede5c5186e62684b0d7a597ce2fe3bd14448ab8f7ef439848c" +dependencies = [ + "async-mutex", + "cached_proc_macro_types", + "darling", + "quote", + "syn", +] + +[[package]] +name = "cached_proc_macro_types" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a4f925191b4367301851c6d99b09890311d74b0d43f274c0b34c86d308a3663" + [[package]] name = "cc" version = "1.0.66" @@ -573,6 +616,41 @@ dependencies = [ "cfg-if 1.0.0", ] +[[package]] +name = "darling" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d706e75d87e35569db781a9b5e2416cff1236a47ed380831f959382ccd5f858" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0c960ae2da4de88a91b2d920c2a7233b400bc33cb28453a2987822d8392519b" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "strsim", + "syn", +] + +[[package]] +name = "darling_macro" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b5a2f4ac4969822c62224815d069952656cadc7084fdca9751e6d959189b72" +dependencies = [ + "darling_core", + "quote", + "syn", +] + [[package]] name = "derive_more" version = "0.99.11" @@ -626,6 +704,12 @@ dependencies = [ "syn", ] +[[package]] +name = "event-listener" +version = "2.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7531096570974c3a9dcf9e4b8e1cede1ec26cf5046219fb3b9d897503b9be59" + [[package]] name = "flate2" version = "1.0.19" @@ -885,6 +969,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6cab2627acfc432780848602f3f558f7e9dd427352224b0d9324025796d2a5e" +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + [[package]] name = "idna" version = "0.2.0" @@ -1000,6 +1090,7 @@ dependencies = [ "askama", "async-recursion", "base64 0.13.0", + "cached", "futures", "regex", "serde", @@ -1647,6 +1738,12 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "213701ba3370744dcd1a12960caa4843b3d68b4d1c0a5d575e0d65b2ee9d16c0" +[[package]] +name = "strsim" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6446ced80d6c486436db5c078dde11a9f73d42b57fb273121e160b84f63d894c" + [[package]] name = "syn" version = "1.0.58" diff --git a/Cargo.toml b/Cargo.toml index d79d1d9..5fa7d9e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,4 +18,5 @@ serde_json = "1.0" async-recursion = "0.3.1" url = "2.2.0" regex = "1.4.2" -time = "0.2.23" \ No newline at end of file +time = "0.2.23" +cached = "0.23.0" diff --git a/src/post.rs b/src/post.rs index 08d942c..7040de3 100644 --- a/src/post.rs +++ b/src/post.rs @@ -37,7 +37,7 @@ pub async fn item(req: HttpRequest) -> HttpResponse { dbg!(req.match_info().get("id").unwrap_or("")); // Send a request to the url, receive JSON in response - match request(&path).await { + match request(path).await { // Otherwise, grab the JSON output from the request Ok(res) => { // Parse the JSON into Post and Comment structs diff --git a/src/search.rs b/src/search.rs index 35330c5..ab6b056 100644 --- a/src/search.rs +++ b/src/search.rs @@ -75,7 +75,7 @@ async fn search_subreddits(q: String) -> Vec { let subreddit_search_path = format!("/subreddits/search.json?q={}&limit=3", q.replace(' ', "+")); // Send a request to the url - match request(&subreddit_search_path).await { + match request(subreddit_search_path).await { // If success, receive JSON in response Ok(response) => { match response["data"]["children"].as_array() { diff --git a/src/subreddit.rs b/src/subreddit.rs index ea57322..e7fa525 100644 --- a/src/subreddit.rs +++ b/src/subreddit.rs @@ -68,7 +68,7 @@ pub async fn wiki(req: HttpRequest) -> HttpResponse { let page = req.match_info().get("page").unwrap_or("index").to_string(); let path: String = format!("/r/{}/wiki/{}.json?raw_json=1", sub, page); - match request(&path).await { + match request(path).await { Ok(res) => { let s = WikiTemplate { sub, @@ -90,7 +90,7 @@ async fn subreddit(sub: &str) -> Result { let path: String = format!("/r/{}/about.json?raw_json=1", sub); // Send a request to the url - match request(&path).await { + match request(path).await { // If success, receive JSON in response Ok(res) => { // Metadata regarding the subreddit diff --git a/src/user.rs b/src/user.rs index be11069..96e3023 100644 --- a/src/user.rs +++ b/src/user.rs @@ -54,7 +54,7 @@ async fn user(name: &str) -> Result { let path: String = format!("/user/{}/about.json", name); // Send a request to the url - match request(&path).await { + match request(path).await { // If success, receive JSON in response Ok(res) => { // Grab creation date as unix timestamp diff --git a/src/utils.rs b/src/utils.rs index 0d32c03..dbe0cdb 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -9,6 +9,7 @@ use serde_json::{from_str, Value}; use std::collections::HashMap; use time::{Duration, OffsetDateTime}; use url::Url; +use cached::proc_macro::cached; // // STRUCTS @@ -295,7 +296,7 @@ pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec { res = response; @@ -392,7 +393,8 @@ pub async fn error(msg: String) -> HttpResponse { } // Make a request to a Reddit API and parse the JSON response -pub async fn request(path: &str) -> Result { +#[cached(size=1000,time=120, result = true)] +pub async fn request(path: String) -> Result { let url = format!("https://www.reddit.com{}", path); let user_agent = format!("web:libreddit:{}", env!("CARGO_PKG_VERSION")); From 226d39328c52384c9e9787e787c11eb5796f75bf Mon Sep 17 00:00:00 2001 From: robrobinbin <8597693+robrobinbin@users.noreply.github.com> Date: Sat, 23 Jan 2021 22:45:41 +0100 Subject: [PATCH 2/2] Make it 60 --- src/utils.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.rs b/src/utils.rs index 2d663e5..0c472f2 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -393,7 +393,7 @@ pub async fn error(msg: String) -> HttpResponse { } // Make a request to a Reddit API and parse the JSON response -#[cached(size=1000,time=120, result = true)] +#[cached(size=1000,time=60, result = true)] pub async fn request(path: String) -> Result { let url = format!("https://www.reddit.com{}", path); let user_agent = format!("web:libreddit:{}", env!("CARGO_PKG_VERSION"));