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!