node.js下redis简单操作实例
发布于 11 年前 作者 fourlee 102192 次浏览 最后一次编辑是 8 年前

redis API:http://redis.readthedocs.org/cn/latest/index.html

npm install redis
// redis 链接
var redis   = require('redis');
var client  = redis.createClient('6379', '127.0.0.1');
// redis 链接错误
client.on("error", function(error) {
    console.log(error);
});
// redis 验证 (reids.conf未开启验证,此项可不需要)
client.auth("foobared");
client.select('15', function(error){
    if(error) {
        console.log(error);
    } else {
        // set
        client.set('str_key_0', '0', function(error, res) {
            if(error) {
                console.log(error);
            } else {
                console.log(res);
            }

            // 关闭链接
            client.end();
        });
    }
});
client.select('15', function(error){
    if(error) {
        console.log(error);
    } else {
        // get
        client.get('str_key_0', function(error, res){
            if(error) {
                console.log(error);
            } else {
                console.log(res);
            }

            // 关闭链接
            client.end();
        });
    }
});
client.select('15', function(error){
    if(error) {
        console.log(error);
    } else {
        // hmset
        var info = {};
        info.baidu = 'www.baidu.com';
        info.sina  = 'www.sina.com';
        info.qq    = 'www.qq.com';
        client.hmset('site', info, function(error, res){
            if(error) {
                console.log(error);
            } else {
                console.log(res);
            }

            // 关闭链接
            client.end();
        });
    }
});
client.select('15', function(error){
    if(error) {
        console.log(error);
    } else {
        // hmget
        client.hmget('site', 'baidu', function(error, res){
            if(error) {
                console.log(error);
            } else {
                console.log(res);
            }

            // 关闭链接
            client.end();
        });
    }
});
client.select('15', function(error){
    if(error) {
        console.log(error);
    } else {
        // hgetall
        client.hgetall('site', function(error, res){
            if(error) {
                console.log(error);
            } else {
                console.log(res);
            }

            // 关闭链接
            client.end();
        });
    }
});
client.select('15', function(error){
    if(error) {
        console.log(error);
    } else {
        // lpush
        client.lpush('list', 'key_0');
        client.lpush('list', 'key_1');
        client.end();
    }
});
client.select('15', function(error){
    if(error) {
        console.log(error);
    } else {
        // lrange
        client.lrange('list', '0', '-1', function(error, res){
            if(error) {
                console.log(error);
            } else {
                console.log(res);
            }

            // 关闭链接
            client.end();
        });
    }
});
10 回复

你写的这些方法,没有调用的示例么,因为对这些方法怎么执行怎么调用还不了解

@perfectnode 你所说的调用示例怎么理解?

@fourlee 就是说你封装的这个基础类,调用的时候怎么调用,可能对我这样小白的用户,还了解不够,所以希望能有增删查改的调用示例。呵呵,我也想做一个封装的好一些的基础类,然后在程序里面可以随意掉用。

@perfectnode 这个只是redis基本操作的一些实例,并不是封装后的类,希望不要误导到你。

怎么样根据 某个字段过滤出一部分数据据呢 比如 somekey => {name:'aa",value:100} , {name:'bb",value:200} {name:'cc",value:300}

我想找出从somekey中找出 >200 的数据项

@yakczh redis 没有提供过滤的API,需要你自己写吧

有没有大型实例,在MVC中的。

问个问题怎么使用EXISTS 来查看是否存在某个键

回到顶部