All Projects → koajs → Koa Redis

koajs / Koa Redis

Licence: mit
Redis storage for Koa session middleware/cache with Sentinel and Cluster support

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Koa Redis

Cash
HTTP response caching for Koa. Supports Redis, in-memory store, and more!
Stars: ✭ 122 (-62.35%)
Mutual labels:  redis, cache, storage, koa, session
Redisson
Redisson - Redis Java client with features of In-Memory Data Grid. Over 50 Redis based Java objects and services: Set, Multimap, SortedSet, Map, List, Queue, Deque, Semaphore, Lock, AtomicLong, Map Reduce, Publish / Subscribe, Bloom filter, Spring Cache, Tomcat, Scheduler, JCache API, Hibernate, MyBatis, RPC, local cache ...
Stars: ✭ 17,972 (+5446.91%)
Mutual labels:  redis, cache, session
Bojack
🐴 The unreliable key-value store
Stars: ✭ 101 (-68.83%)
Mutual labels:  redis, cache, storage
DTC
DTC is a high performance Distributed Table Cache system designed by JD.com that offering hotspot data cache for databases in order to reduce pressure of database and improve QPS.
Stars: ✭ 21 (-93.52%)
Mutual labels:  storage, cache
incache
Powerful key/value in-memory storage or on disk to persist data
Stars: ✭ 16 (-95.06%)
Mutual labels:  storage, cache
pacman.store
Pacman Mirror via IPFS for ArchLinux, Endeavouros and Manjaro
Stars: ✭ 65 (-79.94%)
Mutual labels:  cache, cluster
echo-mw
统一移到hb-go/echo-web ☞
Stars: ✭ 17 (-94.75%)
Mutual labels:  cache, session
Egg Commerce
Stars: ✭ 264 (-18.52%)
Mutual labels:  redis, koa
kubernetes-marketplace
Marketplace of Kubernetes applications available for quick and easy installation in to Civo Kubernetes clusters
Stars: ✭ 136 (-58.02%)
Mutual labels:  storage, cluster
Scrapbook
PHP cache library, with adapters for e.g. Memcached, Redis, Couchbase, APC(u), SQL and additional capabilities (e.g. transactions, stampede protection) built on top.
Stars: ✭ 279 (-13.89%)
Mutual labels:  redis, cache
Senparc.co2net
支持 .NET Framework & .NET Core 的公共基础扩展库
Stars: ✭ 289 (-10.8%)
Mutual labels:  redis, cache
Juicefs
JuiceFS is a distributed POSIX file system built on top of Redis and S3.
Stars: ✭ 4,262 (+1215.43%)
Mutual labels:  redis, storage
Agile-Server
A simple, fast, complete Node.js server solution, based on KOA. 简单快速的 、性能强劲的、功能齐全的 node 服务器解决方案合集,基于 KOA。
Stars: ✭ 24 (-92.59%)
Mutual labels:  koa, session
infinitree
Scalable and encrypted embedded database with 3-tier caching
Stars: ✭ 80 (-75.31%)
Mutual labels:  storage, cache
elara
Elara DB is an easy to use, lightweight key-value database that can also be used as a fast in-memory cache. Manipulate data structures in-memory, encrypt database files and export data. 🎯
Stars: ✭ 93 (-71.3%)
Mutual labels:  storage, cache
varnish-cache-reaper
Simple python/twisted HTTP daemon forwarding PURGE and BAN requests to multiple varnish (or other proxy) instances
Stars: ✭ 12 (-96.3%)
Mutual labels:  cache, cluster
Jetcache
JetCache is a Java cache framework.
Stars: ✭ 3,167 (+877.47%)
Mutual labels:  redis, cache
Cache
Cache library
Stars: ✭ 310 (-4.32%)
Mutual labels:  redis, cache
moosefs-csi
Container Storage Interface (CSI) for MooseFS
Stars: ✭ 44 (-86.42%)
Mutual labels:  storage, cluster
storage-box
Intuitive and easy-to-use storage box.
Stars: ✭ 26 (-91.98%)
Mutual labels:  storage, cache

koa-redis

