auto-claude: subtask-2-1 - Add caching to getAllBlogPosts using existing Cache
This commit is contained in:
+15
-1
@@ -1,5 +1,6 @@
|
|||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
import { cache } from '@/utils/cache';
|
||||||
|
|
||||||
export interface BlogPost {
|
export interface BlogPost {
|
||||||
slug: string;
|
slug: string;
|
||||||
@@ -130,6 +131,14 @@ function parseMarkdownPost(filename: string, content: string): BlogPost | null {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function getAllBlogPosts(): BlogPost[] {
|
export function getAllBlogPosts(): BlogPost[] {
|
||||||
|
const cacheKey = 'blog:all-posts';
|
||||||
|
|
||||||
|
// Check cache first
|
||||||
|
const cachedPosts = cache.get<BlogPost[]>(cacheKey);
|
||||||
|
if (cachedPosts) {
|
||||||
|
return cachedPosts;
|
||||||
|
}
|
||||||
|
|
||||||
if (!fs.existsSync(BLOG_POSTS_DIR)) {
|
if (!fs.existsSync(BLOG_POSTS_DIR)) {
|
||||||
console.warn('Blog posts directory not found:', BLOG_POSTS_DIR);
|
console.warn('Blog posts directory not found:', BLOG_POSTS_DIR);
|
||||||
return [];
|
return [];
|
||||||
@@ -151,7 +160,12 @@ export function getAllBlogPosts(): BlogPost[] {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Sort by date descending (newest first)
|
// Sort by date descending (newest first)
|
||||||
return posts.sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime());
|
const sortedPosts = posts.sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime());
|
||||||
|
|
||||||
|
// Cache the results
|
||||||
|
cache.set(cacheKey, sortedPosts);
|
||||||
|
|
||||||
|
return sortedPosts;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getBlogPostBySlug(slug: string): BlogPost | null {
|
export function getBlogPostBySlug(slug: string): BlogPost | null {
|
||||||
|
|||||||
Reference in New Issue
Block a user