Merge pull request #70 from robrobinbin/master

Experiment with caching requests to Reddit
This commit is contained in:
Spike 2021-01-23 21:50:16 +00:00 committed by GitHub
commit 8f913e696c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 120 additions and 20 deletions

121
Cargo.lock generated
View File

@ -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"
@ -401,9 +410,9 @@ dependencies = [
[[package]]
name = "backtrace"
version = "0.3.55"
version = "0.3.56"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef5140344c85b01f9bbb4d4b7288a8aa4b3287ccef913a14bcc78a1063623598"
checksum = "9d117600f438b1707d4e4ae15d3595657288f8235a0eb593e80ecc98ab34e1bc"
dependencies = [
"addr2line",
"cfg-if 1.0.0",
@ -480,9 +489,9 @@ dependencies = [
[[package]]
name = "bumpalo"
version = "3.4.0"
version = "3.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e8c087f005730276d1096a652e92a8bacee2e2472bcc9715a74d2bec38b5820"
checksum = "f07aa6688c702439a1be0307b6a94dffe1168569e45b9500c1372bc580740d59"
[[package]]
name = "byteorder"
@ -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",
@ -1162,9 +1253,9 @@ dependencies = [
[[package]]
name = "object"
version = "0.22.0"
version = "0.23.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d3b63360ec3cb337817c2dbd47ab4a0f170d285d8e5a2064600f3def1402397"
checksum = "a9a7ab5d64814df0fe4a4b5ead45ed6c5f181ee3ff04ba344313a6c80446c5d4"
[[package]]
name = "once_cell"
@ -1648,10 +1739,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "213701ba3370744dcd1a12960caa4843b3d68b4d1c0a5d575e0d65b2ee9d16c0"
[[package]]
name = "syn"
version = "1.0.58"
name = "strsim"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cc60a3d73ea6594cd712d830cc1f0390fd71542d8c8cd24e70cc54cdfd5e05d5"
checksum = "6446ced80d6c486436db5c078dde11a9f73d42b57fb273121e160b84f63d894c"
[[package]]
name = "syn"
version = "1.0.59"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "07cb8b1b4ebf86a89ee88cbd201b022b94138c623644d035185c84d3f41b7e66"
dependencies = [
"proc-macro2",
"quote",
@ -1686,11 +1783,11 @@ dependencies = [
[[package]]
name = "thread_local"
version = "1.1.0"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb9bc092d0d51e76b2b19d9d85534ffc9ec2db959a2523cdae0697e2972cd447"
checksum = "301bdd13d23c49672926be451130892d274d3ba0b410c18e00daa7990ff38d99"
dependencies = [
"lazy_static",
"once_cell",
]
[[package]]

View File

@ -18,4 +18,5 @@ serde_json = "1.0"
async-recursion = "0.3"
url = "2.2"
regex = "1.4"
time = "0.2"
time = "0.2"
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(""));
// 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

View File

@ -76,7 +76,7 @@ async fn search_subreddits(q: String) -> Vec<Subreddit> {
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() {

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 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<Subreddit, String> {
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

View File

@ -54,7 +54,7 @@ async fn user(name: &str) -> Result<User, String> {
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

View File

@ -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<Post
let post_list;
// Send a request to the url
match request(&path).await {
match request(path.to_string()).await {
// If success, receive JSON in response
Ok(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
pub async fn request(path: &str) -> Result<Value, String> {
#[cached(size=1000,time=60, result = true)]
pub async fn request(path: String) -> Result<Value, String> {
let url = format!("https://www.reddit.com{}", path);
let user_agent = format!("web:libreddit:{}", env!("CARGO_PKG_VERSION"));