1、redis 菜鸟驿站(先在在这里学习redis的安装、配置和命令行操作) http://www.runoob.com/redis/redis-tutorial.html
2、再介绍一个redis 图形化工具(个人非常建议) redis desktop Manager https://www.cnblogs.com/zheting/p/7670154.html
3、node_redis 官方文档 https://github.com/NodeRedis/node_redis
4、参考网址
https://www.cnblogs.com/harelion/p/5203710.html
https://blog.csdn.net/helencoder/article/details/51784654
Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合)。
概念理解网址 1、https://blog.csdn.net/wang379275614/article/details/47172469
2、https://blog.csdn.net/tim_phper/article/details/51891097
string 常用命令: 除了get、set、incr、decr mget等操作外,Redis还提供了下面一些操作:获取字符串长度、往字符串append内容、设置和获取字符串的某一段内容、设置及获取字符串的某一位(bit)、批量设置一系列字符串的内容
hash 常用命令: hget,hset,hmset,hmget,hgetall 等。
list 常用命令: lpush,rpush,lpop,rpop,lrange,BLPOP(阻塞版)等。
set 常用命令: sadd,srem,spop,sdiff ,smembers,sunion 等。
zset 常用命令: zadd,zrange,zrem,zcard等
我不懂得解释,直接代码。能用就行
先声明版本
node 8.10.0
npm 5.8.0
redis 2.8.0(node_modules里的版本,不是软件版本)
1 /* 2 * 下面有些命令的开头是z 或者h 或者s的 3 * z表示有序列表的数据类型 4 * h表示hash的数据类型 5 * s表示set集合的数据类型 6 * l表示list列表的数据类型 7 * 8 * */ 9 10 const redis = require("redis"), 11 RDS_PORT = 6379, //服务器端口 12 RDS_HOST = '127.0.0.1', //服务器ip 13 RDS_OPTS = {}, //设置值 14 db = 15 15 client = redis.createClient({RDS_PORT, RDS_HOST,RDS_OPTS, db}); 16 17 let test = { 18 set: async () => { 19 client.set('token', "普通的种下token", function(err, res) { 20 console.log(err); 21 console.log(res); 22 }); 23 client.expire('token', 60*60*24*30); // 缓存30天 设置失效时间 24 25 /* 26 * set 也允许设置失效时间 27 * */ 28 // client.set('token', '普通的种下token', 'EX', 60*60*24*30); // 缓存30天 设置失效时间 29 }, 30 get: async () => { 31 /* 32 * 得到 33 * */ 34 client.get('token', function(err, reply) { 35 if(err) { 36 console.log("报错了"); 37 console.log(err); 38 throw err; 39 // return; 40 } 41 if(reply) 42 console.log(reply); 43 else 44 console.log("没有值"); 45 }); 46 47 // let reply = await new Promise(function(resolve, reject) { 48 // client.get('token', function(err, reply) { 49 // resolve(reply?reply:''); 50 // }); 51 // }); 52 // if(reply) 53 // console.log(reply); 54 // else 55 // console.log("没有值"); 56 }, 57 remove: async ()=>{ 58 client.del("token",function(err, res) { 59 console.log(err); 60 console.log(res); 61 }); //删除 62 // await client.del('token'); //删除 支持await 63 }, 64 /* 65 * 单个key存储 hash 66 * */ 67 hset: async ()=> { 68 client.hset('hset', 'key001', 'hello', function (err, res) { 69 if (err) { 70 console.log(err); 71 } else { 72 console.log('res:', res); 73 } 74 }); 75 }, 76 /* 77 * 单个key获取 hash 78 * */ 79 hget: async ()=> { //单个key获取 hash 80 client.hget('hset', 'key001', function (err, getRslt) { 81 if (err) { 82 console.log(err); 83 } else { 84 console.log('getRslt:', getRslt); 85 client.quit(); 86 } 87 }); 88 }, 89 /* 90 * 多个key存储 hash 91 * */ 92 hmset: async ()=> { 93 // client.hmset("hosts", "mjr", "1", "another", "23", "home", "1234"); 94 client.hmset('hmset', {a:2,b:3,c:4}, function(err, response) { 95 if (err) { 96 console.log(err); 97 } else { 98 console.log(response); 99 // client.quit(); 100 } 101 }); 102 }, 103 /* 104 * 多个key获取 hash 105 * */ 106 hmget: async () => { 107 client.hmget("hosts", "mjr", "another", "home", function(err, res) { 108 if (err) { 109 console.log(err); 110 } else { 111 console.log(res); 112 } 113 }); 114 client.hmget('hmset', ['a', 'c'], function (err, res) { 115 if (err) { 116 console.log(err); 117 } else { 118 console.log(res); 119 } 120 }); 121 }, 122 /* 123 * 124 * 参考网址 https://blog.csdn.net/qq_28893679/article/details/53005057 125 * 遍历查询所有的key 126 * 可以模糊查询 性能可能损耗,建议独立开一个db 127 * */ 128 keys: async () => { 129 client.keys("605*", function (err, replies) { 130 console.log(replies.length + " replies:"); 131 replies.forEach(function (reply, i) { 132 console.log(" " + i + ": " + reply); 133 }); 134 client.quit(); 135 }); 136 }, 137 /* 138 * 无序集合 139 * */ 140 sadd: async () => { 141 client.sadd("sadd", "a member", redis.print); 142 client.sadd("sadd", "another member", redis.print); 143 let set_size = 10; 144 while (set_size > 0) { 145 client.sadd("bigset", "member " + set_size,redis.print); 146 set_size -= 1; 147 } 148 }, 149 /* 150 * 有序集合 151 * */ 152 zadd: async () => { 153 client.zadd('zadd',[1,'one',2,'ninety-nine',3,'hello'], function(err, response){ 154 if(err) throw err; 155 else { 156 console.log('added '+response+' items.'); 157 } 158 }) 159 }, 160 /* 161 * 返回无序集合 key 的基数(集合中元素的数量)。 162 * 也就是list数据类型里的列表长度 163 * 下面几种对应不同的数据结构 164 * scard zcard hcard 165 * */ 166 scard: async () => { 167 client.scard("sadd",function(err,response){ 168 console.log(response); 169 console.log("Number of key sadd:" + response); 170 }); 171 }, 172 zcard: async () => { 173 client.zcard("sadd",function(err,response){ 174 console.log(response); 175 console.log("Number of key sadd:" + response); 176 }); 177 }, 178 rpush: async () => { 179 client.rpush("rpush", [1, 2, 3, 4, 5]); 180 }, 181 /* 182 * 下面几种对应不同的数据结构 183 * hscan zscan sscan 184 * */ 185 zscan: async () => { 186 client.zscan('bisset', 2, 'COUNT', '0', function(err, res) { 187 console.log(err); 188 console.log(res); 189 console.log(res[1].length); 190 }); 191 }, 192 sscan: async () => { 193 client.sscan('zadd', 2, 'COUNT', '0', function(err, res) { 194 console.log(err); 195 console.log(res); 196 console.log(res[1].length); 197 }); 198 }, 199 hscan: async () => { 200 client.hscan('z', 2, 'COUNT', '0', function(err, res) { 201 console.log(err); 202 console.log(res); 203 console.log(res[1].length); 204 }); 205 }, 206 /* 207 * zcount方法获取指定集合指定范围内的元素个数,设定为-Infinity, Infinity时,可以获取数组长度。 208 * */ 209 zcount: async () => { 210 client.zrange('zadd', -Infinity, Infinity, function(err, resp) { 211 console.log(err); 212 console.log("resp:", resp); 213 }); 214 }, 215 /* 216 * 根据元素在有序排列中的位置 217 * zrange方法获取指定下标范围的内的所有key值,包括起始位置和终止位置。 218 * */ 219 zrange: async () => { 220 client.zrange('zadd', 0, 1, function(err, resp) { 221 console.log(err); 222 console.log("resp:", resp); 223 }); 224 }, 225 /* 226 * 根据元素在有序排列中的位置 227 * 适用于列表数据类型的,set集合使用则会报错 228 * */ 229 lrange: async () => { 230 client.lrange('rpush', 0, 1, function(err, resp) { 231 console.log(err); 232 console.log("resp:", resp); 233 }); 234 }, 235 /* 236 * 随机返回一个key键 237 * */ 238 randomKey: async () => { 239 client.randomkey(function (err, reply) { 240 if (err) return false; 241 console.log(reply); 242 }); 243 } 244 } 245 246 // test.set(); 247 // test.get(); 248 // test.remove() 249 // test.hset(); 250 // test.hget(); 251 // test.hmset(); 252 // test.hmget(); 253 // test.keys(); 254 // test.scard(); 255 // test.zcard(); 256 // test.rpush(); 257 // test.sadd(); 258 // test.zadd(); 259 // test.zscan(); 260 // test.sscan(); 261 // test.hscan(); 262 // test.zcount(); 263 // test.zrange(); 264 // test.lrange(); 265 266 // test.randomKey(); 267 268 // test.hkeys();