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

@es-node/redis-manager

v1.0.5

Published

An ioredis wrapper featured with redlock , NX settings to prevent race condition, and a prefix handler automatically concat and split prefixes.

Downloads

3

Readme

Redis Manager

Redis Manager is a module that provides a set of methods to interact with Redis (ioredis), a popular in-memory data store. It allows you to add, update, delete, and retrieve key-value pairs in Redis.

Installation

Since it is based on ioredis "^5.3.2", and redlock "^5.0.0-beta.2", you will need to install them:

npm install ioredis redlock

or

yarn add ioredis redlock

To install the Redis Manager module, you can use npm or yarn:

npm install @es-node/redis-manager

or

yarn add redis-manager

Usage

import { RedisManager } from 'redis-manager';
import { Redis } from 'ioredis';

// Create a Redis client instance
const redisClient = new Redis(); // Replace this with your actual Redis client instance

// Create a Redis Manager instance
const redisManager = new RedisManager({
    client: redisClient,
    expireMs: 60000, // Optional expiration time in milliseconds for keys
    namespace: 'myapp:', // The namespace prefix for keys
    maxRetries: 3, // The number of retries for failed Redis internal operations
    useRedlock: true, // Enable distributed lock management
    defaultLockTTL: 5000, // Default time-to-live (TTL) value for locks, if overtime, locks will be released.
});

// Add a key-value pair to Redis
const addResult = await redisManager.add({ key: 'mykey', value: 'myvalue', lockTTL: 2000 });
console.log(addResult); // 'OK'

// Update the value of an existing key in Redis
const updateResult = await redisManager.update({ key: 'mykey', value: 'newvalue', lockTTL: 2000 });
console.log(updateResult); // 'OK'

// Upsert a key-value pair in Redis (add if key does not exist, update if key exists)
const upsertResult = await redisManager.upsert({ key: 'mykey', value: 'upsertedvalue', lockTTL: 2000 });
console.log(upsertResult); // 'OK'

// Delete a key from Redis
const deleteResult = await redisManager.delete({ key: 'mykey', lockTTL: 2000 });
console.log(deleteResult); // true

// Check if a key exists in Redis
const hasKey = await redisManager.has('mykey');
console.log(hasKey); // false

// Retrieve the value associated with a key from Redis
const value = await redisManager.get('mykey');
console.log(value); // undefined

// Clear all keys with the specific namespace from Redis
await redisManager.clearNamespace();

// Clear all keys and their associated values from Redis
await redisManager.clearAll();

API

add({ key, value, lockTTL }) Adds a key-value pair to Redis.

  • key (string): The key associated with the value.
  • value (any): The value to be stored.
  • lockTTL (number): Optional time-to-live value for locks in milliseconds.

Returns a promise that resolves to 'OK'.

Throws a KeyExistError if the key already exists.

Throws a RedisInternalError if an error occurs during the Redis operation.

update({ key, value, lockTTL }) Updates the value of an existing key in Redis.

  • key (string): The key associated with the value.
  • value (any): The new value to be updated.
  • lockTTL (number): Optional time-to-live value for locks in milliseconds.

Returns a promise that resolves to 'OK'.

Throws a KeyNotExistError if the key does not exist.

Throws a RedisInternalError if an error occurs during the Redis operation.

upsert({ key, value, lockTTL }) Upserts a key-value pair in Redis. If the key exists, the value is updated; otherwise, a new key-value pair is added.

  • key (string): The key associated with the value.
  • value (any): The value to be stored.
  • lockTTL (number): Optional time-to-live value for locks in milliseconds.

Returns a promise that resolves to 'OK'.

Throws a RedisInternalError if an error occurs during the Redis operation.

delete(key) Deletes a key-value pair from Redis.

  • key (string): The key to delete.

Returns a promise that resolves to the number of keys deleted (0 or 1).

Throws a RedisInternalError if an error occurs during the Redis operation.

get(key) Retrieves the value associated with a key from Redis.

  • key (string): The key to retrieve the value for.

Returns a promise that resolves to the value associated with the key, or null if the key does not exist.

Throws a RedisInternalError if an error occurs during the Redis operation.

has(key) Checks if a key exists in Redis.

  • key (string): The key to check.

Returns a promise that resolves to true if the key exists, or false if it does not exist.

Throws a RedisInternalError if an error occurs during the Redis operation.

clearNamespace() Clears all keys associated with the current namespace from Redis.

Returns a promise that resolves to the number of keys deleted.

Throws a RedisInternalError if an error occurs during the Redis operation.

clearAll() Clears all keys from Redis.

Returns a promise that resolves to the number of keys deleted.

Throws a RedisInternalError if an error occurs during the Redis operation.

namespace (string): The namespace to use.

Returns the current instance of the Redis client for method chaining.

Contributing

Contributions to the Redis Manager project are welcome! If you find a bug, have suggestions for improvements, or want to add new features, feel free to open an issue or submit a pull request on the GitHub repository.

License

The Redis Manager project is licensed under the ISC License.