Debian 11 Bullseye
Sponsored Link

Redis 6 : Use on Node.js2021/09/21

 
This is an example to use Redis on Node.js.
[1] Install Redis Client Module.
debian@dlp:~$
npm install redis

[2] Basic usage on Node.js.
debian@dlp:~$
vi use_redis.js
var redis = require('redis');
var client = new redis.createClient();

client.auth('password');

// set and get Key
client.set('key01', 'value01');
client.get('key01', function (err, val) {
    console.log("key01.value :", val);
});

// append and get Key
client.append('key01', ',value02');
client.get('key01', function (err, val) {
    console.log("key01.value :", val);
});

client.set('key02', 1);
client.get('key02', function (err, val) {
    console.log("key02.value :", val);
});

// increment
client.incrby('key02', 100);
client.get('key02', function (err, val) {
    console.log("key02.value :", val);
});

// decrement
client.decrby('key02', 51);
client.get('key02', function (err, val) {
    console.log("key02.value :", val);
});

// list
client.rpush('list01', 'value01');
client.rpush('list01', 'value02');
client.lrange('list01', 0, -1, function (err, val) {
    console.log("list01.value :", val);
});

// hash
client.hset("hash01", "key01", "value01");
client.hset("hash01", "key02", "value02");
client.hgetall('hash01', function (err, val) {
    console.log("hash01.value :", val);
});

// set
client.sadd("set01", "member01");
client.sadd("set01", "member02");
client.smembers('set01', function (err, val) {
    console.log("set01.value :", val);
});

# run

debian@dlp:~$
node use_redis.js

key01.value : value01
key01.value : value01,value02
key02.value : 1
key02.value : 101
key02.value : 50
list01.value : [ 'value01', 'value02' ]
hash01.value : { key01: 'value01', key02: 'value02' }
set01.value : [ 'member02', 'member01' ]
Matched Content