mirror of
https://github.com/aljazceru/ditto.git
synced 2025-12-30 19:54:24 +01:00
Rework cache middleware to use in-memory cache, remove ExpiringCache module
This commit is contained in:
@@ -1,18 +0,0 @@
|
||||
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();
|
||||
});
|
||||
@@ -1,68 +0,0 @@
|
||||
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;
|
||||
Reference in New Issue
Block a user