Cache trending tags (with code copied from Mostr)

This commit is contained in:
Alex Gleason 2023-12-04 13:19:20 -06:00
parent caf671983b
commit 86daef416f
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
4 changed files with 115 additions and 2 deletions

View File

@ -14,6 +14,7 @@ import {
serveStatic,
} from '@/deps.ts';
import '@/firehose.ts';
import { Time } from '@/utils.ts';
import { actorController } from './controllers/activitypub/actor.ts';
import {
@ -62,6 +63,7 @@ import { nostrController } from './controllers/well-known/nostr.ts';
import { webfingerController } from './controllers/well-known/webfinger.ts';
import { auth19, requirePubkey } from './middleware/auth19.ts';
import { auth98, requireProof, requireRole } from './middleware/auth98.ts';
import { cache } from './middleware/cache.ts';
import { csp } from './middleware/csp.ts';
interface AppEnv extends HonoEnv {
@ -151,8 +153,8 @@ app.get('/api/v2/search', searchController);
app.get('/api/pleroma/frontend_configurations', frontendConfigController);
app.get('/api/v1/trends/tags', trendingTagsController);
app.get('/api/v1/trends', trendingTagsController);
app.get('/api/v1/trends/tags', cache({ cacheName: 'web', expires: Time.minutes(15) }), trendingTagsController);
app.get('/api/v1/trends', cache({ cacheName: 'web', expires: Time.minutes(15) }), trendingTagsController);
app.get('/api/v1/notifications', requirePubkey, notificationsController);
app.get('/api/v1/favourites', requirePubkey, favouritesController);

25
src/middleware/cache.ts Normal file
View 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;
}
};
};

View File

@ -0,0 +1,18 @@
import { assert } from '@/deps-test.ts';
import ExpiringCache from './expiring-cache.ts';
Deno.test('ExpiringCache', async () => {
const cache = new ExpiringCache(await caches.open('test'));
await cache.putExpiring('http://mostr.local/1', new Response('hello world'), 300);
await cache.putExpiring('http://mostr.local/2', new Response('hello world'), -1);
// const resp1 = await cache.match('http://mostr.local/1');
const resp2 = await cache.match('http://mostr.local/2');
// assert(resp1!.headers.get('Expires'));
assert(!resp2);
// await resp1!.text();
});

View File

@ -0,0 +1,68 @@
class ExpiringCache implements Cache {
#cache: Cache;
constructor(cache: Cache) {
this.#cache = cache;
}
add(request: RequestInfo | URL): Promise<void> {
return this.#cache.add(request);
}
addAll(requests: RequestInfo[]): Promise<void> {
return this.#cache.addAll(requests);
}
keys(request?: RequestInfo | URL | undefined, options?: CacheQueryOptions | undefined): Promise<readonly Request[]> {
return this.#cache.keys(request, options);
}
matchAll(
request?: RequestInfo | URL | undefined,
options?: CacheQueryOptions | undefined,
): Promise<readonly Response[]> {
return this.#cache.matchAll(request, options);
}
put(request: RequestInfo | URL, response: Response): Promise<void> {
return this.#cache.put(request, response);
}
putExpiring(request: RequestInfo | URL, response: Response, expiresIn: number): Promise<void> {
const expires = Date.now() + expiresIn;
const clone = new Response(response.body, {
status: response.status,
headers: {
expires: new Date(expires).toUTCString(),
...Object.fromEntries(response.headers.entries()),
},
});
return this.#cache.put(request, clone);
}
async match(request: RequestInfo | URL, options?: CacheQueryOptions | undefined): Promise<Response | undefined> {
const response = await this.#cache.match(request, options);
const expires = response?.headers.get('Expires');
if (response && expires) {
if (new Date(expires).getTime() > Date.now()) {
return response;
} else {
await Promise.all([
this.delete(request),
response.text(), // Prevent memory leaks
]);
}
} else if (response) {
return response;
}
}
delete(request: RequestInfo | URL, options?: CacheQueryOptions | undefined): Promise<boolean> {
return this.#cache.delete(request, options);
}
}
export default ExpiringCache;