连接

redis = require('redis')
var client = redis.createClient('6379', '127.0.0.1'); client.on('connect', function() {
console.log('connected');
});

基础操作

/**
* 1、字符串数据类型
*/ var res = client.set('name', 'abczhijia', (err, data) => {
console.log('err: ', err, ' data: ', data);
}); // err: null data: OK,res的值是true client.get('name', (err, data) => {
console.log('err: ', err, ' data: ', data);
}); // err: null data: abczhijia const cb = (err, data) => {
console.log('err: ', err, ' data: ', data, ' data type: ', typeof data);
} client.set('age', 20, cb); //err: null data: OK data type: string
client.get('age', cb); //err: null data: 20 data type: string /**
* 2、列表数据类型
*/ //从右侧推入
client.rpush('friends', 'mike', 'jhon', cb); //err: null data: 2 data type: number
client.lrange('friends', 0, -1, cb); //err: null data: [ 'mike', 'jhon' ] data type: object //从左侧推入
client.lpush('friends', 'sam', 'bob', cb); //err: null data: 4 data type: number
client.lrange('friends', 0, -1, cb); // err: null data: [ 'bob', 'sam', 'mike', 'jhon' ] data type: object //从右侧弹出
client.rpop('friends', cb); //err: null data: jhon data type: string
//从左侧弹出
client.lpop('friends', cb); //err: null data: bob data type: string
//打印看看发生了啥
client.lrange('friends', 0, -1, cb); // err: null data: [ 'sam', 'mike' ] data type: object //查看索引位置的值
client.lindex('friends', 0, cb); //err: null data: sam data type: string //对列表进行裁剪
client.rpush('friends', 'tom', 'bryant', cb)// err: null data: 4 data type: number
client.ltrim('friends', 1, 2, cb); //err: null data: OK data type: string
client.lrange('friends', 0, -1, cb); //err: null data: [ 'mike', 'tom' ] data type: object /**
* 3、集合数据类型
*/ //往集合ids中加几个元素
client.sadd('ids', 1, 2, cb); //err: null data: 2 data type: number
//查看集合元素
client.smembers('ids', cb); //err: null data: [ '1', '2' ] data type: object
//从集合中删除元素
client.srem('ids', 2, cb); // err: null data: 1 data type: number
//看看发生了啥
client.smembers('ids', cb); //err: null data: [ '1' ] data type: object
//看看集合有多少个元素
client.scard('ids', cb); //err: null data: 1 data type: number
//再加几个元素进去
client.sadd('ids', 3, 5, 8, 9); //
//判断元素是否在集合内
client.sismember('ids', 8, cb); // err: null data: 1 data type: number
client.sismember('ids', 80, cb); //err: null data: 0 data type: number /**
*4、散列数据类型
*/ //往散列上添加多组键值对
client.hmset('phone', 'price', 5888, 'name', 'iphonex', cb); //err: null data: OK data type: string
//查看多个键的值
client.hmget('phone', 'price', 'name', cb); //err: null data: [ '5888', 'iphonex' ] data type: object
//查看键值对的数量
client.hlen('phone', cb); //err: null data: 2 data type: number
//删掉其中一个键值对
client.hdel('phone', 'price', cb); //err: null data: 1 data type: number
//看看price是否还在?
client.hmget('phone', 'price', cb); //err: null data: [ null ] data type: object,原来只留下了null
//再加几个属性
client.hmset('phone', 'vendor', 'apple', 'madein', 'china', cb);
//取出所有的键值对
client.hgetall('phone', cb); //err: null data: { name: 'iphonex', vendor: 'apple', madein: 'china' } data type: object
//取出所有的键
client.hkeys('phone', cb); //err: null data: [ 'name', 'vendor', 'madein' ] data type: object
//取出所有的值
client.hvals('phone', cb); //err: null data: [ 'iphonex', 'apple', 'china' ] data type: object
//判断键是否存在
client.hexists('phone', 'name', cb); //err: null data: 1 data type: number
client.hexists('phone', 'price', cb); //err: null data: 0 data type: number

Native Promises

If you are using node v8 or higher, you can promisify node_redis with util.promisify as in:

const {promisify} = require('util');
const getAsync = promisify(client.get).bind(client);

now getAsync is a promisified version of client.get:

// We expect a value 'foo': 'bar' to be present
// So instead of writing client.get('foo', cb); you have to write:
return getAsync('foo').then(function(res) {
console.log(res); // => 'bar'
});

or using async await:

async myFunc() {
const res = await getAsync('foo');
console.log(res);
}

reference :

