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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@cortec/redis

v1.14.1

Published

<description>

Readme

@cortec/redis

Module Overview

@cortec/redis provides a flexible Redis integration for Node.js applications, supporting both single-node and cluster setups. It offers convenient methods for accessing Redis caches, performing health checks, and transforming objects for hash operations. The module is designed to work seamlessly with the Cortec context and configuration system.

Configuration Options

Where to put config: Place your Redis config in config/default.yml (or your environment-specific config file).

Schema:

redis:
  cache:
    connection:
      host: 'localhost'
      port: 6379
      password: 'secret' # optional
      db: 0 # optional, default: 0
    maxRetries: 5 # optional, default: 5
    encryption: false # optional, enables TLS if true
    cluster: # optional, enables Redis Cluster mode
      nodes:
        - host: 'localhost'
          port: 6379
        - host: 'localhost'
          port: 6380

Field-by-field explanation:

  • redis: Root key for Redis config.
  • cache: Identity/name for this Redis instance (can be any string, e.g. "session", "main", etc.).
  • connection: Connection options for ioredis.
    • host: Redis server hostname or IP.
    • port: Redis server port.
    • password: Password for authentication (optional).
    • db: Database index (optional, default is 0).
  • maxRetries: Maximum number of retry attempts before giving up (optional, default is 5).
  • encryption: If true, enables TLS/SSL for secure connections (optional).
  • cluster: If present, enables Redis Cluster mode.
    • nodes: List of cluster node objects, each with host and port.

How config is loaded: The config is loaded automatically by the @cortec/config module and validated at runtime. Access it in code via:

const config = ctx.provide<IConfig>('config');
const redisConfig = config?.get<any>('redis');

If config is missing or invalid, an error is thrown at startup.

Example Usage

Basic Usage

import CortecRedis from '@cortec/redis';

// Instantiate with optional object transformation for hash operations
const redisModule = new CortecRedis(true);

// After loading the context and configuration:
const cache = redisModule.cache('cache'); // 'cache' is the identity from config

// Set and get a value
await cache.set('key', 'value');
const value = await cache.get('key');
console.log(value); // 'value'

Health Check

await redisModule.healthCheck(); // Throws if any Redis instance is unreachable

Cluster Usage

If your configuration includes a cluster section, you can access the cluster instance in the same way:

const clusterCache = redisModule.cache('clusteredCache');
await clusterCache.set('key', 'value');

Advanced: Object Transformation for Hashes

If you instantiate CortecRedis with transformObjects = true, you can use objects directly with hset and get parsed objects from hgetall:

const redisModule = new CortecRedis(true);
const cache = redisModule.cache('cache');

await cache.hset('user:1', { name: 'Alice', age: 30 });
const user = await cache.hgetall('user:1');
console.log(user); // { name: 'Alice', age: 30 }

Disposal

To gracefully close all Redis connections:

await redisModule.dispose();

Testing: Using TestableCortecRedis

For integration or unit tests, you can use TestableCortecRedis to spin up a disposable Redis container using testcontainers. This allows you to run tests against a real Redis instance without needing a local or shared Redis server.

Testing

You can use TestableCortecRedis together with Cortec core to run integration tests against a real, disposable Redis instance—no manual setup required.

import Cortec from '@cortec/core';
import TestableCortecRedis from '@cortec/redis/testable';

const cortec = new Cortec({ name: 'test-app', version: '1.0.0' });
const redis = new TestableCortecRedis({ version: '7.2' }, true);

cortec.use(redis);

// After Cortec is loaded:
const cache = redis.cache('cache');
await cache.set('key', 'value');
const value = await cache.get('key');
console.log(value); // 'value'
  • The Redis container is managed automatically by Cortec during the test lifecycle.
  • No need to manually configure host/port for tests—connection details are injected.

For more details, see the implementation in src/index.ts and ensure your configuration matches your deployment needs (single-node or cluster).