zoukankan      html  css  js  c++  java
  • node redis操作

    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 = 1
     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     * 获取hash数据类型键值为1的所有key值
    139     * */
    140     hkeys: async () => {
    141         client.hkeys("1", function (err, replies) {
    142             if(err) throw err;
    143             console.log(replies);
    144             client.quit();
    145         });
    146     },
    147     /*
    148     * 获取hash数据类型键值为1的所有键值对数据
    149     * */
    150     hgetall: async () => {
    151         client.hgetall("1", function (err, obj) {
    152             console.log("hgetall:",obj);
    153         });
    154     },
    155     /*
    156     * 无序集合
    157     * */
    158     sadd: async () => {
    159         client.sadd("sadd", "a member", redis.print);
    160         client.sadd("sadd", "another member", redis.print);
    161         let set_size = 10;
    162         while (set_size > 0) {
    163             client.sadd("bigset", "member " + set_size,redis.print);
    164             set_size -= 1;
    165         }
    166     },
    167     /*
    168     * 有序集合
    169     * */
    170     zadd: async () => {
    171         client.zadd('zadd',[1,'one',2,'ninety-nine',3,'hello'], function(err, response){
    172             if(err) throw err;
    173             else {
    174                 console.log('added '+response+' items.');
    175             }
    176         })
    177     },
    178     /*
    179     * 返回无序集合 key 的基数(集合中元素的数量)。
    180     * 也就是list数据类型里的列表长度
    181     * 下面几种对应不同的数据结构
    182     * scard zcard hcard
    183     * */
    184     scard: async () => {
    185         client.scard("sadd",function(err,response){
    186             console.log(response);
    187             console.log("Number of key sadd:" + response);
    188         });
    189     },
    190     zcard: async () => {
    191         client.zcard("sadd",function(err,response){
    192             console.log(response);
    193             console.log("Number of key sadd:" + response);
    194         });
    195     },
    196     rpush: async () => {
    197         client.rpush("rpush", [1, 2, 3, 4, 5]);
    198     },
    199     /*
    200     * 下面几种对应不同的数据结构
    201     * hscan zscan sscan
    202     * */
    203     zscan: async () => {
    204         client.zscan('bisset', 2, 'COUNT', '0', function(err, res) {
    205              console.log(err);
    206              console.log(res);
    207              console.log(res[1].length);
    208         });
    209     },
    210     sscan: async () => {
    211         client.sscan('zadd', 2, 'COUNT', '0', function(err, res) {
    212             console.log(err);
    213             console.log(res);
    214             console.log(res[1].length);
    215         });
    216     },
    217     hscan: async () => {
    218         client.hscan('z', 2, 'COUNT', '0', function(err, res) {
    219             console.log(err);
    220             console.log(res);
    221             console.log(res[1].length);
    222         });
    223     },
    224     /*
    225    * zcount方法获取指定集合指定范围内的元素个数,设定为-Infinity, Infinity时,可以获取数组长度。
    226    * */
    227     zcount: async () => {
    228         client.zrange('zadd', -Infinity, Infinity, function(err, resp) {
    229             console.log(err);
    230             console.log("resp:", resp);
    231         });
    232     },
    233     /*
    234     * 根据元素在有序排列中的位置
    235     * zrange方法获取指定下标范围的内的所有key值,包括起始位置和终止位置。
    236     * */
    237     zrange: async () => {
    238         client.zrange('zadd', 0, 1, function(err, resp) {
    239             console.log(err);
    240             console.log("resp:", resp);
    241         });
    242     },
    243     /*
    244     * 根据元素在有序排列中的位置
    245     * 适用于列表数据类型的,set集合使用则会报错
    246     * */
    247     lrange: async () => {
    248         client.lrange('rpush', 0, 1, function(err, resp) {
    249             console.log(err);
    250             console.log("resp:", resp);
    251         });
    252     },
    253     /*
    254     *  随机返回一个key键
    255     * */
    256     randomKey: async () => {
    257         client.randomkey(function (err, reply) {
    258             if (err) return false;
    259             console.log(reply);
    260         });
    261     },
    262 }
    263 
    264 // test.set();
    265 // test.get();
    266 // test.remove()
    267 // test.hset();
    268 // test.hget();
    269 // test.hmset();
    270 // test.hmget();
    271 // test.keys();
    272 // test.scard();
    273 // test.zcard();
    274 // test.rpush();
    275 // test.sadd();
    276 // test.zadd();
    277 // test.zscan();
    278 // test.sscan();
    279 // test.hscan();
    280 // test.zcount();
    281 // test.zrange();
    282 // test.lrange();
    283 
    284 // test.randomKey();
    285 
    286 // test.hkeys();
    287 
    288 // test.hgetall();
    View Code


  • 相关阅读:
    XAML实例教程系列 依赖属性和附加属性
    分享Silverlight/Windows8/WPF/WP7/HTML5周学习导读(6月4日6月10日)
    QT GUI基本布局
    mqtt client libraries for c
    QT sqlite相关操作
    navicat 激活工具激活时必须断网 ,如果没有断网激活 激活过程中报如下错误 请卸载navicat 重新安装再行激活操作
    vmware 16 windows7企业版 tools安装不了 驱动签名验证
    虚拟机复制
    Install systemtap on Ubuntu 12.04
    DevOps的各个阶段
  • 原文地址:https://www.cnblogs.com/huoan/p/9225150.html
Copyright © 2011-2022 走看看