WIP: Successful Google Auth, broke pretty much everything else in the process
This commit is contained in:
26
server/api/middlewares/cache.js
Normal file
26
server/api/middlewares/cache.js
Normal file
@ -0,0 +1,26 @@
|
||||
// @flow
|
||||
import debug from 'debug';
|
||||
import { type Context } from 'koa';
|
||||
|
||||
const debugCache = debug('cache');
|
||||
|
||||
export default function cache() {
|
||||
return async function cacheMiddleware(ctx: Context, next: () => Promise<*>) {
|
||||
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();
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user