Gathering detailed insights and metrics for koa-cash
Gathering detailed insights and metrics for koa-cash
npm install koa-cash
Typescript
Module System
Min. Node Version
Node Version
NPM Version
JavaScript (100%)
Verify real, reachable, and deliverable emails with instant MX records, SMTP checks, and disposable email detection.
Total Downloads
518,537
Last Day
332
Last Week
1,293
Last Month
5,692
Last Year
98,713
MIT License
158 Stars
114 Commits
20 Forks
7 Watchers
18 Branches
23 Contributors
Updated on Feb 20, 2025
Latest Version
5.0.0
Package Id
koa-cash@5.0.0
Unpacked Size
13.89 kB
Size
5.44 kB
File Count
4
NPM Version
10.7.0
Node Version
18.20.4
Published on
Feb 13, 2025
Cumulative downloads
Total Downloads
Last Day
8.5%
332
Compared to previous day
Last Week
-13%
1,293
Compared to previous week
Last Month
32.7%
5,692
Compared to previous month
Last Year
-11.8%
98,713
Compared to previous year
HTTP response caching for Koa. Supports Redis, in-memory store, and more!
Caches the response based on any arbitrary store you'd like.
options.compression
is set to true
as of v4.0.0):tada: Pairs great with @ladjs/koa-cache-responses :tada:
1npm install koa-cash
1import LRU from 'lru-cache'; 2import koaCash from 'koa-cash'; 3 4// ... 5const cache = new LRU(); 6app.use(koaCash({ 7 get: (key) => { 8 return cache.get(key); 9 }, 10 set(key, value) { 11 return cache.set(key, value); 12 }, 13})) 14 15app.use(async ctx => { 16 // this response is already cashed if `true` is returned, 17 // so this middleware will automatically serve this response from cache 18 if (await ctx.cashed()) return; 19 20 // set the response body here, 21 // and the upstream middleware will automatically cache it 22 ctx.body = 'hello world!'; 23});
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
.
compression
If a truthy value is passed, then compression will be enabled. This value is false
by default.
setCachedHeader
If a truthy value is passed, then X-Cached-Response
header will be set as HIT
when response is served from the cache. This value is false
by default.
methods
If an object is passed, then add extra HTTP method caching. This value is empty by default. But GET
and HEAD
are enabled.
Eg: { POST: true }
hash()
A hashing function. By default, it's:
1function hash(ctx) { 2 return ctx.response.url; // same as ctx.url 3}
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.
1function get(key, maxAge) { 2 return Promise; 3}
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.
1function set(key, value, maxAge) { 2 return Promise; 3}
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.
1const koaCash = require('koa-cash'); 2const LRU = require('lru-cache'); 3 4const cache = new LRU({ 5 maxAge: 30000 // global max age 6}) 7 8app.use(koaCash({ 9 get (key, maxAge) { 10 return cache.get(key) 11 }, 12 set (key, value) { 13 cache.set(key, value) 14 } 15}))
See @ladjs/koa-cache-responses test folder more examples (e.g. Redis with ioredis
).
1const cached = await ctx.cashed(maxAge) // maxAge is passed to your caching strategy
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.
1ctx.cashClear('/')
This is a special method available on the ctx that you can use to clear the cache for a specific key.
GET
and HEAD
requests are cached. (Unless overridden)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 |
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
security policy file detected
Details
Reason
7 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 5
Reason
Found 2/14 approved changesets -- score normalized to 1
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-02-17
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More