Shigarwa da Tsara Redis don NodeJS Ayyuka

Shigarwa da daidaitawa Redis don NodeJS aikin ya ƙunshi matakai masu zuwa:

Mataki 1: Shigarwa Redis

Da farko, kuna buƙatar shigarwa Redis akan kwamfutarka ko uwar garken ku. Redis za a iya shigar ta mai sarrafa kunshin ko zazzage shi daga Redis gidan yanar gizon hukuma.

Misali, akan Ubuntu, zaku iya shigar Redis da umarni masu zuwa a cikin Terminal:

sudo apt update  
sudo apt install redis-server  

Mataki 2: Dubawa Redis

Bayan shigarwa, zaku iya tabbatar da cewa Redis yana gudana daidai ta aiwatar da umarni mai zuwa:

redis-cli ping

Idan Redis yana gudana, zai dawo PONG.

Mataki 3: Saita Redis

Ta hanyar tsoho, Redis yana aiki akan tashar jiragen ruwa 6379 kuma yana amfani da tsarin tsoho. Koyaya, zaku iya siffanta Redis tsari gwargwadon bukatun aikin ku.

Redis Ana adana saitin a cikin fayil redis.conf  ɗin, yawanci yana cikin Redis kundin tsarin shigarwa. Kunna Ubuntu, ana yawan samun fayil ɗin daidaitawa a /etc/redis/redis.conf.

A cikin wannan fayil ɗin sanyi, zaku iya canza tashar jiragen ruwa, adireshin IP na sauraron, da sauran zaɓuɓɓuka.

Mataki na 4: Haɗa daga NodeJS

Don haɗawa da amfani Redis daga NodeJS aikace-aikacenku, kuna buƙatar amfani da Redis ɗakin karatu don NodeJS, kamar redis ko ioredis. Da farko, shigar da Redis ɗakin karatu ta hanyar npm:

npm install redis

Na gaba, a cikin lambar ku NodeJS, zaku iya ƙirƙirar haɗi zuwa Redis kuma kuyi ayyuka kamar haka:

const redis = require('redis');  
  
// Create a Redis connection  
const client = redis.createClient({  
  host: 'localhost',  
  port: 6379,  
});  
  
// Send Redis commands  
client.set('key', 'value',(err, reply) => {  
  if(err) {  
    console.error(err);  
  } else {  
    console.log('Set key-value pair:', reply);  
  }  
});  

Yanzu kun yi nasarar shigar kuma kun tsara Redis aikinku NodeJS kuma kuna iya amfani da shi don adanawa da dawo da bayanai cikin aikace-aikacenku.