app: expose cache function to koa context

This commit is contained in:
DIYgod
2018-05-18 01:31:51 +08:00
parent 1e33575315
commit c893d1ad17
2 changed files with 10 additions and 0 deletions

View File

@@ -21,6 +21,11 @@ module.exports = function(options = {}) {
max: maxLength,
});
options.app.context.cache = {
get: (key) => memoryCache.get(key),
set: (key, value, maxAge) => memoryCache.set(key, value, maxAge),
};
return async function cache(ctx, next) {
const { url, path } = ctx.request;
const resolvedPrefix = typeof prefix === 'function' ? prefix.call(ctx, ctx) : prefix;