fix meme response cache to include filters
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
c2876ef915
commit
34bc07c3c4
4 changed files with 44 additions and 26 deletions
|
@ -26,4 +26,5 @@ term-table = "1.3.0"
|
|||
term_size = "0.3.2"
|
||||
tokio = { version = "0.2.23", features = ["macros", "fs", "process"] }
|
||||
url = "2.2.0"
|
||||
lazy_static = "1.4.0"
|
||||
|
||||
|
|
|
@ -14,8 +14,8 @@ pub async fn run(
|
|||
let (memes, ..) = tokio::try_join!(
|
||||
api::memes(
|
||||
http,
|
||||
cat.as_ref().map(String::as_ref),
|
||||
user.as_ref().map(String::as_ref)
|
||||
cat.as_ref().map(String::from),
|
||||
user.as_ref().map(String::from)
|
||||
),
|
||||
async {
|
||||
if let Some(c) = cat.as_ref() {
|
||||
|
|
|
@ -13,8 +13,8 @@ pub async fn run(
|
|||
let (memes, ..) = tokio::try_join!(
|
||||
api::memes(
|
||||
http,
|
||||
cat.as_ref().map(String::as_ref),
|
||||
user.as_ref().map(String::as_ref),
|
||||
cat.as_ref().map(String::clone),
|
||||
user.as_ref().map(String::clone),
|
||||
),
|
||||
async {
|
||||
if let Some(u) = user.as_ref() {
|
||||
|
@ -36,7 +36,7 @@ pub async fn run(
|
|||
|
||||
info!("Starting search with query '{}'", query);
|
||||
|
||||
for meme in memes {
|
||||
for meme in memes.iter() {
|
||||
let file_name = meme.file_name()?;
|
||||
if let Some(score) = fuzzy_matcher::clangd::fuzzy_match(file_name, &query) {
|
||||
info!("Found matching meme '{}' with score {}", file_name, score);
|
||||
|
|
|
@ -27,15 +27,24 @@ pub mod consts {
|
|||
pub mod api {
|
||||
use crate::api::{CatsResp, Meme, MemesResp, User, UsersResp};
|
||||
use anyhow::Result;
|
||||
use lazy_static::lazy_static;
|
||||
use log::info;
|
||||
use once_cell::sync::OnceCell;
|
||||
use reqwest::Client;
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
use url::Url;
|
||||
|
||||
// cached api responses
|
||||
static CATS: OnceCell<Vec<String>> = OnceCell::new();
|
||||
static MEMES: OnceCell<Vec<Meme>> = OnceCell::new();
|
||||
static USERS: OnceCell<Vec<User>> = OnceCell::new();
|
||||
static CATS: OnceCell<Vec<String>> = OnceCell::new();
|
||||
lazy_static! {
|
||||
// is this type long enough yet?
|
||||
static ref MEMES: Mutex<HashMap<(Option<String>, Option<String>), Arc<Vec<Meme>>>> =
|
||||
Mutex::new(HashMap::new());
|
||||
}
|
||||
|
||||
pub async fn cats(http: &Client) -> Result<&Vec<String>> {
|
||||
Ok(init_once_cell!(CATS, {
|
||||
|
@ -51,30 +60,38 @@ pub mod api {
|
|||
|
||||
pub async fn memes<'a>(
|
||||
http: &Client,
|
||||
cat_filter: Option<&str>,
|
||||
usr_filter: Option<&str>,
|
||||
) -> Result<&'a Vec<Meme>> {
|
||||
Ok(init_once_cell!(MEMES, {
|
||||
let mut url = Url::options().parse("https://data.tilera.xyz/api/jensmemes/memes")?;
|
||||
let mut pairs = url.query_pairs_mut();
|
||||
cat_filter: Option<String>,
|
||||
usr_filter: Option<String>,
|
||||
) -> Result<Arc<Vec<Meme>>> {
|
||||
let filters = (cat_filter, usr_filter);
|
||||
if let Some(m) = MEMES.lock().unwrap().get(&filters) {
|
||||
return Ok(m.clone());
|
||||
}
|
||||
|
||||
if let Some(cat) = cat_filter {
|
||||
pairs.append_pair("category", cat);
|
||||
}
|
||||
let mut url = Url::options().parse("https://data.tilera.xyz/api/jensmemes/memes")?;
|
||||
let mut pairs = url.query_pairs_mut();
|
||||
|
||||
if let Some(usr) = usr_filter {
|
||||
pairs.append_pair("user", usr);
|
||||
}
|
||||
if let Some(cat) = filters.0.as_ref() {
|
||||
pairs.append_pair("category", cat);
|
||||
}
|
||||
|
||||
// drop required in order to move the URL into the request
|
||||
drop(pairs);
|
||||
if let Some(usr) = filters.1.as_ref() {
|
||||
pairs.append_pair("user", usr);
|
||||
}
|
||||
|
||||
info!("Requesting memes from server");
|
||||
let res = http.get(url).send().await?;
|
||||
let memes = serde_json::from_slice::<MemesResp>(&res.bytes().await?)?;
|
||||
// drop required in order to move the URL into the request
|
||||
drop(pairs);
|
||||
|
||||
memes.memes
|
||||
}))
|
||||
info!("Requesting memes from server");
|
||||
let res = http.get(url).send().await?;
|
||||
let memes = serde_json::from_slice::<MemesResp>(&res.bytes().await?)?;
|
||||
|
||||
Ok(MEMES
|
||||
.lock()
|
||||
.unwrap()
|
||||
.entry(filters)
|
||||
.or_insert(Arc::new(memes.memes))
|
||||
.clone())
|
||||
}
|
||||
|
||||
pub async fn users(http: &Client) -> Result<&Vec<User>> {
|
||||
|
|
Loading…
Add table
Reference in a new issue