@m92/redis

1.0.1 • Public • Published

@m92/redis

npm version  License: MIT  Vulnerabilities: Snyk  Downloads


This is an SDK Wrapper which provides Redis functionalities to interact with a Redis Instance. This package uses Node-Redis package using its v4.x.x version.

This package provides the following functionalities:

  • Redis Connection Helper
  • Redis SDK Class

Table of Content


Installation

$ npm install --save @m92/redis

Environment Variables

The following environment variables need to be set to work with this package:

##### Redis Config
export REDIS_ENABLED=false
export REDIS_AUTH_ENABLED=false
export REDIS_CHECK_SERVER_IDENTITY=false
export REDIS_HOST=
export REDIS_PORT=
export REDIS_KEY_PREFIX=
export REDIS_AUTH=

Note:

  • If 'REDIS_ENABLED' is set to 'true', 'REDIS_HOST' and 'REDIS_PORT' are required
  • If 'REDIS_AUTH_ENABLED' is set to 'true', 'REDIS_AUTH'is required
  • 'REDIS_KEY_PREFIX' prepends all redis keys with the specified value

Connecting to Redis

Redis needs to be connected before the 'RedisSdk' methods can executed. The connection can be established as shown below:

import redisClientManager from '@m92/redis/redisClientManager'
await redisClientManager.connect()

Note: Establish this connection only if you set the environment variable 'REDIS_ENABLED' value to 'true'.


Creating a Redis SDK Instance

import RedisSdk from '@m92/redis'

const redisSdk = new RedisSdk()
export default redisSdk

If you wish to pass your custom 'config' for the RedisSdk, then you can build it as follows: Note: You will have to call the redisSdk.connect() to establish a connection before using the RedisSdk Methods

import RedisSdk from '@m92/redis'

const config = {
  CONNECTION_CONFIG: {
    host: '',
    port: '',
    password: ''
  }
  KEY_PREFIX: ''
}

const redisSdk = new RedisSdk(config)
export default redisSdk

To manage redis connections for RedisSdk Instances with custom 'config', 'connect' and 'disconnect' Methods are provided and they can be called as shown below. The 'connect' method must be called before before using the RedisSdk Methods.

// To establish a connection
await redisSdk.connect()

// To release the connection
await redisSdk.disconnect()

Properties of RedisSdk Instance

Properties Description
redisSdk.CONNECTION_CONFIG Connection Config used by the SDK
redisSdk.KEY_PREFIX Redis Key Prefix used by the SDK

Methods of RedisSdk Instance

Method Description
redisSdk.get Gets the value of a redis key
redisSdk.set Sets the value for a redis key
redisSdk.getAndExpire Gets the value of a redis key and sets its expiry
redisSdk.setAndExpire Sets the value of a redis key and sets its expiry
redisSdk.del Deletes a redis key
redisSdk.keys Gets all redis keys for a given key pattern
redisSdk.delByPattern Deletes all redis keys for a given key pattern
redisSdk.incrBy Increments the value of a redis key
redisSdk.incrByAndExpire Increments the value of a redis key and sets its expiry
redisSdk.decrBy Decrements the value of a redis key
redisSdk.decrByAndExpire Decrements the value of a redis key and sets its expiry
redisSdk.exists Returns if keys exists or not

redisSdk.get(key, options)

Arguments
  • key (String): Redis key name
  • options (Object): options as mentioned by 'get' method of redis package
Returns
  • value (any): Value stored in redis
Example
const value = await redisSdk.get(key, options)

redisSdk.set(key, value, options)

Arguments
  • key (String): Redis key name
  • value (any): Value to be stored in redis
  • options (Object): options as mentioned by 'set' method of redis package
Returns
  • undefined
Example
await redisSdk.set(key, value, options)

redisSdk.getAndExpire(key, ttlInSecs, options)

Arguments
  • key (String): Redis key name
  • ttlInSecs (Number): Redis key expiry in seconds
  • options (Object): options as mentioned by 'get' method of redis package
Returns
  • value (any): Value stored in redis
Example
const value = await redisSdk.getAndExpire(key, ttlInSecs, options)

redisSdk.setAndExpire(key, value, ttlInSecs, options)

Arguments
  • key (String): Redis key name
  • value (any): Value to be stored in redis
  • ttlInSecs (Number): Redis key expiry in seconds
  • options (Object): options as mentioned by 'set' method of redis package
Returns
  • undefined
Example
await redisSdk.setAndExpire(key, value, ttlInSecs, options)

redisSdk.del(key)

Arguments
  • key (String): Redis key name
Returns
  • undefined
Example
await redisSdk.del(key)

redisSdk.keys(pattern)

Arguments
  • pattern (String): Redis key pattern
Returns
  • keys (Array): Array of keys
Example
const keys = await redisSdk.keys(pattern)

redisSdk.delByPattern(pattern)

Arguments
  • pattern (String): Redis key pattern
Returns
  • undefined
Example
await redisSdk.delByPattern(pattern)

redisSdk.incrBy(key, value)

Arguments
  • key (String): Redis key name
  • value (Number): Redis value to be incremented by (Defaults to 0)
Returns
  • incrValue (Number): Incremented redis value
Example
const incrValue = await redisSdk.incrBy(key, value)

redisSdk.incrByAndExpire(key, value, ttlInSecs)

Arguments
  • key (String): Redis key name
  • value (Number): Redis value to be incremented by (Defaults to 0)
  • ttlInSecs (Number): Redis key expiry in seconds
Returns
  • incrValue (Number): Incremented redis value
Example
const incrValue = await redisSdk.incrByAndExpire(key, value, ttlInSecs)

redisSdk.decrBy(key, value)

Arguments
  • key (String): Redis key name
  • value (Number): Redis value to be decremented by (Defaults to 0)
Returns
  • decrValue (Number): Decremented redis value
Example
const decrValue = await redisSdk.decrBy(key, value)

redisSdk.decrByAndExpire(key, value, ttlInSecs)

Arguments
  • key (String): Redis key name
  • value (Number): Redis value to be decremented by (Defaults to 0)
  • ttlInSecs (Number): Redis key expiry in seconds
Returns
  • decrValue (Number): Decremented redis value
Example
const decrValue = await redisSdk.decrByAndExpire(key, value, ttlInSecs)

redisSdk.exists(keys)

Arguments
  • keys (Array): Array of redis key names
Returns
  • keyCount (Number): Returns '0' if key does not exist and '1' if exists
Example
const keyCount = await redisSdk.exists(keys)

Resources


License



Readme

Keywords

none

Package Sidebar

Install

npm i @m92/redis

Weekly Downloads

0

Version

1.0.1

License

MIT

Unpacked Size

19 kB

Total Files

6

Last publish

Collaborators

  • ankitgandhi452
  • agarwalmehul