better caching

This commit is contained in:
Michael Freno
2025-12-22 17:38:08 -05:00
parent 11fad35288
commit 541ee5b8b2
3 changed files with 133 additions and 92 deletions

View File

@@ -1,10 +1,12 @@
import { createTRPCRouter, publicProcedure } from "../utils"; import { createTRPCRouter, publicProcedure } from "../utils";
import { ConnectionFactory } from "~/server/utils"; import { ConnectionFactory } from "~/server/utils";
import { withCache } from "~/server/cache"; import { withCacheAndStale } from "~/server/cache";
const BLOG_CACHE_TTL = 24 * 60 * 60 * 1000; // 24 hours
export const blogRouter = createTRPCRouter({ export const blogRouter = createTRPCRouter({
getRecentPosts: publicProcedure.query(async () => { getRecentPosts: publicProcedure.query(async () => {
return withCache("recent-posts", 10 * 60 * 1000, async () => { return withCacheAndStale("blog-recent-posts", BLOG_CACHE_TTL, async () => {
// Get database connection // Get database connection
const conn = ConnectionFactory(); const conn = ConnectionFactory();
@@ -39,11 +41,14 @@ export const blogRouter = createTRPCRouter({
getPosts: publicProcedure.query(async ({ ctx }) => { getPosts: publicProcedure.query(async ({ ctx }) => {
const privilegeLevel = ctx.privilegeLevel; const privilegeLevel = ctx.privilegeLevel;
return withCache(`posts-${privilegeLevel}`, 5 * 60 * 1000, async () => { return withCacheAndStale(
const conn = ConnectionFactory(); `blog-posts-${privilegeLevel}`,
BLOG_CACHE_TTL,
async () => {
const conn = ConnectionFactory();
// Fetch all posts with aggregated data // Fetch all posts with aggregated data
let postsQuery = ` let postsQuery = `
SELECT SELECT
p.id, p.id,
p.title, p.title,
@@ -63,17 +68,17 @@ export const blogRouter = createTRPCRouter({
LEFT JOIN Comment c ON p.id = c.post_id LEFT JOIN Comment c ON p.id = c.post_id
`; `;
if (privilegeLevel !== "admin") { if (privilegeLevel !== "admin") {
postsQuery += ` WHERE p.published = TRUE`; postsQuery += ` WHERE p.published = TRUE`;
} }
postsQuery += ` GROUP BY p.id, p.title, p.subtitle, p.body, p.banner_photo, p.date, p.published, p.category, p.author_id, p.reads, p.attachments`; postsQuery += ` GROUP BY p.id, p.title, p.subtitle, p.body, p.banner_photo, p.date, p.published, p.category, p.author_id, p.reads, p.attachments`;
postsQuery += ` ORDER BY p.date ASC;`; postsQuery += ` ORDER BY p.date ASC;`;
const postsResult = await conn.execute(postsQuery); const postsResult = await conn.execute(postsQuery);
const posts = postsResult.rows; const posts = postsResult.rows;
const tagsQuery = ` const tagsQuery = `
SELECT t.value, t.post_id SELECT t.value, t.post_id
FROM Tag t FROM Tag t
JOIN Post p ON t.post_id = p.id JOIN Post p ON t.post_id = p.id
@@ -81,16 +86,17 @@ export const blogRouter = createTRPCRouter({
ORDER BY t.value ASC ORDER BY t.value ASC
`; `;
const tagsResult = await conn.execute(tagsQuery); const tagsResult = await conn.execute(tagsQuery);
const tags = tagsResult.rows; const tags = tagsResult.rows;
const tagMap: Record<string, number> = {}; const tagMap: Record<string, number> = {};
tags.forEach((tag: any) => { tags.forEach((tag: any) => {
const key = `${tag.value}`; const key = `${tag.value}`;
tagMap[key] = (tagMap[key] || 0) + 1; tagMap[key] = (tagMap[key] || 0) + 1;
}); });
return { posts, tags, tagMap, privilegeLevel }; return { posts, tags, tagMap, privilegeLevel };
}); }
);
}) })
}); });

View File

@@ -7,6 +7,9 @@ import { z } from "zod";
import { ConnectionFactory } from "~/server/utils"; import { ConnectionFactory } from "~/server/utils";
import { TRPCError } from "@trpc/server"; import { TRPCError } from "@trpc/server";
import { env } from "~/env/server"; import { env } from "~/env/server";
import { cache, withCacheAndStale } from "~/server/cache";
const BLOG_CACHE_TTL = 24 * 60 * 60 * 1000; // 24 hours
export const databaseRouter = createTRPCRouter({ export const databaseRouter = createTRPCRouter({
// ============================================================ // ============================================================
@@ -290,40 +293,46 @@ export const databaseRouter = createTRPCRouter({
}) })
) )
.query(async ({ input }) => { .query(async ({ input }) => {
try { return withCacheAndStale(
const conn = ConnectionFactory(); `blog-post-id-${input.id}`,
// Single query with JOIN to get post and tags in one go BLOG_CACHE_TTL,
const query = ` async () => {
SELECT p.*, t.value as tag_value try {
FROM Post p const conn = ConnectionFactory();
LEFT JOIN Tag t ON p.id = t.post_id // Single query with JOIN to get post and tags in one go
WHERE p.id = ? const query = `
`; SELECT p.*, t.value as tag_value
const results = await conn.execute({ FROM Post p
sql: query, LEFT JOIN Tag t ON p.id = t.post_id
args: [input.id] WHERE p.id = ?
}); `;
const results = await conn.execute({
sql: query,
args: [input.id]
});
if (results.rows[0]) { if (results.rows[0]) {
// Group tags by post ID // Group tags by post ID
const post = results.rows[0]; const post = results.rows[0];
const tags = results.rows const tags = results.rows
.filter((row) => row.tag_value) .filter((row) => row.tag_value)
.map((row) => row.tag_value); .map((row) => row.tag_value);
return { return {
post, post,
tags tags
}; };
} else { } else {
return { post: null, tags: [] }; return { post: null, tags: [] };
}
} catch (error) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: "Failed to fetch post by ID"
});
}
} }
} catch (error) { );
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: "Failed to fetch post by ID"
});
}
}), }),
getPostByTitle: publicProcedure getPostByTitle: publicProcedure
@@ -334,47 +343,53 @@ export const databaseRouter = createTRPCRouter({
}) })
) )
.query(async ({ input, ctx }) => { .query(async ({ input, ctx }) => {
try { return withCacheAndStale(
const conn = ConnectionFactory(); `blog-post-title-${input.title}`,
BLOG_CACHE_TTL,
async () => {
try {
const conn = ConnectionFactory();
// Get post by title with JOINs to get all related data in one query // Get post by title with JOINs to get all related data in one query
const postQuery = ` const postQuery = `
SELECT SELECT
p.*, p.*,
COUNT(DISTINCT c.id) as comment_count, COUNT(DISTINCT c.id) as comment_count,
COUNT(DISTINCT pl.user_id) as like_count, COUNT(DISTINCT pl.user_id) as like_count,
GROUP_CONCAT(t.value) as tags GROUP_CONCAT(t.value) as tags
FROM Post p FROM Post p
LEFT JOIN Comment c ON p.id = c.post_id LEFT JOIN Comment c ON p.id = c.post_id
LEFT JOIN PostLike pl ON p.id = pl.post_id LEFT JOIN PostLike pl ON p.id = pl.post_id
LEFT JOIN Tag t ON p.id = t.post_id LEFT JOIN Tag t ON p.id = t.post_id
WHERE p.title = ? AND p.category = ? AND p.published = ? WHERE p.title = ? AND p.category = ? AND p.published = ?
GROUP BY p.id GROUP BY p.id
`; `;
const postResults = await conn.execute({ const postResults = await conn.execute({
sql: postQuery, sql: postQuery,
args: [input.title, input.category, true] args: [input.title, input.category, true]
}); });
if (!postResults.rows[0]) { if (!postResults.rows[0]) {
return null; return null;
}
const postRow = postResults.rows[0];
// Return structured data with proper formatting
return {
post: postRow,
comments: [], // Comments are not included in this optimized query - would need separate call if needed
likes: [], // Likes are not included in this optimized query - would need separate call if needed
tags: postRow.tags ? postRow.tags.split(",") : []
};
} catch (error) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: "Failed to fetch post by title"
});
}
} }
);
const postRow = postResults.rows[0];
// Return structured data with proper formatting
return {
post: postRow,
comments: [], // Comments are not included in this optimized query - would need separate call if needed
likes: [], // Likes are not included in this optimized query - would need separate call if needed
tags: postRow.tags ? postRow.tags.split(",") : []
};
} catch (error) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: "Failed to fetch post by title"
});
}
}), }),
createPost: publicProcedure createPost: publicProcedure
@@ -422,6 +437,9 @@ export const databaseRouter = createTRPCRouter({
await conn.execute(tagQuery); await conn.execute(tagQuery);
} }
// Invalidate blog cache
cache.deleteByPrefix("blog-");
return { data: results.lastInsertRowid }; return { data: results.lastInsertRowid };
} catch (error) { } catch (error) {
console.error(error); console.error(error);
@@ -509,6 +527,9 @@ export const databaseRouter = createTRPCRouter({
await conn.execute(tagQuery); await conn.execute(tagQuery);
} }
// Invalidate blog cache
cache.deleteByPrefix("blog-");
return { data: results.lastInsertRowid }; return { data: results.lastInsertRowid };
} catch (error) { } catch (error) {
console.error(error); console.error(error);
@@ -549,6 +570,9 @@ export const databaseRouter = createTRPCRouter({
args: [input.id] args: [input.id]
}); });
// Invalidate blog cache
cache.deleteByPrefix("blog-");
return { success: true }; return { success: true };
} catch (error) { } catch (error) {
console.error(error); console.error(error);

View File

@@ -48,6 +48,17 @@ class SimpleCache {
delete(key: string): void { delete(key: string): void {
this.cache.delete(key); this.cache.delete(key);
} }
/**
* Delete all keys starting with a prefix
*/
deleteByPrefix(prefix: string): void {
for (const key of this.cache.keys()) {
if (key.startsWith(prefix)) {
this.cache.delete(key);
}
}
}
} }
export const cache = new SimpleCache(); export const cache = new SimpleCache();