From 19c4660eb8ce95ee1438158c2c780240c188a2d0 Mon Sep 17 00:00:00 2001 From: Gabriel Simmer Date: Mon, 28 Aug 2023 10:41:43 +0100 Subject: [PATCH] Add kv based cache --- Cargo.lock | 66 +++++++++++++++++++++++++++++++++++++++++++++++++-- Cargo.toml | 3 +++ src/lib.rs | 45 +++++++++++++++++++++++++++++------ wrangler.toml | 4 ++++ 4 files changed, 109 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e6246fa..c5fc4f2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -32,6 +32,15 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + [[package]] name = "async-trait" version = "0.1.73" @@ -129,9 +138,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec837a71355b28f6556dbd569b37b3f363091c0bd4b2e735674521b4c5fd9bc5" dependencies = [ "android-tzdata", + "iana-time-zone", "js-sys", "num-traits", + "time", "wasm-bindgen", + "winapi", ] [[package]] @@ -383,7 +395,7 @@ dependencies = [ "cfg-if", "js-sys", "libc", - "wasi", + "wasi 0.11.0+wasi-snapshot-preview1", "wasm-bindgen", ] @@ -422,6 +434,7 @@ checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" name = "hn-rss" version = "0.1.0" dependencies = [ + "chrono", "getrandom", "regex", "reqwest", @@ -500,6 +513,29 @@ dependencies = [ "tokio-native-tls", ] +[[package]] +name = "iana-time-zone" +version = "0.1.57" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2fad5b825842d2b38bd206f3e81d6957625fd7f0a361e345c30e01a0ae2dd613" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "windows", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + [[package]] name = "ident_case" version = "1.0.1" @@ -605,7 +641,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "927a765cd3fc26206e66b296465fa9d3e5ab003e651c1b3c060e7956d96b19d2" dependencies = [ "libc", - "wasi", + "wasi 0.11.0+wasi-snapshot-preview1", "windows-sys", ] @@ -1129,6 +1165,17 @@ dependencies = [ "syn 2.0.29", ] +[[package]] +name = "time" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b797afad3f312d1c66a56d11d0316f916356d11bd158fbc6ca6389ff6bf805a" +dependencies = [ + "libc", + "wasi 0.10.0+wasi-snapshot-preview1", + "winapi", +] + [[package]] name = "tinyvec" version = "1.6.0" @@ -1262,6 +1309,12 @@ dependencies = [ "try-lock", ] +[[package]] +name = "wasi" +version = "0.10.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" @@ -1379,6 +1432,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e686886bc078bc1b0b600cac0147aadb815089b6e4da64016cbd754b6342700f" +dependencies = [ + "windows-targets", +] + [[package]] name = "windows-sys" version = "0.48.0" diff --git a/Cargo.toml b/Cargo.toml index 5557faf..44fd959 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,9 @@ reqwest = "0.11" rss = "2.0" regex = "1.5" worker = "0.0.18" +chrono = "0.4.26" + +[target.wasm32-unknown-unknown.dependencies] getrandom = { version = "0.2", features = ["js"] } [lib] diff --git a/src/lib.rs b/src/lib.rs index e65f82f..f76d201 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,9 +1,8 @@ use regex::Regex; -use reqwest::{Error as ReqwestError, StatusCode}; +use reqwest::Error as ReqwestError; use rss::{Channel, Error as RssError}; use std::io::Cursor; - -use worker::{event, Env, Headers, Request, Response, Router}; +use worker::{console_log, event, kv::KvStore, Env, Headers, Request, Response, Router}; #[derive(Debug)] enum CustomError { @@ -28,14 +27,32 @@ pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> worker::Resu let router = Router::new(); router .get_async("/", |_, ctx| async move { - let rss = match fetch_modified_rss().await { - Ok(r) => r, - Err(e) => "".to_owned(), + let kv = match ctx.kv("HN") { + Ok(k) => k, + Err(e) => panic!("{:?}", e), + }; + + let rss = match kv.get("feed").text().await { + Ok(r) => match r { + Some(s) => s, + None => { + let r = fetch_modified_rss().await.unwrap_or("".to_owned()); + match set_cache(&kv, &r).await { + Some(_) => console_log!("set new rss cache"), + None => console_log!("unable to set rss cache"), + }; + r + } + }, + Err(e) => { + console_log!("{:?}", e); + fetch_modified_rss().await.unwrap_or("".to_owned()) + } }; let res = Response::from_bytes(rss.into()).unwrap(); let mut headers = Headers::new(); - headers.set("content-type", "application/rss+xml"); + let _ = headers.set("content-type", "application/rss+xml"); Ok(res.with_headers(headers)) }) @@ -43,6 +60,20 @@ pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> worker::Resu .await } +async fn set_cache(kv: &KvStore, res: &String) -> Option { + let feed = kv + .put("feed", res) + .unwrap() + .expiration_ttl(1800) + .execute() + .await; + if feed.is_err() { + console_log!("{:?}", feed.err()); + return None; + } + Some(true) +} + async fn fetch_modified_rss() -> Result { let url = "https://news.ycombinator.com/rss"; diff --git a/wrangler.toml b/wrangler.toml index 7979334..d1ac5e3 100644 --- a/wrangler.toml +++ b/wrangler.toml @@ -3,6 +3,10 @@ type = "javascript" workers_dev = true compatibility_date = "2022-01-20" +kv_namespaces = [ + { binding = "HN", id = "a30242f65ba34788ab1560f1a9c4eac7", preview_id = "9fd4871334174ceab34d2fd8c0e989cc" } +] + [vars] WORKERS_RS_VERSION = "0.0.18"