aboutsummaryrefslogtreecommitdiff
path: root/src/modules/router
diff options
context:
space:
mode:
authorFuwn <[email protected]>2023-05-17 02:28:39 -0700
committerFuwn <[email protected]>2023-05-17 02:28:39 -0700
commitc90efc39fb626f378c7b0d9f6e45c801041124dd (patch)
tree71dd0a5e518624c42a777fda32210cd65ebe437b /src/modules/router
parentfix(route): fill for root path (diff)
downloadlocus-c90efc39fb626f378c7b0d9f6e45c801041124dd.tar.xz
locus-c90efc39fb626f378c7b0d9f6e45c801041124dd.zip
feat(router): global cache pre-route
Diffstat (limited to 'src/modules/router')
-rw-r--r--src/modules/router/module.rs41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/modules/router/module.rs b/src/modules/router/module.rs
index 76083b8..a51812b 100644
--- a/src/modules/router/module.rs
+++ b/src/modules/router/module.rs
@@ -16,8 +16,47 @@
// Copyright (C) 2022-2022 Fuwn <[email protected]>
// SPDX-License-Identifier: GPL-3.0-only
+use std::sync::{LazyLock, Mutex};
+
+use tokio::time::Instant;
use windmark::context::HookContext;
+use crate::route::{CACHE_RATE, ROUTES};
+
+fn cache(context: &windmark::context::HookContext, response: &str) {
+ static LAST_CACHED: LazyLock<Mutex<Instant>> =
+ LazyLock::new(|| Mutex::new(Instant::now()));
+
+ if (*LAST_CACHED.lock().unwrap()).elapsed()
+ >= std::time::Duration::from_secs(CACHE_RATE)
+ || (*ROUTES.lock().unwrap())
+ .get(context.url.path())
+ .is_some_and(|r| r.text_cache.is_empty())
+ {
+ (*LAST_CACHED.lock().unwrap()) = Instant::now();
+
+ if let Some(route) = (*ROUTES.lock().unwrap()).get_mut(context.url.path()) {
+ route.text_cache = response.to_string();
+ info!("cache set for {}", context.url.path());
+ } else {
+ warn!(
+ "cache could not be set for {} as it is not being tracked",
+ context.url.path()
+ );
+ }
+
+ trace!("recache for {}", {
+ let path = context.url.path();
+
+ if path.is_empty() {
+ "/"
+ } else {
+ path
+ }
+ });
+ }
+}
+
pub fn module(router: &mut windmark::router::Router) {
router.set_pre_route_callback(move |context: HookContext| {
info!(
@@ -36,6 +75,8 @@ pub fn module(router: &mut windmark::router::Router) {
context.url.to_string(),
);
+ cache(&context, &response.content);
+
if let Some(language) =
windmark::utilities::queries_from_url(&context.url).get("translate")
{