Experiment with caching

This commit is contained in:
Robin 2021-01-23 10:48:33 +01:00
parent baf7272cfd
commit 077c222a4e
7 changed files with 108 additions and 8 deletions

97
Cargo.lock generated
View File

@ -346,6 +346,15 @@ dependencies = [
"toml", "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]] [[package]]
name = "async-recursion" name = "async-recursion"
version = "0.3.1" version = "0.3.1"
@ -511,6 +520,40 @@ dependencies = [
"bytes 1.0.1", "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]] [[package]]
name = "cc" name = "cc"
version = "1.0.66" version = "1.0.66"
@ -573,6 +616,41 @@ dependencies = [
"cfg-if 1.0.0", "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]] [[package]]
name = "derive_more" name = "derive_more"
version = "0.99.11" version = "0.99.11"
@ -626,6 +704,12 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "event-listener"
version = "2.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7531096570974c3a9dcf9e4b8e1cede1ec26cf5046219fb3b9d897503b9be59"
[[package]] [[package]]
name = "flate2" name = "flate2"
version = "1.0.19" version = "1.0.19"
@ -885,6 +969,12 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6cab2627acfc432780848602f3f558f7e9dd427352224b0d9324025796d2a5e" checksum = "b6cab2627acfc432780848602f3f558f7e9dd427352224b0d9324025796d2a5e"
[[package]]
name = "ident_case"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
[[package]] [[package]]
name = "idna" name = "idna"
version = "0.2.0" version = "0.2.0"
@ -1000,6 +1090,7 @@ dependencies = [
"askama", "askama",
"async-recursion", "async-recursion",
"base64 0.13.0", "base64 0.13.0",
"cached",
"futures", "futures",
"regex", "regex",
"serde", "serde",
@ -1647,6 +1738,12 @@ version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "213701ba3370744dcd1a12960caa4843b3d68b4d1c0a5d575e0d65b2ee9d16c0" checksum = "213701ba3370744dcd1a12960caa4843b3d68b4d1c0a5d575e0d65b2ee9d16c0"
[[package]]
name = "strsim"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6446ced80d6c486436db5c078dde11a9f73d42b57fb273121e160b84f63d894c"
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.58" version = "1.0.58"

View File

@ -19,3 +19,4 @@ async-recursion = "0.3.1"
url = "2.2.0" url = "2.2.0"
regex = "1.4.2" regex = "1.4.2"
time = "0.2.23" time = "0.2.23"
cached = "0.23.0"

View File

@ -37,7 +37,7 @@ pub async fn item(req: HttpRequest) -> HttpResponse {
dbg!(req.match_info().get("id").unwrap_or("")); dbg!(req.match_info().get("id").unwrap_or(""));
// Send a request to the url, receive JSON in response // 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 // Otherwise, grab the JSON output from the request
Ok(res) => { Ok(res) => {
// Parse the JSON into Post and Comment structs // Parse the JSON into Post and Comment structs

View File

@ -75,7 +75,7 @@ async fn search_subreddits(q: String) -> Vec<Subreddit> {
let subreddit_search_path = format!("/subreddits/search.json?q={}&limit=3", q.replace(' ', "+")); let subreddit_search_path = format!("/subreddits/search.json?q={}&limit=3", q.replace(' ', "+"));
// Send a request to the url // Send a request to the url
match request(&subreddit_search_path).await { match request(subreddit_search_path).await {
// If success, receive JSON in response // If success, receive JSON in response
Ok(response) => { Ok(response) => {
match response["data"]["children"].as_array() { match response["data"]["children"].as_array() {

View File

@ -68,7 +68,7 @@ pub async fn wiki(req: HttpRequest) -> HttpResponse {
let page = req.match_info().get("page").unwrap_or("index").to_string(); let page = req.match_info().get("page").unwrap_or("index").to_string();
let path: String = format!("/r/{}/wiki/{}.json?raw_json=1", sub, page); let path: String = format!("/r/{}/wiki/{}.json?raw_json=1", sub, page);
match request(&path).await { match request(path).await {
Ok(res) => { Ok(res) => {
let s = WikiTemplate { let s = WikiTemplate {
sub, sub,
@ -90,7 +90,7 @@ async fn subreddit(sub: &str) -> Result<Subreddit, String> {
let path: String = format!("/r/{}/about.json?raw_json=1", sub); let path: String = format!("/r/{}/about.json?raw_json=1", sub);
// Send a request to the url // Send a request to the url
match request(&path).await { match request(path).await {
// If success, receive JSON in response // If success, receive JSON in response
Ok(res) => { Ok(res) => {
// Metadata regarding the subreddit // Metadata regarding the subreddit

View File

@ -54,7 +54,7 @@ async fn user(name: &str) -> Result<User, String> {
let path: String = format!("/user/{}/about.json", name); let path: String = format!("/user/{}/about.json", name);
// Send a request to the url // Send a request to the url
match request(&path).await { match request(path).await {
// If success, receive JSON in response // If success, receive JSON in response
Ok(res) => { Ok(res) => {
// Grab creation date as unix timestamp // Grab creation date as unix timestamp

View File

@ -9,6 +9,7 @@ use serde_json::{from_str, Value};
use std::collections::HashMap; use std::collections::HashMap;
use time::{Duration, OffsetDateTime}; use time::{Duration, OffsetDateTime};
use url::Url; use url::Url;
use cached::proc_macro::cached;
// //
// STRUCTS // STRUCTS
@ -295,7 +296,7 @@ pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec<Post
let post_list; let post_list;
// Send a request to the url // Send a request to the url
match request(&path).await { match request(path.to_string()).await {
// If success, receive JSON in response // If success, receive JSON in response
Ok(response) => { Ok(response) => {
res = response; 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 // Make a request to a Reddit API and parse the JSON response
pub async fn request(path: &str) -> Result<Value, String> { #[cached(size=1000,time=120, result = true)]
pub async fn request(path: String) -> Result<Value, String> {
let url = format!("https://www.reddit.com{}", path); let url = format!("https://www.reddit.com{}", path);
let user_agent = format!("web:libreddit:{}", env!("CARGO_PKG_VERSION")); let user_agent = format!("web:libreddit:{}", env!("CARGO_PKG_VERSION"));