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
随机推荐
- 【C#学习笔记】string.Format对C#字符串格式化
文章转自:CSDN http://blog.csdn.net/samsone/article/details/7556781 1.格式化货币(跟系统的环境有关,中文系统默认格式化人民币,英文系统格 ...
- apt-get updete以及apt-get upgrade的区别
You should first run update, then upgrade. Neither of them automatically runs the other. apt-get upd ...
- Spring学习(一)--简化Java开发,认识Spring
一.传统Java开发弊端 在传统的开发之中,任何一个有实际意义的应用都会由两个或更多的类所组成,这些类之间相互协调来完成特定的业务逻辑,按照传统的做法,每个对象负责管理与自己相互协作的对象(即他所依赖 ...
- spring-第十四篇之资源访问Resource接口
1.Resource接口提供的主要方法 1>getInputStream():定位并打开资源,返回资源对应的输入流.每次调用都返回新的输入流.调用者必须负责关闭输入流. 2>isOpen( ...
- c.vim
放在 /usr/share/vim/vim80/syntax/c.vim 最后: syn match cFunctions "\<[a-zA-Z_][a-zA-Z_0-9]*\> ...
- python的小介绍
Python简介 龟叔 优美.清晰.简单 主要应用领域: 云计算 WEB开发 科学技术.人工智能 系统运维 爬虫 金融量化分析 图形GUI 游戏 Python发展史 1989年,Guido开始写Pyt ...
- Day8---Python的字典类型及操作
字典类 1.生成方法: a.介绍: 字典是键值对的集合,键值对 : 键是数据索引的扩展 b.生成方法: 使用{} 或者 dict() a = {'a' = 1, 'b' = 2, 'c' = 3 ...
- jq鼠标移入移除
ele.on({ mouseover : function(){ } , mouseout : function(){ } })
- socket - Linux 套接字
总览 #include <sys/socket.h> mysocket = socket(int socket_family, int socket_type, int protocol) ...
- K8S存储相关yaml
一.ConfigMap 1.使用目录创建 vim game.properties vim ui.properties 在一个文件夹下创建两个文件,通过以下命令创建 kubectl create con ...