【转】Redis 客户端库 ioredis 的使用详解 - 技术宅银魂 - 科技改变生活 - 万事屋

【转】Redis 客户端库 ioredis 的使用详解

ioredis 是一款高性能的 Node.js Redis 客户端库,兼容 Redis 3.0 版本以上的所有功能。ioredis 的 API 设计简单易用,支持多个 Redis 数据库的操作。

本文将介绍 ioredis 的安装与配置、连接 Redis 数据库、设置键值对、列表、哈希表、集合、有序集合等数据结构及其操作、订阅与发布等常用操作。

安装与配置

使用 npm 安装 ioredis:

npm install ioredis

ioredis 支持连接 Redis 单个实例、Redis 集群、Redis Sentinel 等多种模式。连接 Redis 单个实例的配置如下:

const Redis = require("ioredis");

const redis = new Redis({
  host: "localhost",
  port: 6379,
  password: "password",
  db: 0,
});

// 连接成功后执行的回调函数
redis.on("connect", () => {
  console.log("Redis connected!");
});

// 连接出错时执行的回调函数
redis.on("error", (error) => {
  console.error("Redis error: ", error);
});

其中 host 和 port 分别表示 Redis 的主机名和端口号,password 是连接 Redis 的密码,db 是指定的数据库编号。如果省略 password,则默认为 null。Redis 的默认端口号为 6379,数据库编号从 0 开始。

设置键值对

ioredis 提供了设置键值对的方法 set 和 get:

redis.set("foo", "bar"); // 将键 foo 对应的值设置为 bar
redis.get("foo").then((result) => {
  console.log(result); // 输出 'bar'
});

其中 set 与 get 方法中的第一个参数表示键名,第二个参数表示键值。get 方法返回一个 Promise,可以通过 then 方法获取查询结果。

列表

ioredis 提供了列表的操作方法 lpush、lpop、rpush、rpop、llen、lrange 和 lrem:

// 在列表头部插入元素
redis.lpush("mylist", "one", "two", "three");

// 在列表尾部插入元素
redis.rpush("mylist", "four", "five", "six");

// 从列表头部弹出元素
redis.lpop("mylist").then((result) => {
  console.log(result); // 输出 'three'
});

// 从列表尾部弹出元素
redis.rpop("mylist").then((result) => {
  console.log(result); // 输出 'six'
});

// 获取列表长度
redis.llen("mylist").then((result) => {
  console.log(result); // 输出 '4'
});

// 获取列表中的元素
redis.lrange("mylist", 0, -1).then((result) => {
  console.log(result); // 输出 ['three','two','one','four','five']
});

// 删除列表中的元素
redis.lrem("mylist", 1, "one").then((result) => {
  console.log(result); // 输出 '1'
});

其中 lrange 方法用于获取指定范围内的元素,第二个参数和第三个参数分别表示开始和结束索引,如果第三个参数为 -1,则表示获取所有元素。

哈希表

ioredis 提供了哈希表的操作方法 hset、hget、hmset、hmget、hlen、hdel 和 hkeys:

// 设置哈希表中的值
redis.hset("myhash", "field1", "value1");

// 获取哈希表中的值
redis.hget("myhash", "field1").then((result) => {
  console.log(result); // 输出 'value1'
});

// 批量设置哈希表中的值
redis.hmset("myhash", "field2", "value2", "field3", "value3");

// 批量获取哈希表中的值
redis.hmget("myhash", "field1", "field2", "field3").then((result) => {
  console.log(result); // 输出 ['value1', 'value2', 'value3']
});

// 获取哈希表中的字段数量
redis.hlen("myhash").then((result) => {
  console.log(result); // 输出 '3'
});

// 删除哈希表中的字段
redis.hdel("myhash", "field1").then((result) => {
  console.log(result); // 输出 '1'
});

// 获取哈希表中的所有字段名
redis.hkeys("myhash").then((result) => {
  console.log(result); // 输出 ['field2', 'field3']
});

集合

ioredis 提供了集合的操作方法 sadd、spop、scard 和 smembers:

// 向集合中添加元素
redis.sadd("myset", "one", "two", "three");

// 从集合中弹出元素
redis.spop("myset").then((result) => {
  console.log(result); // 输出 'three'
});

// 获取集合中的元素数量
redis.scard("myset").then((result) => {
  console.log(result); // 输出 '2'
});

// 获取集合中的所有元素
redis.smembers("myset").then((result) => {
  console.log(result); // 输出 ['one', 'two']
});

有序集合

ioredis 提供了有序集合的操作方法 zadd、zrange、zrevrange、zrank、zrevrank 和 zrem:

// 向有序集合中添加元素
redis.zadd("myzset", 1, "one", 2, "two", 3, "three");

// 获取有序集合中指定范围内的元素
redis.zrange("myzset", 0, -1).then((result) => {
  console.log(result); // 输出 ['one', 'two', 'three']
});

// 获取有序集合中指定范围内的元素,按照分值从大到小排序
redis.zrevrange("myzset", 0, -1).then((result) => {
  console.log(result); // 输出 ['three', 'two', 'one']
});

// 获取有序集合中指定元素的排名
redis.zrank("myzset", "one").then((result) => {
  console.log(result); // 输出 '0'
});

// 获取有序集合中指定元素的排名,按照分值从大到小排序
redis.zrevrank("myzset", "one").then((result) => {
  console.log(result); // 输出 '2'
});

// 删除有序集合中的元素
redis.zrem("myzset", "one").then((result) => {
  console.log(result); // 输出 '1'
});

订阅与发布

ioredis 支持 Redis 的发布与订阅功能,可以通过 pub、sub 和 unsubscribe 方法实现:

const publisher = new Redis({
  host: "localhost",
  port: 6379,
});

const subscriber = new Redis({
  host: "localhost",
  port: 6379,
});

// 订阅消息
subscriber.subscribe("testChannel");

// 发布消息
publisher.publish("testChannel", "hello world");

// 收到消息时执行的回调函数
subscriber.on("message", (channel, message) => {
  console.log(channel, message); // 输出 'testChannel', 'hello world'
});

// 在 5 秒后取消订阅消息
setTimeout(() => {
  subscriber.unsubscribe("testChannel");
}, 5000);

其中 subscriber.subscribe 方法用于订阅消息,publisher.publish 方法用于发布消息,subscriber.on 方法用于绑定 message 事件处理函数,当订阅的消息到达后自动执行。unsubscribe 方法用于取消订阅消息。

本文转自:阅读原文

    请登录后查看回复内容

万事屋新帖