npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

redis-connection-manager

v1.0.1

Published

Re-use a single or pool of redis connections across several modules/files in your app. Added rejson module as well.

Downloads

8

Readme

Redis Connection Manager

A Global Redis Connection Manager that can be used anywhere in your app and closed once at the end of tests.

npm

Why?

At dwyl we use Redis everywhere because its fast!

If you're new to Redis, checkout our beginners tutorial: https://github.com/dwyl/learn-redis

Given that Redis can handle millions of operations per second, it is unlikely to be the bottleneck in your application/stack.

Where you can (unintentionally) create an issue is by having too many connections to your Redis Datastore. Don't laugh, we've seen this happen, where people open a new connection to Redis for each incoming http request (and forget to close them!) and thus quickly run out of available connections to Redis!

Most apps really only need one connection to Redis (per node.js instance) Or, if you are using Redis' Publish/Subscribe feature, you will need two connections per node.js server; one for a "standard" connection (the "Publisher"") and another as a "Subscriber".

Given that we modularise our apps and we don't want each file opening multiple connections to the Redis datastore (because Redis connections are a scarce resource - e.g: RedisCloud is 30 connections - and each connection needs to be closed for tape tests to exit...) we decided write a little script to instantiate a single connection to Redis which can be re-used across multiple files.

What?

An easy way to re-use your single Redis connection (or pair of connections - when using Redis Publish/Subscribe) across multiple files/handlers in your application and close once at the end of your tests.

How?

Install from NPM

npm install redis-connection-manager --save

Use in your code

var redisClient = require('redis-connection-manager')(); // require & connect
redisClient.set('hello', 'world');
redisClient.get('hello', function (err, reply) {
  console.log('hello', reply.toString()); // hello world
});

Using Redis Publish / Subscriber ?

You can use the standard redisClient for publishing but will need to have a separate connection to subscribe on.

Create a Subscriber connection by supplying the word subscriber when starting the redis-connection-manager:

var redisSub = require('redis-connection-manager')('subscriber');
redisSub.subscribe("chat:messages:latest", "chat:people:new");
// see: https://github.com/dwyl/hapi-socketio-redis-chat-example ;-)

Closing your Connection(s)

Closing your connections is easy.

var redisClient = require('redis-connection-manager')(); // require & connect
redisClient.set('hello', 'world');
redisClient.get('hello', function (err, reply) {
  console.log('hello', reply.toString()); // hello world
  redisClient.end(true); // this will "flush" any outstanding requests to redis
});

If you have created multiple connections in your app (you would do this to use Redis' Publish/Subscribe feature), we have a simple method to "Close All" the connections you have opened in a single command: killall()

e.g:

var redisClient = require('redis-connection-manager')(); // require & connect
var redisSub = require('redis-connection-manager')('subscriber');

// do things with redisClient and redisSub in your app...
// when you want to close both connections simply call:
require('redis-connection-manager').killall();

Using redis-connection-manager with env2

If you are using env2 to load your configuration file, simply require env2 before requiring redis-connection-manager:

require('env2')('.env'); // load the redis URL
var redisClient = require('redis-connection-manager')();
// now use your redis connection

Using rejson with redis-connection-manager

If you are using rejson module with your Redis build, you can enable rejson commands by setting the ENABLE_REJSON flag before requiring redis-connection-manager.

process.env.ENABLE_REJSON = true;
var redisClient = require('redis-connection-manager')();
// now use your redis connection

Need More?

If you need us to support a different Redis-as-a-service provider or want to have more configuration options, please let us know! Join the chat at https://gitter.im/dwyl/chat

Contributors

As with all @dwyl projects contributions welcome

Environment Variables

If you want to help improve/update/extend this module, please ask us for access to the environment variables (.env file) with REDISCLOUD_URL so you can test your modifications locally.

Failed Connection ?

If you are seeing a "Redis Connection Error" message in your terminal, e.g:

- - - - - - - - Redis Connection Error: - - - - - - - -
{ Error: 'Redis connection to 127.0.0.1:6380 failed',
  code: 'ECONNREFUSED',
  errno: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 4321 }
- - - - - - - - - - - - - - - - - - - - - - - - - - - -

Either your local instance of Redis is not running or is running on a different port from the standard which is 6379. Confirm Redis is running then try again!