Redis basic
nurfitri •Installing redis server.
-
Ubuntu based system.
- sudo apt update
- sudo apt install redis
- sudo systemctl start redis
- redis-cli ping
-
Centos/RHEAL based system
- Digital Ocean Reference
- sudo yum install epel-release
- sudo yum install redis -y
- systemctl start redis.service
- redis-cli ping
- after running redis-cli ping you will get PONG in return from the server
Connecting Javascript project to redis.
- to connect our javascript to redis, we use redis node module.
- install redis using: npm install redis.
- then you can require the module using: const redis = require('redis')
- default redis url for redis server in localhost : redis://127.0.0.1:6379
- example code to create connection to server.
const redis = require('redis'); const redisUrl = 'redis://127.0.0.1:6379'; const client = redis.createClient(redisUrl);
Storing and Getting Data to Redis
- redis store data in form of (key,value) index.
- we can only store string data in redis, so let say you want to store an object or array you need to stringify it into a json object.
- to store data in redis, we use the set method from the redis node module.
- example code.
client.set('myKey','myValue',myCallback);
- to get data from redis, we use get method by giving the key as the parameter
- example code.
client.get('myKey',myCallback)
- the callback for get method returns two value, error and value of the key.
- so let say we simply want to print the value.
//this will print the value 'myValue` we previously set. client.get('myKey',(error,value)=>console.log(value));
- noticed that we are require to pass a callback in order to receive data from server, because this is an asynchronous operation.
- if you don't want to use callback and instead want to use promise, we can make use of promisify to convert our function into returning a promise.
const {promisify} = require('util'); const getAsync = promisify(client.get).bind(client);
- getAsync is now our new function that will return a promise
- we can use it with await or using then
- example using await.
//make sure this code run in async function try{ const value = await getAsync('myKey'); console.log(value); }catch(error){ console.error(error) }
- example using .then
getAsync('myKey').then(console.log).catch(console.error)