Nodejs下redis简单操作实例

Nodejs下redis简单操作实例

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();
    });
}

});


8 回复

Node.js 下 Redis 简单操作实例

Redis 是一个高性能的键值存储系统,常用于缓存、消息队列等场景。本示例将展示如何使用 Node.js 操作 Redis 数据库。

安装 Redis 客户端

首先,你需要安装 Redis 客户端库:

npm install redis

连接到 Redis

接下来,我们将连接到 Redis 服务器并执行一些基本操作。

// 引入 redis 模块
var redis = require('redis');

// 创建 Redis 客户端
var client = redis.createClient(6379, '127.0.0.1');

错误处理

当连接出现错误时,我们可以通过监听 error 事件来捕获错误信息:

client.on("error", function(error) {
    console.log(error);
});

验证

如果你的 Redis 配置文件中开启了验证,可以使用以下代码进行验证:

client.auth("foobared", function(error) {
    if (error) {
        console.log(error);
    } else {
        console.log("Authentication successful");
    }
});

设置键值对

我们可以使用 set 命令来设置一个字符串类型的键值对:

client.select('15', function(error) {
    if (error) {
        console.log(error);
    } else {
        client.set('str_key_0', '0', function(error, res) {
            if (error) {
                console.log(error);
            } else {
                console.log(res); // 输出 "OK"
            }
            client.end();
        });
    }
});

获取键值对

接下来,我们通过 get 命令来获取刚刚设置的键值对:

client.select('15', function(error) {
    if (error) {
        console.log(error);
    } else {
        client.get('str_key_0', function(error, res) {
            if (error) {
                console.log(error);
            } else {
                console.log(res); // 输出 "0"
            }
            client.end();
        });
    }
});

设置哈希表

使用 hmset 命令可以设置多个字段的哈希表:

client.select('15', function(error) {
    if (error) {
        console.log(error);
    } else {
        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); // 输出 "OK"
            }
            client.end();
        });
    }
});

获取哈希表中的值

使用 hmget 命令可以获取哈希表中的特定字段:

client.select('15', function(error) {
    if (error) {
        console.log(error);
    } else {
        client.hmget('site', 'baidu', function(error, res) {
            if (error) {
                console.log(error);
            } else {
                console.log(res); // 输出 ["www.baidu.com"]
            }
            client.end();
        });
    }
});

获取整个哈希表

使用 hgetall 命令可以获取整个哈希表:

client.select('15', function(error) {
    if (error) {
        console.log(error);
    } else {
        client.hgetall('site', function(error, res) {
            if (error) {
                console.log(error);
            } else {
                console.log(res); // 输出 { baidu: 'www.baidu.com', sina: 'www.sina.com', qq: 'www.qq.com' }
            }
            client.end();
        });
    }
});

列表操作

使用 lpush 命令可以向列表头部插入元素:

client.select('15', function(error) {
    if (error) {
        console.log(error);
    } else {
        client.lpush('list', 'key_0');
        client.lpush('list', 'key_1');
        client.end();
    }
});

获取列表元素

使用 lrange 命令可以获取列表中的所有元素:

client.select('15', function(error) {
    if (error) {
        console.log(error);
    } else {
        client.lrange('list', 0, -1, function(error, res) {
            if (error) {
                console.log(error);
            } else {
                console.log(res); // 输出 ["key_1", "key_0"]
            }
            client.end();
        });
    }
});

以上就是使用 Node.js 操作 Redis 的一些基础示例。希望这些代码对你有所帮助!


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

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

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

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

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

在这个帖子中,我们将展示如何使用 Node.js 进行 Redis 的基本操作。首先需要安装 Redis 客户端库,可以通过以下命令安装:

npm install redis

接下来是连接 Redis 服务器并执行一些常见的操作(如设置键值对、获取键值、哈希操作以及列表操作)的示例代码:

// 引入 redis 模块
const redis = require('redis');

// 创建 Redis 客户端
const client = redis.createClient(6379, '127.0.0.1');

// 监听错误事件
client.on("error", function(error) {
    console.error(error);
});

// 设置键值对
client.set('str_key_0', '0', function(err, reply) {
    if (err) throw err;
    console.log(reply); // 输出: OK

    // 获取键值
    client.get('str_key_0', function(err, reply) {
        if (err) throw err;
        console.log(reply); // 输出: 0

        // 设置哈希表
        const info = { baidu: 'www.baidu.com', sina: 'www.sina.com', qq: 'www.qq.com' };
        client.hmset('site', info, function(err, reply) {
            if (err) throw err;
            console.log(reply); // 输出: OK

            // 获取哈希表中的一个字段
            client.hmget('site', 'baidu', function(err, replies) {
                if (err) throw err;
                console.log(replies); // 输出: [ 'www.baidu.com' ]

                // 获取哈希表的所有字段
                client.hgetall('site', function(err, replies) {
                    if (err) throw err;
                    console.log(replies); // 输出: { baidu: 'www.baidu.com', sina: 'www.sina.com', qq: 'www.qq.com' }

                    // 向列表插入元素
                    client.rpush(['list', 'key_0'], function(err, replies) {
                        if (err) throw err;
                        console.log(replies); // 输出: 1

                        // 获取列表中的所有元素
                        client.lrange('list', 0, -1, function(err, replies) {
                            if (err) throw err;
                            console.log(replies); // 输出: [ 'key_0' ]
                            
                            // 关闭客户端连接
                            client.quit();
                        });
                    });
                });
            });
        });
    });
});

这些示例展示了如何通过 Node.js 对 Redis 进行基本的操作,包括字符串、哈希和列表数据类型的存储与检索。

回到顶部