1,582
社区成员




无论是什么类型的应用,都少不了和数据打交道。尤其是一些复杂的应用场景,都少不了一个高效可靠的数据库。例如日常开发中最常见的 MySQL 等关系型数据库,让数据的存储、检索轻松简单起来,甚至可以轻松地处理百万量级的数据。而从广义上讲,数据库可以分为二大类,分别是关系型数据库与非关系型数据库,上一期我们讲过的 SQLite 即为关系型数据库,而本文要讲的 Redis 则是非关系型数据库,是一个以 BSD 协议发行的、开源免费的,并具备极高性能的键值数据库。同时 Redis 也是业务应用中最为常见的“缓存”数据库。
为了方便应用开发, EdgerOS 同样引入了 Redis 的客户端模块,满足不同业务场景下对数据处理的需求。开发者可以使用改客户端方便地连接并使用 Redis 服务器。
const redis = require('redis');
EdgerOS 将 Redis 的客户端封装在了 redis 模块中,在开发中要使用时需要先引入该模块。
使用 createClient 方法来创建 Redis 客户端,调用此方法会返回一个 RedisClient 的对象;
redis.createClient([options])
options {Object} 选项
returns: {Redis} 返回一个 RedisClient 的对象
示例如下:
const redis = require('redis');
const client = redis.createClient({
host: '10.4.0.180',
port: 6379,
detect_buffers: true
});
// or
const client = redis.createClient({
host: '10.4.0.180',
port: 6379,
retry_strategy: function (options) {
if (options.error && options.error.code === 'ECONNREFUSED') {
// 发生 'ECONNREFUSED' 错误时候断开链接,并刷新所有命令
return new Error('The server refused the connection');
}
if (options.total_retry_time > 1000 * 60 * 60) {
// 发生‘重连超时’错误时候断开链接,并刷新所有命令
return new Error('Retry time exhausted');
}
if (options.attempt > 10) {
// 断开连接并抛出一个错误
return undefined;
}
// 重连之后
return Math.min(options.attempt * 100, 3000);
},
});
相关选项说明:
host:Redis 服务器的地址
port:Redis 服务器的端口
detect_buffers:回复是否使用 Buffer 选项
retry_strategy:将选项对象作为参数的一种函数
使用 hmset 方法来保存数据, 允许存储多组数据
client.hmset(hash, key1, val1, ...keyN, valN[, callback])
hash
{String} Redis 键
key
{String} 字段的键
val
{String | Buffer | Number | Date} 字段的值
callback
{Function} 回调函数
error
{Error} 错误对象
使用 hgetall 方法来读取数据
client.hgetall(hash[, callback])
hash
{String} Redis 键
callback
{Function} 回调函数
error
{Error} 错误对象
value
{Object} 获取的结果
示例如下:
const redis = require('redis');
const client = redis.createClient({ host: '10.4.0.180', port: 6379 });
client.hmset('key', 'foo', 'bar', 'hello', 'world');
client.hgetall('key', function (err, value) {
console.log(value.foo); // > 'bar'
console.log(value.hello); // > 'world'
});
订阅与发布
使用 publish 方法进行客户端消息发布
client.publish(channel, message)
channel
{String} 发布的路径
message
{String | Number | Buffer | Date} 发布的消息
使用 subscribe 方法对服务端的消息进行订阅
client.subscribe(channel)
channel
{String} 订阅的路径
示例如下:
const redis = require('redis');
const subscriber = redis.createClient({ host: '10.4.0.180', port: 6379 });
const publisher = redis.createClient({ host: '10.4.0.180', port: 6379 });
let messageCount = 0;
subscriber.on('subscribe', function (channel, count) {
publisher.publish('a channel', 'a message');
publisher.publish('a channel', 'another message');
});
subscriber.on('message', function (channel, message) {
messageCount += 1;
console.log("Subscriber received message in channel '" + channel + "': " + message);
if (messageCount === 2) {
subscriber.unsubscribe();
subscriber.quit();
publisher.quit();
}
});
subscriber.subscribe('a channel');
至此,Redis 客户端的主要功能与实现相信大家都有一个初步的认识,由于篇幅所限,不能完整详细的阐述 Redis Client 模块的所有功能,有兴趣的同学可以从我们 EdgerOS 官网获取完整信息。而我们EdgerOS 除了提供 Redis 客户端以外, 还有像非关系型数据库中的 Mysql 客户端等, 都可在官网中获取相关信息,希望本文可以对您的应用开发有所裨益。