https://github.com/NodeRedis/node_redis

https://segmentfault.com/a/1190000014681783?utm_source=tag-newest

node js 操作redis promise的更多相关文章

  1. 在centos7中安装redis,并通过node.js操作redis

    引言 最近在学习node.js 连接redis的模块,所以尝试了一下在虚拟机中安装cent OS7,并安装redis,并使用node.js 操作redis.所以顺便做个笔记. 如有不对的地方,欢迎大家 ...

  2. [Node.js]操作redis

    摘要 在实际开发中,免不了要操作mysql,mongodb,redis等数据存储服务器.这里先简单介绍如何操作redis. 一个例子 关于redis服务端的安装这里不再介绍,重点不在这里.感兴趣的可以 ...

  3. [Node.js]操作mysql

    摘要 上篇文章介绍了node.js操作redis的简单实例,这里介绍如何操作mysql. 安装 安装mysql模块 cnpm install mysql 一个例子 新建一个mysql.js的文件,代码 ...

  4. node.js操作数据库之MongoDB+mongoose篇

    前言 node.js的出现,使得用前端语法(javascript)开发后台服务成为可能,越来越多的前端因此因此接触后端,甚至转向全栈发展.后端开发少不了数据库的操作.MongoDB是一个基于分布式文件 ...

  5. 使用node js 操作 Mysql 数据库

    使用node js 操作 Mysql 数据库 http://www.nodejs.org/ //node js 数据库操作 MySQL //使用https://github.com/felixge/n ...

  6. node.js零基础详细教程(7):node.js操作mongodb,及操作方法的封装

    第七章 建议学习时间4小时  课程共10章 学习方式:详细阅读,并手动实现相关代码 学习目标:此教程将教会大家 安装Node.搭建服务器.express.mysql.mongodb.编写后台业务逻辑. ...

  7. Node.js 操作 OSX 系统麦克风、扬声器音量

    最近几年 Electron 很火,公司也正好有个项目想做跨平台客户端,大家研究了一下就选择了 Electron,第一次做 js 的项目遇到了不少坑,不过也都一点点解决了. 因为项目中需要对用户录音,H ...

  8. Node.js 操作Mongodb

    Node.js 操作Mongodb1.简介官网英文文档  https://docs.mongodb.com/manual/  这里几乎什么都有了MongoDB is open-source docum ...

  9. node.js操作Cookie

    node.js操作Cookie http://www.tuicool.com/articles/F3UF7n

随机推荐

  1. 公司C++规范学习

    目录 公司C++规范学习 语法部分 风格/约定 公司C++规范学习 语法部分 class和struct关键字的选择:class表示被封装的用户自定义类型,不公开定义非静态数据成员,struct表示数据 ...

  2. 任务调度(02)Spring Schedule

    任务调度(02)Spring Schedule [toc] Spring 3.0 提供两种任务调度方式:一是定时任务调度:二是异步任务调度.这两种任务调度方式都是基于 JUC 实现的,是一种非常轻量级 ...

  3. 红玫瑰&爱情转移

  4. sshpass非交互SSH密码验证

    1.yum安装yum install sshpass -y1.1编译安装yum install wget -ywget http://sourceforge.net/projects/sshpass/ ...

  5. [fw]Linux系统使用time计算命令执行的时间

    Linux系统使用time计算命令执行的时间 当测试一个程序或比较不同算法时,执行时间是非常重要的,一个好的算法应该是用时最短的.所有类UNIX系统都包含time命令,使用这个命令可以统计时间消耗.例 ...

  6. jar包 war包

    jar包和war包的区别: war是一个web模块,其中需要包括WEB-INF,是可以直接运行的WEB模块.而jar一般只是包括一些class文件,在声明了Main_class之后是可以用java命令 ...

  7. Problems occurred when invoking code from plug-in: "org.eclipse.jface".

    java.lang.NullPointerException at com.genuitec.eclipse.easie.core.AppServer.getServerLabel(Unknown S ...

  8. String是个啥?

    String是个啥? 字符串?不可变字符串?今天想起来这个又意思的东西,所以来记录一下.我们说String是不可变字符串,那他就真的不可变吗? public class StringDemo { pu ...

  9. UIViewController push或presentViewController 弹出方式

    //导航控制器数量 add xjz 判断是push还是present出来的 NSArray *viewcontrollers = self.navigationController.viewContr ...

  10. go语言从例子开始之Example38.排序

    Go 的 sort 包实现了内置和用户自定义数据类型的排序功能.我们首先关注内置数据类型的排序. Example: package main import ( "fmt" &quo ...