cache: serve stale response
This commit is contained in:
parent
6ab3a640bf
commit
afed0a0b34
|
@ -134,7 +134,7 @@ app.get('/users/:username', actorController);
|
||||||
|
|
||||||
app.get('/nodeinfo/:version', nodeInfoSchemaController);
|
app.get('/nodeinfo/:version', nodeInfoSchemaController);
|
||||||
|
|
||||||
app.get('/api/v1/instance', cache({ expires: Time.seconds(3) }), instanceController);
|
app.get('/api/v1/instance', cache({ expires: Time.minutes(5) }), instanceController);
|
||||||
|
|
||||||
app.get('/api/v1/apps/verify_credentials', appCredentialsController);
|
app.get('/api/v1/apps/verify_credentials', appCredentialsController);
|
||||||
app.post('/api/v1/apps', createAppController);
|
app.post('/api/v1/apps', createAppController);
|
||||||
|
|
|
@ -12,19 +12,32 @@ export const cache = (opts: CacheOpts): MiddlewareHandler => {
|
||||||
let expires = Date.now() + opts.expires;
|
let expires = Date.now() + opts.expires;
|
||||||
|
|
||||||
return async (c, next) => {
|
return async (c, next) => {
|
||||||
if (!response || (Date.now() > expires)) {
|
const now = Date.now();
|
||||||
debug('Building cache for page', c.req.url);
|
const expired = now > expires;
|
||||||
expires = Date.now() + opts.expires;
|
|
||||||
|
|
||||||
|
async function updateCache() {
|
||||||
await next();
|
await next();
|
||||||
|
|
||||||
const res = c.res.clone();
|
const res = c.res.clone();
|
||||||
if (res.status < 500) {
|
if (res.status < 500) {
|
||||||
response = res;
|
response = res;
|
||||||
}
|
}
|
||||||
} else {
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (response && !expired) {
|
||||||
debug('Serving page from cache', c.req.url);
|
debug('Serving page from cache', c.req.url);
|
||||||
return response.clone();
|
return response.clone();
|
||||||
|
} else {
|
||||||
|
expires = Date.now() + opts.expires;
|
||||||
|
if (response && expired) {
|
||||||
|
debug('Serving stale cache, rebuilding', c.req.url);
|
||||||
|
const stale = response.clone();
|
||||||
|
updateCache();
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, 0));
|
||||||
|
return stale;
|
||||||
|
}
|
||||||
|
debug('Building cache for page', c.req.url);
|
||||||
|
return await updateCache();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue