feat: lru cache
This commit is contained in:
@@ -6,6 +6,9 @@ import mime from "mime-types";
|
||||
import { env } from "$env/dynamic/private";
|
||||
import { withCache } from "$lib/server/cache";
|
||||
|
||||
const DEFAULT_TTL = 1000 * 60 * 15; // 15min
|
||||
const EXTENDED_TTL = 1000 * 60 * 60 * 24 * 7; // 1 week
|
||||
|
||||
// Define what we are storing in the cache
|
||||
type CachedAsset = {
|
||||
buffer: ArrayBuffer;
|
||||
@@ -24,6 +27,8 @@ export const GET: RequestHandler = async ({ params, url }) => {
|
||||
// 1. Create a unique cache key for this specific asset variation
|
||||
const cacheKey = `asset:${path}?w=${width ?? ""}&h=${height ?? ""}`;
|
||||
|
||||
const ttl = path.toString().includes("cover") ? DEFAULT_TTL : EXTENDED_TTL;
|
||||
|
||||
try {
|
||||
// 2. Use withCache to wrap the fetching logic
|
||||
const cachedData = await withCache<CachedAsset>(
|
||||
@@ -113,8 +118,7 @@ export const GET: RequestHandler = async ({ params, url }) => {
|
||||
throw lastError || new Error("Asset not found");
|
||||
// --- END OF FETCH LOGIC ---
|
||||
},
|
||||
// Optional: Set a specific TTL for images (e.g., 1 hour instead of default 15m)
|
||||
1000 * 60 * 60,
|
||||
ttl,
|
||||
);
|
||||
|
||||
// 3. Construct a fresh Response using the cached data
|
||||
|
||||
Reference in New Issue
Block a user