Home > Web Front-end > JS Tutorial > A detailed introduction on how to use Node.js to operate redis to add query function

A detailed introduction on how to use Node.js to operate redis to add query function

黄舟
Release: 2017-05-25 09:28:41
Original
2303 people have browsed it

Redis is a memory-based key (key) value (value) type data structure storage container. It can work completely in memory and can also be stored persistently. . When Redis works in persistence mode, it can be used as a non-relational database.

In actual development, it is inevitable to operate mysql, mongodb, redis and other data storage servers. Here is a brief introduction to how to operate redis.

An example

About the installation of the redis serverI won’t introduce it here, the focus is not here. Those who are interested can install it themselves.

For a new module, we need to install the redis module in our project before it can be used.

Command

cnpm install redis
Copy after login

Create a new redis.js file with the following code:

//引入redis
var redis = require("redis");
//创建redis客户端
var client = redis.createClient("6379", "127.0.0.1");
//连接错误处理
client.on("error", function (error) {
  console.log(error);
});
//redis验证 (如果redis没有开启验证,此配置可以不写)
client.auth("123456");
//查找

client.select("15", function (error) {
  if (error) {
    console.log(error);
  } else {
    client.set("node_redis_key", JSON.stringify({ "name": "wolfy", age: 28 }), function (error, res) {
      if (error) {
        console.log(error);
      } else {
        console.log(res);
      };
      //操作完成,关闭redis连接
      client.end(true);

    });
  };
});
Copy after login

Query

##Summary

Only list the operations of redis here For adding and querying, you can try the others by yourself. The methods are similar.

The above is the detailed content of A detailed introduction on how to use Node.js to operate redis to add query function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template