2.4.0 • Published 3 months ago

@janiscommerce/redis v2.4.0

Weekly downloads
3
License
ISC
Repository
github
Last release
3 months ago

redis

Build Status Coverage Status npm version

Installation

npm install @janiscommerce/redis

Breaking changes Since 2.0.0 :warning:

  • Config host is required to connect. Connection will be ignored if not received.
  • The package now works as a wrapper of redis package for using Redis commands
  • Removed Api method set(), get(), del(), use redis commands instead
  • Now async method connect() must be executed before using any other command.

Configuration

Client configuration

Env Vars

If the env vars REDIS_WRITE_URL is set, will create a Redis connection

Config object parameter

Since 2.3.0

The connect() allows to receive an object with the url.

Settings

:warning: Deprecated :warning:

This package uses @janiscommerce/settings.

In .janiscommercerc.json file requires to have the configuration under the redis.

  • The field host is required.

See an example below

{
    "redis": {
        "host": "redis.example.host"
    }
}

Cluster Mode

The env var REDIS_CLUSTER_MODE must be set with a truthy value.

Env Vars

If the env vars REDIS_WRITE_URL and REDIS_READ_URL will be used for creating a Redis cluster connection.

Config object parameter

Since 2.3.0

The connect() allows to receive an object with the url as String or String Array.

API

connect(config = {})

async | Connects the Redis server using settings.

:new: Parameters

  • config the optional configuration.
    • config.url optional url as String for connecting the client of cluster. Since 2.3.0
    • config.url optional url as String Array for connecting. Exclusive for in cluster mode. Since 2.3.0
    • :new: config.maxRetries optional Number indicates the max amount of connection retries. (Default: 3)
      • When the max retries are reached the Client stops retrying and throws an error
      • This won't close the cached connection, the cached connection will persist and won't retry to connect.
        • If you need to set a limit of retries but retry when your process is executed again, then try catch the error and use closeConnection
        • If you don't want it to retry connection until your process is restarted, then don't need to close the connection.
    • :new: config.connectTimeout optional Number indicates the connection timeout in miliseconds. (Default: 5000)

Return

  • client: The Redis client when host is present in settings.
  • undefined: When host is not present in settings.

Throw an Error if Redis Server fails.

closeConnection()

async | Closes the active connection.

Usage

const Redis = require('@janiscommerce/redis');

(async () => {

    const redisCluster = await Redis.connect();

    await redisCluster.set('product-123', 'blue-shirt');

    const value = await redisCluster.get('product-123');

    // expected value: blue-shirt

})();

// Usage with custom max retries
(async () => {

    try {

        const redisCluster = await Redis.connect({
            connectTimeout: 1000,
            maxRetries: 1
        });

    }catch(err) {
        console.log(err.message);
        await Redis.closeConnection();
    }
})();

Errors

The errors are informed with a RedisError. This object has a code that can be useful for a debugging or error handling. The codes are the following:

CodeDescription
1Redis error
2Max connection retries reached

:information_source: For more examples see redis

2.4.0

3 months ago

2.3.0

10 months ago

2.2.0

10 months ago

2.1.0

11 months ago

2.0.0

12 months ago

1.1.0

4 years ago

1.0.0

5 years ago