Don sarrafa ayyukan bayanai daga Redis ciki NodeJS, kuna buƙatar amfani da Redis ɗakin karatu don NodeJS irin su redis
ko ioredis
sannan aiwatar da ayyuka na yau da kullun kamar ƙara, sabuntawa, sharewa, da tambayar bayanai a cikin Redis. A ƙasa akwai jagora mai sauƙi don aiwatar da waɗannan ayyuka:
Mataki 1: Shigar da Redis ɗakin karatu
Da farko, shigar da Redis ɗakin karatu ta amfani da npm:
npm install redis
Mataki 2: Haɗa zuwa Redis
lambar ku NodeJS, ƙirƙirar haɗi zuwa Redis:
const redis = require('redis');
// Create a Redis connection
const client = redis.createClient({
host: 'localhost', // Replace 'localhost' with the IP address of the Redis server if necessary
port: 6379, // Replace 6379 with the Redis port if necessary
});
// Listen for connection errors
client.on('error',(err) => {
console.error('Error:', err);
});
Mataki 3: Ƙara, Sabuntawa, Sharewa da Bayanan Tambaya
Bayan saita haɗin, zaku iya aiwatar da ayyukan bayanai kamar haka:
Ƙara bayanai :
// Store a value in Redis with the key 'name' and value 'John'
client.set('name', 'John',(err, reply) => {
if(err) {
console.error('Error:', err);
} else {
console.log('Stored:', reply);
}
});
Bayanan tambaya:
// Retrieve a value from Redis with the key 'name'
client.get('name',(err, reply) => {
if(err) {
console.error('Error:', err);
} else {
console.log('Retrieved:', reply);
}
});
Sabunta bayanai :
// Update the value of the key 'name' to 'Alice'
client.set('name', 'Alice',(err, reply) => {
if(err) {
console.error('Error:', err);
} else {
console.log('Updated:', reply);
}
});
Share bayanai :
// Delete the data with the key 'name'
client.del('name',(err, reply) => {
if(err) {
console.error('Error:', err);
} else {
console.log('Deleted:', reply);
}
});
Ta amfani da Redis ɗakin karatu a cikin NodeJS, zaku iya sarrafa ayyukan bayanai cikin sauƙi Redis kuma ku yi amfani da damar adana bayanai cikin sauri da inganci a cikin aikace-aikacenku.