node js 操作redis promise
连接
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的更多相关文章
- 在centos7中安装redis,并通过node.js操作redis
引言 最近在学习node.js 连接redis的模块,所以尝试了一下在虚拟机中安装cent OS7,并安装redis,并使用node.js 操作redis.所以顺便做个笔记. 如有不对的地方,欢迎大家 ...
- [Node.js]操作redis
摘要 在实际开发中,免不了要操作mysql,mongodb,redis等数据存储服务器.这里先简单介绍如何操作redis. 一个例子 关于redis服务端的安装这里不再介绍,重点不在这里.感兴趣的可以 ...
- [Node.js]操作mysql
摘要 上篇文章介绍了node.js操作redis的简单实例,这里介绍如何操作mysql. 安装 安装mysql模块 cnpm install mysql 一个例子 新建一个mysql.js的文件,代码 ...
- node.js操作数据库之MongoDB+mongoose篇
前言 node.js的出现,使得用前端语法(javascript)开发后台服务成为可能,越来越多的前端因此因此接触后端,甚至转向全栈发展.后端开发少不了数据库的操作.MongoDB是一个基于分布式文件 ...
- 使用node js 操作 Mysql 数据库
使用node js 操作 Mysql 数据库 http://www.nodejs.org/ //node js 数据库操作 MySQL //使用https://github.com/felixge/n ...
- node.js零基础详细教程(7):node.js操作mongodb,及操作方法的封装
第七章 建议学习时间4小时 课程共10章 学习方式:详细阅读,并手动实现相关代码 学习目标:此教程将教会大家 安装Node.搭建服务器.express.mysql.mongodb.编写后台业务逻辑. ...
- Node.js 操作 OSX 系统麦克风、扬声器音量
最近几年 Electron 很火,公司也正好有个项目想做跨平台客户端,大家研究了一下就选择了 Electron,第一次做 js 的项目遇到了不少坑,不过也都一点点解决了. 因为项目中需要对用户录音,H ...
- Node.js 操作Mongodb
Node.js 操作Mongodb1.简介官网英文文档 https://docs.mongodb.com/manual/ 这里几乎什么都有了MongoDB is open-source docum ...
- node.js操作Cookie
node.js操作Cookie http://www.tuicool.com/articles/F3UF7n
随机推荐
- 【ABAP系列】SAP ABAP中关于commit的一点解释
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP中关于commi ...
- poj2236Wireless Network
Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have ...
- 关于public private protected访问修饰符
这个似乎都是老生常谈了,特别是找工作第一轮笔试的时候很爱考这些,再罗列一次,特别要注意继承的情况: 默认状态:即是不加修饰符的时候,所谓的default状态,在类内部可以被访问,在相同的包下面 ...
- hdu5857 Median(模拟)
Median Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Subm ...
- 【经典转载】关于Struts2的拦截器
拦截器(interceptor)是Struts2最强大的特性之一,也可以说是struts2的核心,拦截器可以让你在Action和result被执行之前或之后进行一些处理.同时,拦截器也可以让你将通用的 ...
- c#后台计算2个日期之间的天数差
/ 计算2个日期之间的天数差 DateTime dt1 = Convert.DateTime("2007-8-1"); DateTime dt2 = Convert.DateTi ...
- Js数据去重复,时间更换格式,cookie,localStorage和sessionStorage的使用等通用方法
一,数组去重复 function unique(arr) { // 遍历arr,把元素分别放入tmp数组(不存在才放) var tmp = new Array(); for (var i in arr ...
- LeetCode 852. Peak Index in a Mountain Array(C++)
Let's call an array A a mountain if the following properties hold: A.length >= 3 There exists som ...
- 获取class的儿子,报错undefined
var tds = document.getElementsByClassName("dv1")[0].children console.log(tds) 因为cla ...
- linux典型应用对系统资源使用的特点