zoukankan      html  css  js  c++  java
  • node(redis)

    给出node下redis操作的简单例子:

    var redis = require("redis"),
    client = redis.createClient(6379,'127.0.0.1',{});  
    
    client.on("error", function (err) {  
        console.log("Error " + err);  
    });  
    
    //设置并获取key1的值
    client.set("key1", "val1", redis.print);
    client.set("key1", "val2", redis.print);
    client.get("key1", function(err, reply) {
        console.log(reply);//打印结果val2
    });
    
    client.mset(["key2", "val2", "key3", "val3"], function (err, res) {
        client.get("key3", function(err, reply) {
            console.log(reply);//打印结果val3
        });
    });
    
    //在名为hosts的hash表中添加数据
    client.hmset("hosts", "mjr", "1", "another", "23", "home", "1234");
    client.hgetall("hosts", function (err, obj) {
        console.log(obj);//打印结果{"mjr", "1", "another", "23", "home", "1234"}
    });
    
    
    client.hset("hash key", "hashtest 1", "some value", redis.print);
    client.hset(["hash key", "hashtest 2", "some other value"], redis.print);
    //返回hash key中的keys
    client.hkeys("hash key", function (err, replies) {
        console.log(replies.length + " replies:");
        replies.forEach(function (reply, i) {
            console.log("    " + i + ": " + reply);
        });
    });
    
    client.multi([
                  ["mset", "something1","1", "something2","1", redis.print],
                  ["incr", "something1"],//值加1
                  ["incr", "something2"]//值加1
              ]).exec(function (err, replies) {
                  console.log(replies);//打印['OK',2,2]
                  
              });
    
    client.get("something1", function (err, obj) {
        console.log(obj);//打印2
    });
  • 相关阅读:
    C
    数论::整除分块
    洛谷P1262 间谍网络
    洛谷P1649 【[USACO07OCT]障碍路线Obstacle Course】
    HDU2066dijkstra模板题
    Captain Flint and Treasure
    CodeForces
    CodeForces
    HDU-1827
    HDU 1811
  • 原文地址:https://www.cnblogs.com/Fredric-2013/p/4519424.html
Copyright © 2011-2022 走看看