1.0.0 • Published 4 years ago

ioredis-conn-pool v1.0.0

Weekly downloads
1,549
License
MIT
Repository
github
Last release
4 years ago

ioredis-conn-pool

npm package

Note: A redis pool client


NPM version NPM Downloads

Table of contents


Installation

npm install --save ioredis-conn-pool

# or

cnpm install --save ioredis-conn-pool

Usage

'use strict';

const { RedisPool } = require('ioredis-conn-pool');

const pool = new RedisPool({
  redis: {
    port: 6379,          // Redis port
    host: '127.0.0.1',   // Redis host
    password: 'auth'
  },
  pool: {
    min: 2,
    max: 10
  }
});

async function todo() {
  let client;
  try {

    // get a connection for redis
    client = await pool.getConnection();

    // save something to redis
    client.set('test', 'test redis');

    // get something from redis
    const result = await client.get('test');
    console.log('saved successfully', result);

    // delete something from redis
    client.del('test');
    console.log('deleted successfully', result);

  } catch (e) {

    // caught an error
    console.error(e);

  } finally {

    // finally release redis client to pool
    if (client) {
      await pool.release(client);
      console.log('released');
    }

  }

  // close connection with redis
  process.on('SIGINT', () => {
    server.close(() => {
      pool.end()
        .catch(e => e)
        .then(() => {
          setTimeout(() => process.exit(), 500)
        });
    });
  });
}

todo();

Examples