A Simple Method for reading and writing Redis databases in Node. js applications

Source: Internet
Author: User

A Simple Method for reading and writing Redis databases in Node. js applications

This article mainly introduces a simple method for reading and writing Redis databases in Node. js applications. Redis is a memory-based high-speed database. For more information, see

Before starting this article, make sure that you have installed Redis, Node. js, and Node. js Redis extensions-node_redis

First, create a new folder and create a text file app. js. The content of the file is as follows:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

Var redis = require ("redis ")

, Client = redis. createClient ();

 

Client. on ("error", function (err ){

Console. log ("Error" + err );

});

 

Client. on ("connect", runSample );

 

Function runSample (){

// Set a value

Client. set ("string key", "Hello World", function (err, reply ){

Console. log (reply. toString ());

});

// Get a value

Client. get ("string key", function (err, reply ){

Console. log (reply. toString ());

});

}

After connecting to Redis, The runSample function is called and a value is set. Then the value is read. The running result is as follows:

?

1

2

OK

Hello World

You can also use the EXPIRE command to set the object expiration time. The Code is as follows:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

Var redis = require ('redis ')

, Client = redis. createClient ();

 

Client. on ('error', function (err ){

Console. log ('error' + err );

});

 

Client. on ('connect ', runSample );

 

Function runSample (){

// Set a value with an expiration

Client. set ('string key', 'Hello world', redis. print );

// Expire in 3 seconds

Client. expire ('string key', 3 );

 

// This timer is only to demo the TTL

// Runs every second until the timeout

// Occurs on the value

Var myTimer = setInterval (function (){

Client. get ('string key', function (err, reply ){

If (reply ){

Console. log ('I live:' + reply. toString ());

} Else {

ClearTimeout (myTimer );

Console. log ('I expired ');

Client. quit ();

}

});

},1000 );

}

Note: The timer used above is only used to demonstrate the EXPIRE command. You must exercise caution when using the timer in the Node. js project.

The output result of running the above program is:

?

1

2

3

4

5

Reply: OK

I live: Hello World

I live: Hello World

I live: Hello World

I expired

Next we will check how long a value remains before it expires:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

Var redis = require ('redis ')

, Client = redis. createClient ();

 

Client. on ('error', function (err ){

Console. log ('error' + err );

});

 

Client. on ('connect ', runSample );

 

Function runSample (){

// Set a value

Client. set ('string key', 'Hello world', redis. print );

// Expire in 3 seconds

Client. expire ('string key', 3 );

 

// This timer is only to demo the TTL

// Runs every second until the timeout

// Occurs on the value

Var myTimer = setInterval (function (){

Client. get ('string key', function (err, reply ){

If (reply ){

Console. log ('I live:' + reply. toString ());

Client. ttl ('string key', writeTTL );

} Else {

ClearTimeout (myTimer );

Console. log ('I expired ');

Client. quit ();

}

});

},1000 );

}

 

Function writeTTL (err, data ){

Console. log ('I live for this long yet:' + data );

}

Running result:

?

1

2

3

4

5

6

7

8

Reply: OK

I live: Hello World

I live for this long yet: 2

I live: Hello World

I live for this long yet: 1

I live: Hello World

I live for this long yet: 0

I expired

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.