// node.js访问redis前提条件,安装redis模块
// -- npm install redis
//导入redis模块
var redis = require('redis');
//新建连接 redis.createClient(端口,ip地址0); 端口参数可以是数值也可以是字符串
var client = redis.createClient(6379, 'ip地址');
//如果有密码,请认证
client.auth("密码");//如果想要选择其他库,例如3
/*** 说明:*      1.Redis不支持自定义数据库的名字;*      2.Redis也不支持为每个数据库设置不同的访问密码;*      3.多个数据库之间并不是完全隔离的,比如FLUSHALL命令可以清空全部数据库的数据;*      4.数据库更像是一种命名空间,而不适宜存储不同应用程序的数据,不同的应用应该使用不同的Redis实例存储数据;*      5.一个空Redis实例占用的内在只有1M左右,所以不用担心多个Redis实例会额外占用很多内存。*/
client.select(3,function(error){if(error) {console.log(error)};console.log('已切换到数据库3');
})
//一下操作都是在数据库3下执行
// client.set('测试',"在数据库3中插入数据",function(error,info){
//     if(error) return error;
//     if(info === 'OK'){
//         console.log('插入成功')
//     }
// })
// client.get('测试',function(error,data){
//     if(error) return error;
//     console.log(data);
// })
// client.get('http://spgz.com/162304',function(error,data){
//     if(error) return error;
//     console.log(data);
// })//切换为数据库1
client.select(1,function(error,data){if(error) return error;console.log('已切换到数据库1');
})//-------------------------String 操作--------------------------//
// set 设置存储在给定键中的值 返回值'OK'
// client.set("string key", 20,function(error,info){
//     if(error){
//         console.log(error);
//     }
//     //"=="相等,但是不严格(字符串'456'和数字456相等);"===":严格相等
//     if(info == 'OK'){
//         console.log('在数据库1中set成功')
//     }
// });
// get 获取存储在给定键中的值 返回值 value或空
// client.get('string key',function(error,data){
//     if(error){
//         console.log(error);
//     }
//     console.log(data);
// })
// del 删除存储在给定键中的值(任意类型) 返回值 0或1 0:库中没有要删除的数据;1:删除成功
// client.del('list',function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// });
// incrby 将键存储的值加上整数,返回值 value最后的值,key中的值需要为整数。不然会报错
// client.incrby('string key',10,function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info)
//     client.get('string key',function(error,data){
//         console.log(data);
//     });
// })
// decrby 将键存储的值减去整数// incrbyfloat 将键存储的值加上浮点数// append  将值value追加到给定键当前存储值的末尾 返回值 value最后的值 ,这个操作是单纯的拼接,例如整数30 拼 80 等于 3080
// client.append('string key',80,function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// })// getrange 获取指定键的index范围内的所有字符组成的子串 返回值 指定范围的子串
// client.getrange('string key',0,2,function(error,data){
//     if(error){
//         console.log(error);
//     }
//     console.log(data);
// })// setrange 将指定键值从指定偏移量开始的子串设为指定值 ,如果指定的key不存在,其效果与set类似,整数会转为字符串
// client.setrange('string key1',0,0,function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// })//--------------------------list 操作---------------------------//
// rpush 将给定值推入列表的右端 返回值 当前列表长度
// client.rpush("list",['test1','test2','test3','test4','test5'],function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// })// lrange 获取列表在给定范围上的所有值 返回值 当前所有值 如果超出范围则返回所有,不报错。
// client.lrange('list',0,1000,function(error,data){
//     if(error){
//         console.log(error);
//     }
//     console.log(data);
// })// lindex 获取列表在给定位置上的单个元素// lpop 从列表左端弹出一个值,并返回被弹出的值// rpop 从列表右端弹出一个值,并返回被弹出的值// 将列表按指定的index范围裁减, 返回值 OK ,只保留指定范围内的数据
// client.ltrim('list',10,15,function(error,data){
//     if(error){
//         console.log(error);
//     }
//     console.log(data);
// });//---------------------------set 操作---------------------------//
// 先清空集合,方便测试
// client.del('setTest',function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log('清空集合');
// })// sadd 将给定元素添加到集合 返回值 插入元素数量
// client.sadd('setTest',['setTest1','setTest2','setTest3','setTest4'],function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// });// smembers 返回集合中包含的所有元素 返回值 array(无序),插入和读出的顺序不一定一致
// client.smembers('setTest',function(error,data){
//     if(error){
//         console.log(error);
//     }
//     console.log(data);
// });// sismenber 检查给定的元素是否存在于集合中 返回值 0/1 0:不存在,1:存在
// client.sismember('setTest','setTest3',function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// })// srem 如果给定的元素在集合中,则移除此元素 返回值 0/1 0:没有匹配上,1:匹配上并删除
// client.srem('setTest','setTest37',function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// })// scad 返回集合包含的元素的数量 返回值 元素的数量
// client.scard('setTest',function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// })// spop 随机地移除集合中的一个元素,并返回此元素 返回值 被删除的元素
// client.spop('setTest',function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// })// smove 将指定成员 member 元素从 source 集合移动到 destination 集合 返回值 0/1 0:元素不是 source 集合的成员,并且没有任何操作对 destination 集合执行;1:如果成员元素被成功移除,返回 1
// client.smove('setTest','setTest01','setTest4',function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// })// sdiff 返回那些存在于第一个集合,但不存在于其他集合的元素(差集)
// client.sdiff('setTest','setTest01',function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info)
// })// sdiffstore 将sdiff操作的结果存储到指定的键中 第一个参数是存储到的指定集合
// client.sdiffstore('setTest02','setTest','setTest01',function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// })// sinter 返回那些同时存在于所有集合中的元素(交集)
// client.sinter('setTest','setTest01',function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// })// sinterstore 将sinter操作的结果存储到指定的键中
// client.sinterstore('setTest03','setTest','setTest01',function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// })// sunion 返回那些至少存在于一个集合中的元素(并集)
// client.sunion('setTest','setTest01',function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// })// sunionstore 将sunion操作的结果存储到指定的键中
// client.sunionstore('setTest04','setTest','setTest01',function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// })//---------------------------hash 操作---------------------------//
// hset 在散列里面关联起给定的键值对 1(新增)/0(更新) 返回结果为0说明已存在相同 key 的值
// 以下两种写法效果想同
// client.hset("hash key", "hashtest 3", "some value", redis.print);
// client.hset(["hash key", "hashtest 2", "some other value"], redis.print);// hget 获取指定散列键的值,需要指定到里面一层,不然报错
// client.hget("hash key","hashtest 3",function(error,data){
//     if(error){
//         console.log(error);
//     }
//     console.log(data);
// })// hgetall 获取散列包含的键值对 返回值 hash键里面的所有键值对,格式为json;
// client.hgetall("hash key",function(error,data){
//     if(error){
//         console.log(error);
//     }
//     console.log(data);
// })// hdel 如果给定键 存在于散列里面,则移除这个键 返回值 0/1 0:没有存在 1:已移除
// client.hdel("hash key","hashtest 3",function(error,data){
//     if(error){
//         console.log(error);
//     }
//     console.log(data);
// })// hmset 为散列里面的一个或多个键设置值 返回值 不报错就返回OK
// 以下两条命令效果相同
// client.hmset("hash key",{"hashtest 6":"hashtest 4","hashtest 7":"hashtest 5"},function(error,data){
//     if(error){
//         console.log(error);
//     }
//     console.log(data);
// })
// client.hmset("hash key","hashtest 6","hashtest 4","hashtest 7","hashtest 5",function(error,data){
//     if(error){
//         console.log(error);
//     }
//     console.log(data);
// })// hmget 从散列里面获取一个或多个键的值 返回值 对应结果的列表,格式list
// client.hmget("hash key",["hashtest 1","hashtest 2"],function(error,data){
//     if(error){
//         console.log(error)
//     }
//     console.log(data);
// })// hlen 返回散列包含的键值对数量 返回值 键值对数量
// client.hlen("hash key",function(error,data){
//     if(error){
//         console.log(error)
//     }
//     console.log(data);
// })// hexists 检查给定键是否在散列中 返回值 0/1 0:不存在 1:存在
// client.hexists('hash key','hashtest 1',function(error,data){
//     if(error){
//         console.log(error);
//     }
//     console.log(data);
// })// hkeys 获取散列包含的所有键 返回值 散列中所有键,格式list
// client.hkeys('hash key',function(error,data){
//     if(error){
//         console.log(error);
//     }
//     console.log(data);
// })// hvals 获取散列包含的所有值 返回值 散列中所有值,格式list
// client.hvals('hash key',function(error,data){
//     if(error){
//         console.log(error);
//     }
//     console.log(data);
// })// hincrby 为哈希表中的字段值加上指定增量值 返回值 运算后的值 传入的值要为int ,指定的键的值也要为int,浮点数也不可以,如果键不存在,则新增该键并将其值初始化为0后加上指定增量值
// client.hincrby('hash key',1,123,function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// })// hincrbyfloat 将存储的键值以指定浮点数增加 返回值 运算后的值 可以加整数
// client.hincrbyfloat('hash key',1,1,function(error,data){
//     if(error){
//         console.log(error);
//     }
//     console.log(data);
// })//---------------------------zset 操作---------------------------//
// zadd 有序集合插入,序号可以一致,如果值一致,更新
// client.zadd('zsetTest',4,'test5',6,'test6',function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// })// zrange 根据元素在有序排列中的位置,从中取出元素
// client.zrange('zsetTest',0,2,function(error,data){
//     if(error){
//         console.log(error);
//     }
//     console.log(data);
// })// zrangebyscore 获取有序集合在给定分值范围内的所有元素,下面语句获取4 <=score <= 10 的数据,返回值为list
// 有序集合结构
// zsetKey//score : munber
// client.zrangebyscore('zsetTest',4,10,function(error,data){
//     if(error){
//         console.log(error);
//     }
//     console.log(data);
// })// zrem 如果给定成员存在于有序集合,则移除 返回值 移除的个数
// client.zrem('zsetTest','test1','test2', 'test3',function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// })// zcard 获取一个有序集合中的成员数量 有序集的元素个数 返回值 成员数量
// client.zcard('zsetTest',function(error,data){
//     if(error){
//         console.log(error);
//     }
//     console.log(data)
// })//---------------------------keys 操作---------------------------//
// del 删除一个(或多个)keys 返回值 0/1 0:没有该键不存在删除;1:已删除
// client.del('zsetTest',function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// })// exists 查询一个key是否存在 返回值 0/1 0:不存在 1:存在
// client.exists('zsetTest',function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// })// expire 设置一个key的过期的秒数 返回值 0/1 0:不存在 1:已设置
// client.expire('setTest02',300,function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// })// pexpire 设置一个key的过期的毫秒数 返回值 0/1 0:不存在 1:已设置// expireat 设置一个UNIX时间戳的过期时间 返回值 0/1 0:不存在 1:已设置// pexpireat 设置一个UNIX时间戳的过期时间(毫秒) 返回值 0/1 0:不存在 1:已设置// persist 移除key的过期时间 返回值 0/1 0:不存在 1:已设置
// client.persist('setTest02',function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// })// sort (集合,列表,有序集合)将指定键中的值排序输出,如果要排序字符串,需要加入参数alpha
// client.sort('setTest02','alpha',function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// })// flushdb 清空当前数据库
// client.flushdb(function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// })// flushall 清空全部数据库
// client.flushall(function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// })

