Skip to content

UnKnoWn-Consortium/koa-cachier

Repository files navigation

KOA-CACHIER💲💲

A Koa Middleware backed by a Redis-powered cache store

Install

npm i koa-cachier

Usage

import Koa from "koa";
const app = new Koa();

import { Store, cachier } from "koa-cachier";

const store = new Store(); 
app.use(cachier(store));

API

cachier(store, options?);

The middleware factory accepts an options object. It is expected to contain the following properties:

cache2XXOnly?

Type: string
Default: true
Optional: Yes

When set to true, only successful response bodies are cached.
When set to false, all response bodies are cached regardless of response codes.

ignoreEmptyBody?

Type: string
Default: true
Optional: Yes

When set to true, empty response bodies are not cached.
When set to false, empty response bodies are also cached.

keyCustomizer(ctx)?

Type: Function
Default: undefined
Optional: Yes

By default, cache store keys are generated with the keyBuilder as defined in

export function keyBuilder(request: Request, ...args: string[]): string {
let key = `${ request.path }${ request.search }`;
for (const arg of args) {
if (arg) {
key += `:${ arg }`;
}
}
return key;
}

keyBuilder is a named export in koa-cachier.

You can change that by passing in your own key generator via keyCustomizer.
Koa ctx is passed to this function when called.

cachier(store, options?, postCacheMiddleware?);

postCacheMiddleware(ctx, next)?

Type: Function
Default: undefined
Optional: Yes

The middleware factory also accepts a postCacheMiddleware function as the last parameter.

If defined, it gets called with Koa ctx and next whenever a cached body is found in the store.

Combined with koa-compose, postCacheMiddleware can be leveraged to create an alternative post-cache middleware chain.

About

A Koa Middleware backed by a Redis-powered cache store

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published