build status Coveralls David deps David devDeps license code style styled with prettier made with lass

Redis storage for Koa session middleware/cache with Sentinel and Cluster support

NPM

v4.0.0+ now uses ioredis and has support for Sentinel and Cluster!

Table of Contents

Install

npm:

npm install koa-redis

yarn:

yarn add koa-redis

Usage

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.

Basic

const session = require('koa-generic-session');
const redisStore = require('koa-redis');
const koa = require('koa');

const app = koa();
app.keys = ['keys', 'keykeys'];
app.use(session({
  store: redisStore({
    // Options specified here
  })
}));

app.use(function *() {
  switch (this.path) {
  case '/get':
    get.call(this);
    break;
  case '/remove':
    remove.call(this);
    break;
  case '/regenerate':
    yield regenerate.call(this);
    break;
  }
});

function get() {
  const session = this.session;
  session.count = session.count || 0;
  session.count++;
  this.body = session.count;
}

function remove() {
  this.session = null;
  this.body = 0;
}

function *regenerate() {
  get.call(this);
  yield this.regenerateSession();
  get.call(this);
}

app.listen(8080);

Sentinel

const session = require('koa-generic-session');
const redisStore = require('koa-redis');
const koa = require('koa');

const app = koa();
app.keys = ['keys', 'keykeys'];
app.use(session({
  store: redisStore({
    // Options specified here
    // <https://github.com/luin/ioredis#sentinel>
    sentinels: [
      { host: 'localhost', port: 26379 },
      { host: 'localhost', port: 26380 }
      // ...
    ],
    name: 'mymaster'
  })
}));

// ...

Cluster

const session = require('koa-generic-session');
const redisStore = require('koa-redis');
const koa = require('koa');

const app = koa();
app.keys = ['keys', 'keykeys'];
app.use(session({
  store: redisStore({
    // Options specified here
    // <https://github.com/luin/ioredis#cluster>
    isRedisCluster: true,
    nodes: [
      {
        port: 6380,
        host: '127.0.0.1'
      },
      {
        port: 6381,
        host: '127.0.0.1'
      }
      // ...
    ],
    // <https://github.com/luin/ioredis/blob/master/API.md#new-clusterstartupnodes-options>
    clusterOptions: {
      // ...
      redisOptions: {
        // ...
      }
    }
  })
}));

// ...

Options

  • all 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 connection
  • client (object) - supply your own client, all other options are ignored unless duplicate is also supplied
  • duplicate (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).
  • DEPRECATED: old options - auth_pass and pass have been replaced with password, and socket has been replaced with path, however all of these options are backwards compatible.

Events

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.

API

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:

const session = require('koa-generic-session');
const redisStore = require('koa-redis')({
  // Options specified here
});
const app = require('koa')();

app.keys = ['keys', 'keykeys'];
app.use(session({
  store: redisStore
}));

module(options)

Initialize the Redis connection with the optionally provided options (see above). The variable session below references this.

session.get(sid)

Generator that gets a session by ID. Returns parsed JSON is exists, null if it does not exist, and nothing upon error.

session.set(sid, sess, ttl)

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().

session.destroy(sid)

Generator that destroys a session (removes it from Redis) by ID. Tields ioredis's client.del().

session.quit()

Generator that stops a Redis session after everything in the queue has completed. Yields ioredis's client.quit().

session.end()

Alias to session.quit(). It is not safe to use the real end function, as it cuts off the queue.

session.status

String giving the connection status updated using client.status.

session.connected

Boolean giving the connection status updated using client.status after any of the events above is fired.

session.client

Direct access to the ioredis client object.

Benchmark

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

Testing

  1. Start a Redis server on localhost:6379. You can use redis-windows if you are on Windows or just want a quick VM-based server.
  2. Clone the repository and run npm i in it (Windows should work fine).
  3. If you want to see debug output, turn on the prompt's DEBUG flag.
  4. Run npm test to run the tests and generate coverage. To run the tests without generating coverage, run npm run-script test-only.

License

MIT © dead_horse

Contributors

Name Website
dead_horse
Nick Baugh http://niftylettuce.com/

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].