Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
HTTP response caching for Koa. HTTP response caching for Koa. Supports Redis, in-memory store, and more!
HTTP response caching for Koa. Supports Redis, in-memory store, and more!
Caches the response based on any arbitrary store you'd like.
:tada: Pairs great with @ladjs/koa-cache-responses :tada:
npm:
npm install koa-cash
yarn:
yarn add koa-cash
const koaCash = require('koa-cash');
// ...
app.use(koaCash())
app.use(async ctx => {
// this response is already cashed if `true` is returned,
// so this middleware will automatically serve this response from cache
if (await ctx.cashed()) return;
// set the response body here,
// and the upstream middleware will automatically cache it
ctx.body = 'hello world!';
});
Options are:
maxAge
Default max age (in milliseconds) for the cache if not set via await ctx.cashed(maxAge)
.
threshold
Minimum byte size to compress response bodies. Default 1kb
.
hash()
A hashing function. By default, it's:
function hash(ctx) {
return ctx.response.url; // same as ctx.url
}
ctx
is the Koa context and is also passed as an argument. By default, it caches based on the URL.
get()
Get a value from a store. Must return a Promise, which returns the cache's value, if any.
function get(key, maxAge) {
return Promise;
}
Note that all the maxAge
stuff must be handled by you. This module makes no opinion about it.
set()
Set a value to a store. Must return a Promise.
function set(key, value, maxAge) {
return Promise;
}
Note: maxAge
is set by .cash = { maxAge }
. If it's not set, then maxAge
will be 0
, which you should then ignore.
Using a library like lru-cache, though this would not quite work since it doesn't allow per-key expiration times.
const koaCash = require('koa-cash');
const LRU = require('lru-cache');
const cache = new LRU({
maxAge: 30000 // global max age
})
app.use(koaCash({
get (key, maxAge) {
return cache.get(key)
},
set (key, value) {
cache.set(key, value)
}
}))
See @ladjs/koa-cache-responses test folder more examples (e.g. Redis with ioredis
).
This is how you enable a route to be cached. If you don't call await ctx.cashed()
, then this route will not be cached nor will it attempt to serve the request from the cache.
maxAge
is the max age passed to get()
.
If cached
is true
, then the current request has been served from cache and you should early return
. Otherwise, continue setting ctx.body=
and this will cache the response.
GET
and HEAD
requests are cached.200
responses are cached. Don't set 304
status codes on these routes - this middleware will handle it for youDate
objects as well as Buffer
objects. Otherwise, you may have to serialize/deserialize yourself.Name | Website |
---|---|
Jonathan Ong | http://jongleberry.com |
Nick Baugh | http://niftylettuce.com |
FAQs
HTTP response caching for Koa. HTTP response caching for Koa. Supports Redis, in-memory store, and more!
The npm package koa-cash receives a total of 1,115 weekly downloads. As such, koa-cash popularity was classified as popular.
We found that koa-cash demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 6 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.