[10] Redis – Node.js
28 stycznia 2022To jest przykład użycia Redis w Node.js
[1] Zainstaluj moduł klienta Redis.
[user01@vlsr01 ~]$ npm install redis
[2] Podstawowe użycie Node.js
[user01@vlsr01 ~]$ mcedit use_redis.js var redis = require('redis'); var client = new redis.createClient(); client.auth('TajneHasło'); //ustaw i pobierz klucz client.set('key01', 'value01'); client.get('key01', function (err, val) { console.log("key01.value :", val); }); //dodaj i pobierz klucz 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); }); //inkrementuj client.incrby('key02', 100); client.get('key02', function (err, val) { console.log("key02.value :", val); }); //decrementuj client.decrby('key02', 51); client.get('key02', function (err, val) { console.log("key02.value :", val); }); //listuj client.rpush('list01', 'value01'); client.rpush('list01', 'value02'); client.lrange('list01', 0, -1, function (err, val) { console.log("list01.value :", val); }); //mieszaj client.hset("hash01", "key01", "value01"); client.hset("hash01", "key02", "value02"); client.hgetall('hash01', function (err, val) { console.log("hash01.value :", val); }); //ustaw client.sadd("set01", "member01"); client.sadd("set01", "member02"); client.smembers('set01', function (err, val) { console.log("set01.value :", val); }); #uruchom [user01@vlsr01 ~]$ 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 : [ 'member01', 'member02' ]