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-client-cache

v2.1.1

Published

A simple cache of RedisClient instances for use with AWS Lambdas.

Downloads

6

Readme

redis-client-cache v2.1.1

A simple module-scope cache of RedisClient instances by host and port (primarily for AWS Lambda use).

NB: This module depends on the external redis module and caches that module's RedisClient instances.

Main module:

  • redis-client-cache.js

This module is exported as a Node.js module.

Installation

Using npm:

$ npm i --save redis-client-cache

Usage

  • To use the RedisClient cache to set and get a previously or newly cached, "raw" (untested) RedisClient instance per host-port combination
const RedisClientCache = require('redis-client-cache');

// Choose a Redis adapter to use - either 'rcc-redis-adapter' or 'rcc-ioredis-adapter' 
// For unit testing, choose either 'rcc-redis-mock-adapter' or 'rcc-ioredis-mock-adapter'
const redisAdapter = require('rcc-redis-adapter');

// Preamble to create a context and configure logging on the context
let context = {};
const logging = require('logging-utils');
context = logging.configureLogging(context); // or your own custom logging configuration (see logging-utils README.md)

// NB: Configure the redis client cache with an appropriate Redis adapter to use
context = RedisClientCache.configureRedisClientCache(context, redisAdapter);
const redisClientCache = context.redisClientCache;
assert(redisClientCache);

const host = '127.0.0.1'; // ... replace with your redis server's host
const port = 6379; // ... replace with your redis server's port


// Define the redis client constructor options that you want to use, e.g.
const redisClientOptions = {
  host: host,
  port: port,
  string_numbers: true // ... `redis` adapter example ... not valid for `ioredis`
  // See https://www.npmjs.com/package/redis#options-object-properties for full details for `redis` adapter
  // ...
};

// To create and cache a new RedisClient instance with the given RedisClient constructor options for either the default 
// host and port or for the host and port specified in the given options OR to reuse a previously cached RedisClient 
// instance (if any) that is compatible with the given options
const redisClient = redisClientCache.setRedisClient(redisClientOptions, context);
assert(redisClient);

// To get a previously set or configured RedisClient instance for a specified host and port
const redisClient1 = redisClientCache.getRedisClientAndReplaceIfClosing('localhost', 9999, context);
assert(redisClient1);

// ... or, less useful, for the DEFAULT host and port
const redisClient2 = redisClientCache.getRedisClient(redis.defaultHost, redis.defaultPort);
assert(redisClient2);

// To get the original options that were used to construct a cached RedisClient instance for a specified host and port
const optionsUsed1 = redisClientCache.getRedisClientOptionsUsed('localhost', 9999);
assert(optionsUsed1);

// ... or, less useful, for the DEFAULT host and port
const optionsUsed2 = redisClientCache.getRedisClientOptionsUsed(redis.defaultHost, redis.defaultPort);
assert(optionsUsed2);

// To remove (and also end/quit) a cached RedisClient instance from the cache
const {host1, port1, deleted, disconnected} = redisClientCache.deleteAndDisconnectRedisClient('localhost', 9999, context);
assert(host1 && port1 && deleted && disconnected);

// To asynchronously test connectivity of a RedisClient instance
redisClientCache.isRedisClientUsable(redisClient, context).then(usable => {
  // usable will be true if the async connectivity test worked; otherwise false
  assert(usable === true);
  // ...
});

// To simultaneously test connectivity of a RedisClient instance and then EITHER return it (if the test passed)
// OR return a brand new instance (if the test failed)
redisClientCache.replaceRedisClientIfUnusable(redisClient, redisClientOptions, context).then(client => {
  assert(client);
  // ...
});
  • To use the RedisClient cache to set and get a previously or newly cached, tested and USABLE RedisClient instance (if the connectivity test passed) or a brand new instance (if the connectivity test failed) per host-port combination. Note that this function returns a promise, since it performs an asynchronous connectivity test against the Redis server (and also suffers the overhead of doing so).
const RedisClientCache = require('redis-client-cache');

// Choose a Redis adapter to use - either 'rcc-redis-adapter' or 'rcc-ioredis-adapter' 
// For unit testing, choose either 'rcc-redis-mock-adapter' or 'rcc-ioredis-mock-adapter'
const redisAdapter = require('rcc-ioredis-adapter');

// Preamble to create a context and configure logging on the context
let context = {};
const logging = require('logging-utils');
context = logging.configureLogging(context); // or your own custom logging configuration (see logging-utils README.md)

// NB: Configure the redis client cache with an appropriate Redis adapter to use
context = RedisClientCache.configureRedisClientCache(context, redisAdapter);
const redisClientCache = context.redisClientCache;
assert(redisClientCache);

const host = '127.0.0.1'; // ... your redis server's host
const port = 6379; // ... your redis server's port

// Define the RedisClient constructor options that you want to use, e.g.
const redisClientOptions = {
  host: host,
  port: port,
  string_numbers: true
  // See https://www.npmjs.com/package/redis#options-object-properties for full details (if using `rcc-redis-adapter`) 
  // ...
};

// To set a new "tested" and USABLE RedisClient instance with the given RedisClient constructor options for either the 
// default host and port or for the host and port specified in the given options OR to reuse a previously cached, 
// tested and USABLE RedisClient instance (if any) that is compatible with the given options
const redisClientPromise = redisClientCache.setRedisClientAndReplaceIfUnusable(redisClientOptions, context);
// ...
redisClientPromise.then(client => {
  assert(client);
  // ...  
});

// To later retrieve the cached RedisClient instance
const client = redisClientCache.getRedisClient(host, port);
assert(client);

Unit tests

This module's unit tests were developed with and must be run with tape. The unit tests have been tested on Node.js v6.10.3.

To run the unit tests - against redis-mock and ioredis-mock:

npm test

To run the integration tests - against redis and ioredis:

npm run itest

See the package source for more details.

Changes

See CHANGES.md