连接

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. python实现获取文件夹中的最新文件

    实现代码如下: #查找某目录中的最新文件import osclass FindNewFile: def find_NewFile(self,path): #获取文件夹中的所有文件 lists = os ...

  2. Recurrent Neural Network(1):Architecture

    Recurrent Neural Network是在单个神经元上,除了输入与输出外,添加了一条Recurrent回路.也就是说,节点当前的状态将会影响其未来的状态.下式可以表征此关系: st= f(s ...

  3. C#的一般处理程序中Cookie的写入、读取、清除

    1.写入Cookie值 string userName = context.Request.Form["u_Name"].ToString().Trim(); string pwd ...

  4. < python PIL - 批量图像处理 - RGB图像生成灰度图像 >

    < python PIL - 批量图像处理 - RGB图像生成灰度图像 > 直接用python自带的PIL图像库,将一个文件夹下所有jpg/png的RGB图像转换成灰度/黑白图像 from ...

  5. 02 - Jmeter4.x正则表达式以及跨线程使用变量

    话不多说 直接开撸 上图可以看出,有两个请求,其中第二个请求返回了登录超时,结合第一个登录接口来看,这个是需要header请求内容的也就是 token:当然设置一个token又怎么可能难得倒我们,无非 ...

  6. js 解决函数加载的问题

    var queue = function(funcs, scope) {         (function next() {               if(funcs.length > 0 ...

  7. spring-第十二篇之两种后处理器

    1.扩展IoC容器使用后处理器扩展 bean后处理器:对容器中的bean进行后处理,也就是额外的加强. 容器后处理:对IoC容器进行后处理,增强容器功能. 2.bean后处理器      负责处理容器 ...

  8. 21、numpy—Matplotlib

    NumPy Matplotlib Matplotlib 是 Python 的绘图库. 它可与 NumPy 一起使用,提供了一种有效的 MatLab 开源替代方案. 它也可以和图形工具包一起使用,如 P ...

  9. Saying goodbye to Flash in Chrome一代人的回忆FLASH

    一早打开chorme就推送了这条FLASH将在2020年推出CHORME 想起了当年风靡全球的flash热潮,游戏视频动画,都由flash运行,最熟悉的童年游戏4399,小时候的天堂. 说起这个不得不 ...

  10. YARN的job提交流程

    1.客户端向ResourceManagement 提交 运行的请求 (hadoop jar xxxx.jar) 2.ResourceManager进行检查,没有问题的时候,向客户端返回一个共享资源的路 ...