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

@m92/redis

v1.0.1

Published

Redis Wrapper Module

Downloads

1

Readme

@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