swap out web-time for chrono

This commit is contained in:
Evan Almloff committed 2024-02-19 08:03:29 -06:00
1 parent 1f0e03ca19
commit 6724847685
3 files changed
+19 -18

No files matched your search

Generated
+1 -1
View File
@@ -2902,6 +2902,7 @@ dependencies = [
"argh", "argh",
"askama_escape", "askama_escape",
"async-trait", "async-trait",
"chrono",
"dioxus", "dioxus",
"dioxus-core", "dioxus-core",
"dioxus-html", "dioxus-html",
@@ -2917,7 +2918,6 @@ dependencies = [
"thiserror", "thiserror",
"tokio", "tokio",
"tracing", "tracing",
"web-time",
] ]
[[package]] [[package]]
+2 -2
View File
@@ -20,7 +20,7 @@ tracing = { workspace = true }
http = "0.2.9" http = "0.2.9"
async-trait = "0.1.58" async-trait = "0.1.58"
serde_json = { version = "1.0" } serde_json = { version = "1.0" }
web-time = "1.0.0" chrono = { verison = "0.4.34", optional = true }
[target.'cfg(target_arch = "wasm32")'.dependencies] [target.'cfg(target_arch = "wasm32")'.dependencies]
tokio = { version = "1.28", features = ["io-util"], optional = true } tokio = { version = "1.28", features = ["io-util"], optional = true }
@@ -41,4 +41,4 @@ dioxus-signals = { workspace = true }
[features] [features]
default = [] default = []
incremental = ["dep:tokio"] incremental = ["dep:tokio", "chrono"]
+16 -15
View File
@@ -3,6 +3,8 @@
#![allow(non_snake_case)] #![allow(non_snake_case)]
use crate::fs_cache::ValidCachedPath; use crate::fs_cache::ValidCachedPath;
use chrono::offset::Utc;
use chrono::DateTime;
use dioxus_core::VirtualDom; use dioxus_core::VirtualDom;
use rustc_hash::FxHasher; use rustc_hash::FxHasher;
use std::{ use std::{
@@ -23,9 +25,8 @@ pub use crate::incremental_cfg::*;
pub struct IncrementalRenderer { pub struct IncrementalRenderer {
pub(crate) static_dir: PathBuf, pub(crate) static_dir: PathBuf,
#[allow(clippy::type_complexity)] #[allow(clippy::type_complexity)]
pub(crate) memory_cache: Option< pub(crate) memory_cache:
lru::LruCache<String, (web_time::SystemTime, Vec<u8>), BuildHasherDefault<FxHasher>>, Option<lru::LruCache<String, (DateTime<Utc>, Vec<u8>), BuildHasherDefault<FxHasher>>>,
>,
pub(crate) invalidate_after: Option<Duration>, pub(crate) invalidate_after: Option<Duration>,
pub(crate) ssr_renderer: crate::Renderer, pub(crate) ssr_renderer: crate::Renderer,
pub(crate) map_path: PathMapFn, pub(crate) map_path: PathMapFn,
@@ -117,7 +118,7 @@ impl IncrementalRenderer {
fn add_to_memory_cache(&mut self, route: String, html: Vec<u8>) { fn add_to_memory_cache(&mut self, route: String, html: Vec<u8>) {
if let Some(cache) = self.memory_cache.as_mut() { if let Some(cache) = self.memory_cache.as_mut() {
cache.put(route, (web_time::SystemTime::now(), html)); cache.put(route, (Utc::now(), html));
} }
} }
@@ -138,20 +139,20 @@ impl IncrementalRenderer {
.as_mut() .as_mut()
.and_then(|cache| cache.get(&route)) .and_then(|cache| cache.get(&route))
{ {
if let Ok(elapsed) = timestamp.elapsed() { let now = Utc::now();
let age = elapsed.as_secs(); let elapsed = timestamp.signed_duration_since(now);
if let Some(invalidate_after) = self.invalidate_after { let age = elapsed.num_seconds();
if elapsed < invalidate_after { if let Some(invalidate_after) = self.invalidate_after {
tracing::trace!("memory cache hit {:?}", route); if elapsed.to_std().unwrap() < invalidate_after {
output.write_all(cache_hit).await?;
let max_age = invalidate_after.as_secs();
return Ok(Some(RenderFreshness::new(age, max_age)));
}
} else {
tracing::trace!("memory cache hit {:?}", route); tracing::trace!("memory cache hit {:?}", route);
output.write_all(cache_hit).await?; output.write_all(cache_hit).await?;
return Ok(Some(RenderFreshness::new_age(age))); let max_age = invalidate_after.as_secs();
return Ok(Some(RenderFreshness::new(age as u64, max_age)));
} }
} else {
tracing::trace!("memory cache hit {:?}", route);
output.write_all(cache_hit).await?;
return Ok(Some(RenderFreshness::new_age(age as u64)));
} }
} }
// check the file cache // check the file cache