nodejs 操作 redis相关推荐

  1. 分享一个nodejs中koa操作redis的工具类 基于 ioredis

    分享一个node 操作redis的工具类 基于ioredis redis.js const config = require(':config/server.base.config'); const ...

  2. NodeJS 使用redis实现定时执行方法

    NodeJS 使用redis实现定时执行任务 文章目录 NodeJS 使用redis实现定时执行任务 场景 使用Redis定时器解决 Redis定时器 Redis发布订阅 操作 nodejs代码 主意 ...

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

    1.cent OS7 下使用redis 关闭防火墙 systemctl stop firewalld.service #停止firewallsystemctl disable firewalld.se ...

  4. 在 Node.js 中操作 Redis

    在 Node.js 中操作 Redis Node.js 中可以操作 Redis 的软件包推荐列表:https://redis.io/clients#nodejs. 推荐下面两个: node-redis ...

  5. 30 个 php 操作 redis 常用方法代码例子

    这篇文章主要介绍了 30 个 php 操作 redis 常用方法代码例子 , 本文其实不止 30 个方法 , 可以操作 string 类 型. list 类型和 set 类型的数据 , 需要的朋友可以 ...

  6. java 操作 redis_java操作Redis

    10. java操作Redis 10.1 环境准备 1. 引入依赖 redis.clients jedis 2.9.0 2.创建jedis对象 package org.example; import ...

  7. 还在直接操作 Redis?赶快来试试这个

    以下文章来源方志朋的博客,回复"666"获面试宝典 有些人还在直接用Jedis操作Redis数据库,但这种方式非常不方便,而且很不灵活.用Spring Boot整合Redis之后, ...

  8. 自己动手写一个能操作redis的客户端

    作者:孤独烟 来自:http://rjzheng.cnblogs.com/ 引言 redis大家在项目中经常会使用到.官网也提供了多语言的客户端供大家操作redis,如下图所示 但是,大家有思考过,这 ...

  9. redis缓存失效时间设为多少_java操作Redis缓存设置过期时间的方法

    关于Redis的概念和应用本文就不再详解了,说一下怎么在java应用中设置过期时间. 在应用中我们会需要使用redis设置过期时间,比如单点登录中我们需要随机生成一个token作为key,将用户的信息 ...

