Node. js uses the redis database to cache data

Source: Internet
Author: User
Tags redis server
The Redis database adopts a simple design concept. The latest source code package is less than 2 Mb. Its usage is also different from the general database. The following article introduces how node. js uses the redis database to cache data. If you need it, let's take a look at it. The Redis database adopts a simple design concept. The latest source code package is less than 2 Mb. Its usage is also different from the general database. The following article introduces how node. js uses the redis database to cache data. If you need it, let's take a look at it.

I. Run redis

The Redis server uses port 6379 by default.

redis-server

Custom Port

redis-server –port 6390

Client

redis-cli

Specify ip address and port connection

redis-cli -h 127.0.0.1 -p 6390

Test whether the client and server are connected.

Ping

Ii. Nodejs connection to redis

Passredis.createClient(port,host,options)To connect to the redis Server

var redis = require("redis")var client = redis.createClient();

/* Client. HMSET stores the hash key value */client. HMSET (key, val, function (err, result) {if (err) {return callback ({code: 0, msg: err});} callback ({code: 1, msg: result});/* set the expiration time to 1 day */client. EXPIRE (bottleId, 86400 );});

/* Return a random key of the current database */client. RANDOMKEY (function (err, key) {if (! Key) {return callback ({code: 0, msg: 'No data'});}/* return the hash object based on the key */client. HGETALL (key, function (err, val) {if (err) {return callback ({code: 0, msg: err});} callback ({code: 1, msg: val});/* Delete the key value based on the key */client. DEL (key );});});

Iii. Common Redis commands

Redis command reference manual

Clear Database

FLUSHALL

Delete key

DEL key

Check whether the key exists.

EXISTS key // string HEXISTS key field // check whether the specified field EXISTS in the hash table key.

Return the type of the value stored in the key.

TYPE key

Obtains the value stored by the key.

String

GET key

Hash

HGETALL key // obtain all fields and values of the specified key in the hash table

For more articles about node. js using redis database to cache data, refer to PHP Chinese network!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.