go on a tangent while fixing a cache bug

This commit is contained in:
slonkazoid 2024-12-28 19:56:48 +03:00
parent 06a79057a0
commit ed74e84932
Signed by: slonk
SSH key fingerprint: SHA256:tbZfJX4IOvZ0LGWOWu5Ijo8jfMPi78TU7x1VoEeCIjM
3 changed files with 54 additions and 13 deletions

View file

@ -206,7 +206,7 @@ impl PostManager for Blag {
Ok(posts) Ok(posts)
} }
#[instrument(level = "info", skip(self))] #[instrument(skip(self))]
async fn get_post( async fn get_post(
&self, &self,
name: Arc<str>, name: Arc<str>,

View file

@ -1,3 +1,4 @@
use std::fmt::Debug;
use std::io::{Read, Write}; use std::io::{Read, Write};
use std::ops::Deref; use std::ops::Deref;
use std::sync::Arc; use std::sync::Arc;
@ -8,7 +9,7 @@ use color_eyre::eyre::{self, Context};
use scc::HashMap; use scc::HashMap;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tokio::io::AsyncReadExt; use tokio::io::AsyncReadExt;
use tracing::{debug, info, instrument}; use tracing::{debug, info, instrument, trace, Span};
/// do not persist cache if this version number changed /// do not persist cache if this version number changed
pub const CACHE_VERSION: u16 = 5; pub const CACHE_VERSION: u16 = 5;
@ -37,14 +38,20 @@ pub struct CacheKey {
} }
impl Cache { impl Cache {
#[instrument(level = "debug", skip(self), fields(entry_mtime))]
pub async fn lookup(&self, name: Arc<str>, mtime: u64, extra: u64) -> Option<CacheValue> { pub async fn lookup(&self, name: Arc<str>, mtime: u64, extra: u64) -> Option<CacheValue> {
trace!("looking up in cache");
match self.0.get_async(&CacheKey { name, extra }).await { match self.0.get_async(&CacheKey { name, extra }).await {
Some(entry) => { Some(entry) => {
let cached = entry.get(); let cached = entry.get();
Span::current().record("entry_mtime", cached.mtime);
trace!("found in cache");
if mtime <= cached.mtime { if mtime <= cached.mtime {
trace!("entry up-to-date");
Some(cached.clone()) Some(cached.clone())
} else { } else {
let _ = entry.remove(); let _ = entry.remove();
debug!("removed stale entry");
None None
} }
} }
@ -52,19 +59,24 @@ impl Cache {
} }
} }
#[instrument(level = "debug", skip(self), fields(entry_mtime))]
pub async fn lookup_metadata( pub async fn lookup_metadata(
&self, &self,
name: Arc<str>, name: Arc<str>,
mtime: u64, mtime: u64,
extra: u64, extra: u64,
) -> Option<PostMetadata> { ) -> Option<PostMetadata> {
trace!("looking up metadata in cache");
match self.0.get_async(&CacheKey { name, extra }).await { match self.0.get_async(&CacheKey { name, extra }).await {
Some(entry) => { Some(entry) => {
let cached = entry.get(); let cached = entry.get();
Span::current().record("entry_mtime", cached.mtime);
if mtime <= cached.mtime { if mtime <= cached.mtime {
trace!("entry up-to-date");
Some(cached.meta.clone()) Some(cached.meta.clone())
} else { } else {
let _ = entry.remove(); let _ = entry.remove();
debug!("removed stale entry");
None None
} }
} }
@ -72,6 +84,7 @@ impl Cache {
} }
} }
#[instrument(level = "debug", skip(self))]
pub async fn insert( pub async fn insert(
&self, &self,
name: Arc<str>, name: Arc<str>,
@ -80,7 +93,10 @@ impl Cache {
rendered: Arc<str>, rendered: Arc<str>,
extra: u64, extra: u64,
) -> Option<CacheValue> { ) -> Option<CacheValue> {
self.0 trace!("inserting into cache");
let r = self
.0
.upsert_async( .upsert_async(
CacheKey { name, extra }, CacheKey { name, extra },
CacheValue { CacheValue {
@ -89,15 +105,38 @@ impl Cache {
mtime, mtime,
}, },
) )
.await .await;
debug!(
"{} cache",
match r {
Some(_) => "updated in",
None => "inserted into",
}
);
r
} }
#[instrument(level = "debug", skip(self))]
#[allow(unused)] #[allow(unused)]
pub async fn remove(&self, name: Arc<str>, extra: u64) -> Option<(CacheKey, CacheValue)> { pub async fn remove(&self, name: Arc<str>, extra: u64) -> Option<(CacheKey, CacheValue)> {
self.0.remove_async(&CacheKey { name, extra }).await trace!("removing from cache");
let r = self.0.remove_async(&CacheKey { name, extra }).await;
debug!(
"item {} cache",
match r {
Some(_) => "removed from",
None => "did not exist in",
}
);
r
} }
#[instrument(name = "cleanup", skip_all)] #[instrument(level = "debug", name = "cleanup", skip_all)]
pub async fn retain(&self, predicate: impl Fn(&CacheKey, &CacheValue) -> bool) { pub async fn retain(&self, predicate: impl Fn(&CacheKey, &CacheValue) -> bool) {
let old_size = self.0.len(); let old_size = self.0.len();
let mut i = 0; let mut i = 0;

View file

@ -105,19 +105,21 @@ pub async fn watch_templates<'a>(
let mut templates = Vec::new(); let mut templates = Vec::new();
for event in events { for event in events {
trace!("file event: {event:?}");
if let Err(err) = process_event(event, &mut templates).await { if let Err(err) = process_event(event, &mut templates).await {
error!("error while processing event: {err}"); error!("error while processing event: {err}");
} }
} }
let mut reg = reg.write().await; if !templates.is_empty() {
for template in templates.into_iter() { let mut reg = reg.write().await;
debug!("registered template {}", template.0); for template in templates.into_iter() {
reg.register_template(&template.0, template.1); debug!("registered template {}", template.0);
reg.register_template(&template.0, template.1);
}
drop(reg);
info!("updated custom templates");
} }
drop(reg);
info!("updated custom templates");
} }
Ok(()) Ok(())