最新文章

  1. c++ 创建 mat
  2. 相关子查询 与非相关子查询
  3. php接受post值报错,php接收post参数时报错怎么办
  4. 台湾国立大学郭彦甫Matlab教程笔记(16) 数值微分 numerical differentiation
  5. Beautiful Lyrics
  6. ipad iphone开发_如何在iPhone或iPad上更改应用程序的语言
  7. 在需要时开启Perl新特性
  8. GVim中以十六进制方式打开文件
  9. HDU2029 Palindromes _easy version【入门】
  10. Discuz!NT 系统架构分析(以前的)
  11. 中国省市区三级联动带下拉美化带数据库
  12. 自制固件iOS4.1刷机、解锁教程
  13. [ZT]COMPAQ PROLIANT 8500上手动安装NetWare 4.11
  14. 温莎大学计算机硕士,温莎大学应用计算机硕士申请条件
  15. 信息传播渠道多元化日渐明显
  16. 判断两个IP地址(ipv4)是否在同一个网段
  17. Python爬虫(scrapy模块、bs4模块) 爬取笔趣阁全本小说(三级页面)
  18. 前端html页面中的命名规范
  19. 亲测无限坐席在线客服系统源码,基于ThinkPHP的一款在线客服系统源码
  20. Spring-Web(一) RestTemplate使用与源码浅析

热门文章

  1. Orange1.0开发日记(1)
  2. NVIDIA Tesla® P100 加速训练 OCR 模型
  3. 天刀服务器的位置2018,2018天刀哪区人多 | 手游网游页游攻略大全
  4. 基于单片机多功能智能电子时钟电路设计-毕业设计
  5. 2023年北京科技大学电子信息(控制工程)专业专硕考研上岸经验分享
  6. 过去几轮熊市是怎么牛回来的?这轮熊市的出路又在那里?
  7. 《闲聊瞎扯系列:科目二考试日记》
  8. 苹果明日发布第二财季财报 此前预计营收550到590亿美元
  9. python不及格人数_下面 if语句统计“成绩(score)优秀的男生以及不及格的男生”的人数,正确的语句是____________。_学小易找答案...
  10. 中小学生必备电视学习软件|宅家不虚度, 学习先人一步