mirror of
https://github.com/aljazceru/ditto.git
synced 2026-01-04 14:14:24 +01:00
Cache trending tags (with code copied from Mostr)
This commit is contained in:
25
src/middleware/cache.ts
Normal file
25
src/middleware/cache.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import ExpiringCache from '@/utils/expiring-cache.ts';
|
||||
|
||||
import type { MiddlewareHandler } from '@/deps.ts';
|
||||
|
||||
export const cache = (options: {
|
||||
cacheName: string;
|
||||
expires?: number;
|
||||
}): MiddlewareHandler => {
|
||||
return async (c, next) => {
|
||||
const key = c.req.url.replace('http://', 'https://');
|
||||
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);
|
||||
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);
|
||||
return response;
|
||||
}
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user