What is @ioredis/commands?
@ioredis/commands is an npm package that provides a comprehensive list of Redis commands for use with the ioredis library. It allows developers to interact with Redis databases by executing various commands related to data manipulation, server management, and more.
What are @ioredis/commands's main functionalities?
String Operations
This feature allows you to perform basic string operations such as setting and getting the value of a key in Redis.
const Redis = require('ioredis');
const redis = new Redis();
// Set a key-value pair
redis.set('key', 'value');
// Get the value of a key
redis.get('key', (err, result) => {
console.log(result); // 'value'
});
Hash Operations
This feature allows you to perform operations on hash data structures in Redis, such as setting and getting fields.
const Redis = require('ioredis');
const redis = new Redis();
// Set a field in a hash
redis.hset('hash', 'field', 'value');
// Get a field from a hash
redis.hget('hash', 'field', (err, result) => {
console.log(result); // 'value'
});
List Operations
This feature allows you to perform operations on list data structures in Redis, such as pushing and popping values.
const Redis = require('ioredis');
const redis = new Redis();
// Push a value to a list
redis.lpush('list', 'value');
// Pop a value from a list
redis.rpop('list', (err, result) => {
console.log(result); // 'value'
});
Set Operations
This feature allows you to perform operations on set data structures in Redis, such as adding members and checking membership.
const Redis = require('ioredis');
const redis = new Redis();
// Add a member to a set
redis.sadd('set', 'member');
// Check if a member exists in a set
redis.sismember('set', 'member', (err, result) => {
console.log(result); // 1 if member exists, 0 otherwise
});
Pub/Sub
This feature allows you to use the publish/subscribe messaging paradigm in Redis, enabling you to send and receive messages between different parts of your application.
const Redis = require('ioredis');
const redis = new Redis();
const subscriber = new Redis();
// Subscribe to a channel
subscriber.subscribe('channel');
// Listen for messages
subscriber.on('message', (channel, message) => {
console.log(`Received message: ${message} from channel: ${channel}`);
});
// Publish a message
redis.publish('channel', 'Hello, world!');
Other packages similar to @ioredis/commands
redis
The 'redis' npm package is a popular client for interacting with Redis databases. It provides a similar set of functionalities for executing Redis commands, but it is generally considered to be less feature-rich and less performant compared to ioredis.
node-redis
The 'node-redis' package is another Redis client for Node.js. It offers a straightforward API for executing Redis commands and is known for its simplicity and ease of use. However, it may lack some of the advanced features and optimizations found in ioredis.
Redis Commands
This module exports all the commands that Redis supports.
Install
$ npm install @ioredis/commands
Usage
const commands = require('@ioredis/commands');
.list
is an array contains all the lowercased commands:
commands.list.forEach((command) => {
console.log(command);
});
.exists()
is used to check if the command exists:
commands.exists('set')
commands.exists('other-command')
.hasFlag()
is used to check if the command has the flag:
commands.hasFlag('set', 'readonly')
.getKeyIndexes()
is used to get the indexes of keys in the command arguments:
commands.getKeyIndexes('set', ['key', 'value'])
commands.getKeyIndexes('mget', ['key1', 'key2'])