Request time cache, tracking collaborators etc

This commit is contained in:
Jori Lallo
2016-08-15 12:51:26 +02:00
parent 94e39d74bf
commit 11f6c533b8
15 changed files with 141 additions and 33 deletions

View File

@ -0,0 +1,24 @@
import debug from 'debug';
const debugCache = debug('cache');
export default function cache() {
return async function cacheMiddleware(ctx, next) {
ctx.cache = {};
ctx.cache.set = async (id, value) => {
ctx.cache[id] = value;
}
ctx.cache.get = async (id, def) => {
if (ctx.cache[id]) {
debugCache(`hit: ${id}`);
} else {
debugCache(`miss: ${id}`);
ctx.cache.set(id, await def());
}
return ctx.cache[id];
};
return next();
};
}