refactor: simplify ISR cache invalidation to homepage only

This commit is contained in:
2026-01-14 12:59:31 -06:00
parent d360f2284e
commit 030680c897
3 changed files with 14 additions and 24 deletions
+7 -8
View File
@@ -261,8 +261,8 @@ mod tests {
fn test_is_cacheable_path() { fn test_is_cacheable_path() {
// Should cache // Should cache
assert!(is_cacheable_path("/")); assert!(is_cacheable_path("/"));
assert!(is_cacheable_path("/projects")); assert!(is_cacheable_path("/some-page"));
assert!(is_cacheable_path("/projects/my-project")); assert!(is_cacheable_path("/pgp"));
// Should not cache // Should not cache
assert!(!is_cacheable_path("/admin")); assert!(!is_cacheable_path("/admin"));
@@ -274,12 +274,11 @@ mod tests {
#[test] #[test]
fn test_cache_key() { fn test_cache_key() {
assert_eq!(cache_key("/projects", None), "/projects"); assert_eq!(cache_key("/", None), "/");
assert_eq!(cache_key("/projects", Some("")), "/projects"); assert_eq!(cache_key("/", Some("")), "/");
assert_eq!( assert_eq!(cache_key("/", Some("tag=rust")), "/?tag=rust");
cache_key("/projects", Some("tag=rust")), assert_eq!(cache_key("/", Some("utm_source=x")), "/?utm_source=x");
"/projects?tag=rust" assert_eq!(cache_key("/some-page", None), "/some-page");
);
} }
#[tokio::test] #[tokio::test]
+5 -14
View File
@@ -244,7 +244,7 @@ pub async fn create_project_handler(
tracing::info!(project_id = %project.id, project_name = %project.name, "Project created"); tracing::info!(project_id = %project.id, project_name = %project.name, "Project created");
// Invalidate cached pages that display projects // Invalidate cached pages that display projects
state.isr_cache.invalidate_many(&["/", "/projects"]).await; state.isr_cache.invalidate("/").await;
( (
StatusCode::CREATED, StatusCode::CREATED,
@@ -414,12 +414,7 @@ pub async fn update_project_handler(
tracing::info!(project_id = %project.id, project_name = %project.name, "Project updated"); tracing::info!(project_id = %project.id, project_name = %project.name, "Project updated");
// Invalidate cached pages that display projects // Invalidate cached pages that display projects
// Also invalidate slug-based path in case project detail pages exist state.isr_cache.invalidate("/").await;
let project_path = format!("/projects/{}", project.slug);
state
.isr_cache
.invalidate_many(&["/", "/projects", &project_path])
.await;
Json(project.to_api_admin_project(tags)).into_response() Json(project.to_api_admin_project(tags)).into_response()
} }
@@ -482,11 +477,7 @@ pub async fn delete_project_handler(
tracing::info!(project_id = %project_id, project_name = %project.name, "Project deleted"); tracing::info!(project_id = %project_id, project_name = %project.name, "Project deleted");
// Invalidate cached pages that display projects // Invalidate cached pages that display projects
let project_path = format!("/projects/{}", project.slug); state.isr_cache.invalidate("/").await;
state
.isr_cache
.invalidate_many(&["/", "/projects", &project_path])
.await;
Json(project.to_api_admin_project(tags)).into_response() Json(project.to_api_admin_project(tags)).into_response()
} }
@@ -609,7 +600,7 @@ pub async fn add_project_tag_handler(
match db::add_tag_to_project(&state.pool, project_id, tag_id).await { match db::add_tag_to_project(&state.pool, project_id, tag_id).await {
Ok(()) => { Ok(()) => {
// Invalidate cached pages - tags affect how projects are displayed // Invalidate cached pages - tags affect how projects are displayed
state.isr_cache.invalidate_many(&["/", "/projects"]).await; state.isr_cache.invalidate("/").await;
( (
StatusCode::CREATED, StatusCode::CREATED,
@@ -681,7 +672,7 @@ pub async fn remove_project_tag_handler(
match db::remove_tag_from_project(&state.pool, project_id, tag_id).await { match db::remove_tag_from_project(&state.pool, project_id, tag_id).await {
Ok(()) => { Ok(()) => {
// Invalidate cached pages - tags affect how projects are displayed // Invalidate cached pages - tags affect how projects are displayed
state.isr_cache.invalidate_many(&["/", "/projects"]).await; state.isr_cache.invalidate("/").await;
( (
StatusCode::OK, StatusCode::OK,
+2 -2
View File
@@ -80,7 +80,7 @@ pub async fn create_tag_handler(
{ {
Ok(tag) => { Ok(tag) => {
// Invalidate cached pages - tag list appears on project pages // Invalidate cached pages - tag list appears on project pages
state.isr_cache.invalidate_many(&["/", "/projects"]).await; state.isr_cache.invalidate("/").await;
(StatusCode::CREATED, Json(tag.to_api_tag())).into_response() (StatusCode::CREATED, Json(tag.to_api_tag())).into_response()
} }
@@ -226,7 +226,7 @@ pub async fn update_tag_handler(
{ {
Ok(updated_tag) => { Ok(updated_tag) => {
// Invalidate cached pages - tag updates affect project displays // Invalidate cached pages - tag updates affect project displays
state.isr_cache.invalidate_many(&["/", "/projects"]).await; state.isr_cache.invalidate("/").await;
Json(updated_tag.to_api_tag()).into_response() Json(updated_tag.to_api_tag()).into_response()
} }