29
loading...
This website collects cookies to deliver better user experience
$ sudo add-apt-repository ppa:redislabs/redis
$ sudo apt-get update
$ sudo apt-get install redis
brew update
brew install redis
Startup Commands
redis-server
- To start Redis server use the commandredis-cli
and to close it use quit
commandBasic commands
SET name your-name
- Setting a value
GET name
- Get the above value
DEL name
- Delete a key-value by
EXISTS name
- To check if a key exist
KEYS *
- Get all keys
flushall
- Clear all the data
Expiration
ttl key-name
- Check how long a key has before being deleted automatically. if the result is -1
it means ttl(Time To Live) is not set and it wont expire.expire key-name 10
- Set a ttl of 10 seconds.setex name 10 your-name
- To set a ttl while setting a key value pair.lpush fruits apple
- Push an item into the left of a list. rpush fruits mango
- Push an item into the right of a list. lrange fruits 0 -1
- Get all the items from the list. The -1
stands for the index at the end of the list.LPOP fruits
- Removes the leftmost item in the list.RPOP fruits
- Removes the rightmost item in the list.SADD todo "read book"
- To add item into a set. (note: if we try to add "read book" again it won't be added as it is a duplicate)SMEMBERS todo
- Show all the items in the todo set.SREM todo "read book"
- To remove item from set. LIST
s and SET
s in Redis hold sequences of items, Redis HASH
es store a mapping of keys to values.HSET person name John
- Here name is the key and John is the value.HGET person name
- This returns the value associated with the key name and in this case it returns John.HGETALL person
- Get all the info about person.HDEL person name
- Remove the name property.HEXISTS person name
- To check if the property exists.npm i redis
redis-server
// Import redis package
const Redis = require('redis')
// Create redis client, in case ofer development only
const redisClient = Redis.createClient()
// Incase of production pass your production instance url and use the below line
const redisClient = Redis.createClient({ url: "your-production-url"})
redisClient
instance to do all the commands I mentioned above. for example -redisClient.setex('photos', 3600, JSON.stringyfy(some-value-to-store))
app.get("/photos", async(req, res) => {
const albumId = req.query.albumId
const { data } = await axios.get(
"https://jsonplaceholder.typicode.com/photos"
{ params: { albumId }}
)
})
// Import redis package
const Redis = require('redis');
// Create redis client, in case ofer development only
const redisClient = Redis.createClient();
// Incase of production pass your production instance url and use the below line
const redisClient = Redis.createClient({ url: 'your-production-url' });
app.get('/photos', async (req, res) => {
const albumId = req.query.albumId;
redisClient.get('photos', async (error, photos) => {
if (error) console.error(error);
if (photos != null) {
return res.json(JSON.parse(photos));
} else {
const { data } = await axios.get('https://jsonplaceholder.typicode.com/photos', {
params: { albumId }
});
redisClient.setex('photos', 3600, JSON.stringyfy(data));
res.json(data);
}
});
});