From 136ad0ecf44ada514f7890d289ebccf4a64c3b4f Mon Sep 17 00:00:00 2001 From: Mylloon Date: Tue, 11 Apr 2023 10:45:25 +0200 Subject: [PATCH] cache all page 60s --- Cargo.lock | 183 ++++++++++++++++++++++++++++++++++++++- Cargo.toml | 1 + src/routes/agreements.rs | 3 + src/routes/contrib.rs | 2 + src/routes/index.rs | 2 + src/routes/networks.rs | 2 + src/routes/not_found.rs | 2 + src/routes/portfolio.rs | 2 + 8 files changed, 196 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 6fe07ab..9080f91 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -271,6 +271,23 @@ version = "0.10.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "619743e34b5ba4e9703bba34deac3427c72507c7159f5fd030aea8cac0cfe341" +[[package]] +name = "async-trait" +version = "0.1.68" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9ccdd8f2a161be9bd5c023df56f1b2a0bd1d83872ae53b71a84a12c9bf6e842" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.13", +] + +[[package]] +name = "async_once" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ce4f10ea3abcd6617873bae9f91d1c5332b4a778bd9ce34d0cd517474c1de82" + [[package]] name = "autocfg" version = "1.1.0" @@ -353,6 +370,44 @@ dependencies = [ "bytes", ] +[[package]] +name = "cached" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e5877db5d1af7fae60d06b5db9430b68056a69b3582a0be8e3691e87654aeb6" +dependencies = [ + "async-trait", + "async_once", + "cached_proc_macro", + "cached_proc_macro_types", + "futures", + "hashbrown 0.13.2", + "instant", + "lazy_static", + "once_cell", + "thiserror", + "tokio", +] + +[[package]] +name = "cached_proc_macro" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e10ca87c81aaa3a949dbbe2b5e6c2c45dbc94ba4897e45ea31ff9ec5087be3dc" +dependencies = [ + "cached_proc_macro_types", + "darling", + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[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.79" @@ -424,6 +479,41 @@ dependencies = [ "nom", ] +[[package]] +name = "darling" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b750cb3417fd1b327431a470f388520309479ab0bf5e323505daf0290cd3850" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "109c1ca6e6b7f82cc233a97004ea8ed7ca123a9af07a8230878fcfda9b158bf0" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "strsim", + "syn 1.0.109", +] + +[[package]] +name = "darling_macro" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4aab4dbc9f7611d8b55048a3a16d2d010c2c8334e46304b40ac1cc14bf3b48e" +dependencies = [ + "darling_core", + "quote", + "syn 1.0.109", +] + [[package]] name = "derive_more" version = "0.99.17" @@ -462,6 +552,7 @@ version = "0.1.0" dependencies = [ "actix-files", "actix-web", + "cached", "glob", "latex2mathml", "markdown", @@ -497,12 +588,42 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "futures" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23342abe12aba583913b2e62f22225ff9c950774065e4bfb61a19cd9770fec40" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "955518d47e09b25bbebc7a18df10b81f0c766eaf4c4f1cccef2fca5f2a4fb5f2" +dependencies = [ + "futures-core", + "futures-sink", +] + [[package]] name = "futures-core" version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4bca583b7e26f571124fe5b7561d49cb2868d79116cfa0eefce955557c6fee8c" +[[package]] +name = "futures-io" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fff74096e71ed47f8e023204cfd0aa1289cd54ae5430a9523be060cdb849964" + [[package]] name = "futures-sink" version = "0.3.28" @@ -522,6 +643,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "26b01e40b772d54cf6c6d721c1d1abd0647a0106a12ecaa1c186273392a69533" dependencies = [ "futures-core", + "futures-sink", "futures-task", "pin-project-lite", "pin-utils", @@ -579,6 +701,12 @@ version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" +[[package]] +name = "hashbrown" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43a3c133739dddd0d2990f9a4bdf8eb4b21ef50e4851ca85ab661199821d510e" + [[package]] name = "heck" version = "0.3.3" @@ -626,6 +754,12 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4a1e36c821dbe04574f602848a19f742f4fb3c98d40449f11bcad18d6b17421" +[[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.3.0" @@ -643,7 +777,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" dependencies = [ "autocfg", - "hashbrown", + "hashbrown 0.12.3", +] + +[[package]] +name = "instant" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" +dependencies = [ + "cfg-if", ] [[package]] @@ -1202,6 +1345,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + [[package]] name = "syn" version = "1.0.109" @@ -1224,6 +1373,26 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "thiserror" +version = "1.0.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "978c9a314bd8dc99be594bc3c175faaa9794be04a5a5e153caba6915336cebac" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.13", +] + [[package]] name = "time" version = "0.3.20" @@ -1280,9 +1449,21 @@ dependencies = [ "pin-project-lite", "signal-hook-registry", "socket2", + "tokio-macros", "windows-sys", ] +[[package]] +name = "tokio-macros" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61a573bdc87985e9d6ddeed1b3d864e8a302c847e40d647746df2f1de209d1ce" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.13", +] + [[package]] name = "tokio-util" version = "0.7.7" diff --git a/Cargo.toml b/Cargo.toml index f8e35c5..d1f24e0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ license = "AGPL-3.0-or-later" [dependencies] actix-web = "4" actix-files = "0.6" +cached = "0.42.0" ramhorns = "0.14.0" toml = "0.7.3" serde = { version = "1.0.159", features = ["derive"] } diff --git a/src/routes/agreements.rs b/src/routes/agreements.rs index 15d8cef..d61611d 100644 --- a/src/routes/agreements.rs +++ b/src/routes/agreements.rs @@ -1,5 +1,6 @@ use crate::{config::Config, template::Infos}; use actix_web::{dev::ConnectionInfo, get, routes, web, HttpRequest, HttpResponse, Responder}; +use cached::proc_macro::once; use ramhorns::Content; #[routes] @@ -19,6 +20,7 @@ struct SecurityTemplate { url: String, } +#[once(time = 60)] fn get_security(config: Config, info: ConnectionInfo) -> String { config.tmpl.render( "security.txt", @@ -46,6 +48,7 @@ pub async fn robots() -> impl Responder { HttpResponse::Ok().body(get_robots()) } +#[once(time = 60)] fn get_robots() -> String { // TODO, see https://www.robotstxt.org/orig.html "User-agent: * Allow: /".to_string() diff --git a/src/routes/contrib.rs b/src/routes/contrib.rs index 72c0170..2935c7f 100644 --- a/src/routes/contrib.rs +++ b/src/routes/contrib.rs @@ -1,5 +1,6 @@ use crate::{config::Config, template::Infos}; use actix_web::{get, web, HttpResponse, Responder}; +use cached::proc_macro::once; use ramhorns::Content; #[get("/contrib")] @@ -10,6 +11,7 @@ pub async fn page(config: web::Data) -> impl Responder { #[derive(Content)] struct PortfolioTemplate {} +#[once(time = 60)] pub fn get_page(config: Config) -> std::string::String { config.tmpl.render( "contrib.html", diff --git a/src/routes/index.rs b/src/routes/index.rs index e6da094..6583647 100644 --- a/src/routes/index.rs +++ b/src/routes/index.rs @@ -1,4 +1,5 @@ use actix_web::{get, web, HttpResponse, Responder}; +use cached::proc_macro::once; use ramhorns::Content; use crate::{config::Config, template::Infos}; @@ -11,6 +12,7 @@ pub async fn page(config: web::Data) -> impl Responder { #[derive(Content)] struct IndexTemplate {} +#[once(time = 60)] pub fn get_page(config: Config) -> std::string::String { config.tmpl.render( "index.html", diff --git a/src/routes/networks.rs b/src/routes/networks.rs index cb1abaa..fd926e5 100644 --- a/src/routes/networks.rs +++ b/src/routes/networks.rs @@ -1,4 +1,5 @@ use actix_web::{get, web, HttpResponse, Responder}; +use cached::proc_macro::once; use ramhorns::Content; use crate::{config::Config, template::Infos}; @@ -11,6 +12,7 @@ pub async fn page(config: web::Data) -> impl Responder { #[derive(Content)] struct NetworksTemplate {} +#[once(time = 60)] pub fn get_page(config: Config) -> std::string::String { config.tmpl.render( "networks.html", diff --git a/src/routes/not_found.rs b/src/routes/not_found.rs index 41bb0df..051d00c 100644 --- a/src/routes/not_found.rs +++ b/src/routes/not_found.rs @@ -1,4 +1,5 @@ use actix_web::{web, HttpResponse, Responder}; +use cached::proc_macro::once; use ramhorns::Content; use crate::{config::Config, template::Infos}; @@ -10,6 +11,7 @@ pub async fn page(config: web::Data) -> impl Responder { #[derive(Content)] struct Error404Template {} +#[once(time = 60)] pub fn get_page(config: Config) -> std::string::String { config .tmpl diff --git a/src/routes/portfolio.rs b/src/routes/portfolio.rs index e39f0f6..34349d5 100644 --- a/src/routes/portfolio.rs +++ b/src/routes/portfolio.rs @@ -1,4 +1,5 @@ use actix_web::{get, web, HttpResponse, Responder}; +use cached::proc_macro::once; use glob::glob; use ramhorns::Content; @@ -19,6 +20,7 @@ struct PortfolioTemplate { univ_content: String, } +#[once(time = 60)] pub fn get_page(config: Config) -> std::string::String { let projects_dir = "data/projects"; let ext = ".md";