NoSQL数据库(三)04-Redis进阶与实战——nodejs操作redis数据库之ioredis更新属于node_redis改良版 & ioredis的可视化工具安装、基本语法、管道与事务

NoSQL数据库(三)04-Redis进阶与实战——nodejs操作redis数据库之ioredis更新属于node_redis改良版 & ioredis的可视化工具安装、基本语法、管道与事务

nodejs操作redis数据库

框架选择
  • node_redis
  • ioredis

node_redis星星更多但是我们选择ioredis,因为ioredis更新,属于node_redis改良版。

【文章】ioredis 的开发背景以及与 node_redis 合并的计划

不过ioredis与node_redis的作者正在讨论将两者合为一个库。

  • ioredis安装
    • npm install ioredis
可视化工具安装
  • 收费
  • 免费:AnotherRedisDesktopManager
安装

https://github.com/mood6666/AnotherRedisDesktopManager

在这里插入图片描述

基本语法
  • 一些简单的操作
var Redis = require('ioredis');
var redis = new Redis();

redis.set('foo', 'bar');
redis.get('foo', function (err, result) {
  console.log(result);
});
redis.del('foo');

// Or using a promise if the last argument isn't a function
redis.get('foo').then(function (result) {
  console.log(result);
});

// Arguments to commands are flattened, so the following are the same:
redis.sadd('set', 1, 3, 5, 7);
redis.sadd('set', [1, 3, 5, 7]);

// All arguments are passed directly to the redis server:
redis.set('key', 100, 'EX', 10);
  • 连接redis
new Redis()       // Connect to 127.0.0.1:6379
new Redis(6380)   // 127.0.0.1:6380
new Redis(6379, '192.168.1.1')        // 192.168.1.1:6379
new Redis('/tmp/redis.sock')
new Redis({
  port: 6379,          // Redis port
  host: '127.0.0.1',   // Redis host
  family: 4,           // 4 (IPv4) or 6 (IPv6)
  password: 'auth',
  db: 0
})
  • pipelining

对redis实现的管道,避免出现前面提到的往返时延问题。

var pipeline = redis.pipeline();
pipeline.set('foo', 'bar');
pipeline.del('cc');
pipeline.exec(function (err, results) {
  // `err` is always null, and `results` is an array of responses
  // corresponding to the sequence of queued commands.
  // Each response follows the format `[err, result]`.
});

// You can even chain the commands:
redis.pipeline().set('foo', 'bar').del('cc').exec(function (err, results) {
});

// `exec` also returns a Promise:
var promise = redis.pipeline().set('foo', 'bar').get('foo').exec();
promise.then(function (result) {
  // result === [[null, 'OK'], [null, 'bar']]
});

还有另外一种调用方式:

redis.pipeline([
  ['set', 'foo', 'bar'],
  ['get', 'foo']
]).exec(function () { /* ... */ });
  • 事务
redis.multi().set('foo', 'bar').get('foo').exec(function (err, results) {
  // results === [[null, 'OK'], [null, 'bar']]
});
实例

node_index.js

var Redis = require('ioredis');
var redis = new Redis();  // 实例化了一个客户端

// redis.set('foo', 'bar');

// redis.get('foo', function(err, result) { // 第一个参数是err这是nodejs的约定
//     console.log('result', result)
// })

// // redis.del('foo');

// // redis.sadd('set', 1 , 3, 5 ,7);
// redis.sadd('set', [2, 4, 6, 8]);  // 类似apply和call

//  // 需要过期时间
//  redis.set('guoqi', 100, 'EX', 5);
// var pipeline = redis.pipeline();

// pipeline.set('hello', 'world').set('nihao', 'china').exec();

redis.multi().set('shiwu3', 'aaa').set('shiwu4', asdsad).exec();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值