mirror of
https://github.com/redlib-org/redlib.git
synced 2025-05-15 14:32:50 +00:00
Experiment with caching
This commit is contained in:
parent
baf7272cfd
commit
077c222a4e
7 changed files with 108 additions and 8 deletions
|
@ -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
|
||||
|
|
|
@ -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(' ', "+"));
|
||||
|
||||
// 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() {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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=120, 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"));
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue