Gathering detailed insights and metrics for koa-redis
Gathering detailed insights and metrics for koa-redis
Gathering detailed insights and metrics for koa-redis
Gathering detailed insights and metrics for koa-redis
npm install koa-redis
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
354 Stars
127 Commits
40 Forks
10 Watching
10 Branches
25 Contributors
Updated on 25 Nov 2024
Minified
Minified + Gzipped
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-35.4%
2,548
Compared to previous day
Last week
-30.3%
14,990
Compared to previous week
Last month
-12.2%
117,616
Compared to previous month
Last year
-36.5%
1,493,273
Compared to previous year
4
25
Redis storage for Koa session middleware/cache with Sentinel and Cluster support
v4.0.0+ now uses ioredis
and has support for Sentinel and Cluster!
npm:
1npm install koa-redis
yarn:
1yarn add koa-redis
koa-redis
works with koa-generic-session (a generic session middleware for koa).
For more examples, please see the examples folder of koa-generic-session
.
1const session = require('koa-generic-session'); 2const redisStore = require('koa-redis'); 3const koa = require('koa'); 4 5const app = koa(); 6app.keys = ['keys', 'keykeys']; 7app.use(session({ 8 store: redisStore({ 9 // Options specified here 10 }) 11})); 12 13app.use(function *() { 14 switch (this.path) { 15 case '/get': 16 get.call(this); 17 break; 18 case '/remove': 19 remove.call(this); 20 break; 21 case '/regenerate': 22 yield regenerate.call(this); 23 break; 24 } 25}); 26 27function get() { 28 const session = this.session; 29 session.count = session.count || 0; 30 session.count++; 31 this.body = session.count; 32} 33 34function remove() { 35 this.session = null; 36 this.body = 0; 37} 38 39function *regenerate() { 40 get.call(this); 41 yield this.regenerateSession(); 42 get.call(this); 43} 44 45app.listen(8080);
1const session = require('koa-generic-session'); 2const redisStore = require('koa-redis'); 3const koa = require('koa'); 4 5const app = koa(); 6app.keys = ['keys', 'keykeys']; 7app.use(session({ 8 store: redisStore({ 9 // Options specified here 10 // <https://github.com/luin/ioredis#sentinel> 11 sentinels: [ 12 { host: 'localhost', port: 26379 }, 13 { host: 'localhost', port: 26380 } 14 // ... 15 ], 16 name: 'mymaster' 17 }) 18})); 19 20// ...
1const session = require('koa-generic-session'); 2const redisStore = require('koa-redis'); 3const koa = require('koa'); 4 5const app = koa(); 6app.keys = ['keys', 'keykeys']; 7app.use(session({ 8 store: redisStore({ 9 // Options specified here 10 // <https://github.com/luin/ioredis#cluster> 11 isRedisCluster: true, 12 nodes: [ 13 { 14 port: 6380, 15 host: '127.0.0.1' 16 }, 17 { 18 port: 6381, 19 host: '127.0.0.1' 20 } 21 // ... 22 ], 23 // <https://github.com/luin/ioredis/blob/master/API.md#new-clusterstartupnodes-options> 24 clusterOptions: { 25 // ... 26 redisOptions: { 27 // ... 28 } 29 } 30 }) 31})); 32 33// ...
ioredis
options - Useful things include url
, host
, port
, and path
to the server. Defaults to 127.0.0.1:6379
db
(number) - will run client.select(db)
after connectionclient
(object) - supply your own client, all other options are ignored unless duplicate
is also suppliedduplicate
(boolean) - When true, it will run client.duplicate()
on the supplied client
and use all other options supplied. This is useful if you want to select a different DB for sessions but also want to base from the same client object.serialize
- Used to serialize the data that is saved into the store.unserialize
- Used to unserialize the data that is fetched from the store.isRedisCluster
(boolean) - Used for creating a Redis cluster instance per ioredis
Cluster options, if set to true
, then a new Redis cluster will be instantiated with new Redis.Cluster(options.nodes, options.clusterOptions)
(see Cluster docs for more info).nodes
(array) - Conditionally used for creating a Redis cluster instance when isRedisCluster
option is true
, this is the first argument passed to new Redis.Cluster
and contains a list of all the nodes of the cluster ou want to connect to (see Cluster docs for more info).clusterOptions
(object) - Conditionally used for created a Redi cluster instance when isRedisCluster
option is true
, this is the second argument passed to new Redis.Cluster
and contains options, such as redisOptions
(see Cluster docs for more info).auth_pass
and pass
have been replaced with password
, and socket
has been replaced with path
, however all of these options are backwards compatible.See the ioredis
docs for more info.
Note that as of v4.0.0 the disconnect
and warning
events are removed as ioredis
does not support them. The disconnect
event is deprecated, although it is still emitted when end
events are emitted.
These are some the functions that koa-generic-session
uses that you can use manually. You will need to initialize differently than the example above:
1const session = require('koa-generic-session'); 2const redisStore = require('koa-redis')({ 3 // Options specified here 4}); 5const app = require('koa')(); 6 7app.keys = ['keys', 'keykeys']; 8app.use(session({ 9 store: redisStore 10}));
Initialize the Redis connection with the optionally provided options (see above). The variable session
below references this.
Generator that gets a session by ID. Returns parsed JSON is exists, null
if it does not exist, and nothing upon error.
Generator that sets a JSON session by ID with an optional time-to-live (ttl) in milliseconds. Yields ioredis
's client.set()
or client.setex()
.
Generator that destroys a session (removes it from Redis) by ID. Tields ioredis
's client.del()
.
Generator that stops a Redis session after everything in the queue has completed. Yields ioredis
's client.quit()
.
Alias to session.quit()
. It is not safe to use the real end function, as it cuts off the queue.
String giving the connection status updated using client.status
.
Boolean giving the connection status updated using client.status
after any of the events above is fired.
Direct access to the ioredis
client object.
Server | Transaction rate | Response time |
---|---|---|
connect without session | 6763.56 trans/sec | 0.01 secs |
koa without session | 5684.75 trans/sec | 0.01 secs |
connect with session | 2759.70 trans/sec | 0.02 secs |
koa with session | 2355.38 trans/sec | 0.02 secs |
Detailed benchmark report here
localhost:6379
. You can use redis-windows
if you are on Windows or just want a quick VM-based server.npm i
in it (Windows should work fine).DEBUG
flag.npm test
to run the tests and generate coverage. To run the tests without generating coverage, run npm run-script test-only
.MIT © dead_horse
Name | Website |
---|---|
dead_horse | |
Nick Baugh | http://niftylettuce.com/ |
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
security policy file detected
Details
Reason
Found 3/18 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
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
Reason
36 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
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