reqmeister: middleware/cache, reqmeister, nip05, unfurl, refactor some code

This commit is contained in:
Alex Gleason
2023-12-27 20:07:13 -06:00
parent e121a8805e
commit 2fc9988c06
10 changed files with 39 additions and 30 deletions

View File

@@ -1,6 +1,7 @@
import { Debug, type MiddlewareHandler } from '@/deps.ts';
import ExpiringCache from '@/utils/expiring-cache.ts';
import type { MiddlewareHandler } from '@/deps.ts';
const debug = Debug('ditto:middleware:cache');
export const cache = (options: {
cacheName: string;
@@ -11,14 +12,14 @@ export const cache = (options: {
const cache = new ExpiringCache(await caches.open(options.cacheName));
const response = await cache.match(key);
if (!response) {
console.debug('Building cache for page', c.req.url);
debug('Building cache for page', c.req.url);
await next();
const response = c.res.clone();
if (response.status < 500) {
await cache.putExpiring(key, response, options.expires ?? 0);
}
} else {
console.debug('Serving page from cache', c.req.url);
debug('Serving page from cache', c.req.url);
return response;
}
};