Challenge: INCR and DECR
You are working with a platform where users can like or dislike various posts. Each post has a unique identifier, such as post:1, post:2, post:3.
The task is to build a system to track reactions on multiple posts by randomly adding likes and dislikes, and then displaying the total count of reactions for each post.
-
Create 3 posts, each with two counters: one for likes and one for dislikes. For example:
post:1:likes,post:1:dislikes, and so on; -
Use the
INCRorINCRBYcommand to add likes and dislikes to each post. Add 3 likes and 2 dislikes topost:1, and 5 likes and 1 dislike topost:2; -
Use the
DECRorDECRBYcommand to decrease reactions for some posts. Remove 1 like frompost:1and 2 dislikes frompost:2; -
Use the
GETcommand to retrieve the current number of likes and dislikes for each post.
The expected result should look like this:
Post 1: 2 likes, 2 dislikes
Post 2: 5 likes, 0 dislikes
Post 3: 0 likes, 0 dislikes
Create counters for likes and dislikes for each post:
SET post:1:likes 0
SET post:1:dislikes 0
SET post:2:likes 0
SET post:2:dislikes 0
SET post:3:likes 0
SET post:3:dislikes 0
Add 3 likes and 2 dislikes to post:1:
INCRBY post:1:likes 3
INCRBY post:1:dislikes 2
Add 5 likes and 1 dislike to post:2:
INCRBY post:2:likes 5
INCRBY post:2:dislikes 1
Remove 1 like from post:1 and 2 dislikes from post:2:
DECRBY post:1:likes 1
DECRBY post:2:dislikes 2
Retrieve the current number of likes and dislikes for each post:
GET post:1:likes
GET post:1:dislikes
GET post:2:likes
GET post:2:dislikes
GET post:3:likes
GET post:3:dislikes
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you explain how the INCRBY and DECRBY commands work in Redis?
What happens if I try to decrease a counter below zero?
Can you show how to display the results in a formatted way?
Awesome!
Completion rate improved to 3.33
Challenge: INCR and DECR
Swipe to show menu
You are working with a platform where users can like or dislike various posts. Each post has a unique identifier, such as post:1, post:2, post:3.
The task is to build a system to track reactions on multiple posts by randomly adding likes and dislikes, and then displaying the total count of reactions for each post.
-
Create 3 posts, each with two counters: one for likes and one for dislikes. For example:
post:1:likes,post:1:dislikes, and so on; -
Use the
INCRorINCRBYcommand to add likes and dislikes to each post. Add 3 likes and 2 dislikes topost:1, and 5 likes and 1 dislike topost:2; -
Use the
DECRorDECRBYcommand to decrease reactions for some posts. Remove 1 like frompost:1and 2 dislikes frompost:2; -
Use the
GETcommand to retrieve the current number of likes and dislikes for each post.
The expected result should look like this:
Post 1: 2 likes, 2 dislikes
Post 2: 5 likes, 0 dislikes
Post 3: 0 likes, 0 dislikes
Create counters for likes and dislikes for each post:
SET post:1:likes 0
SET post:1:dislikes 0
SET post:2:likes 0
SET post:2:dislikes 0
SET post:3:likes 0
SET post:3:dislikes 0
Add 3 likes and 2 dislikes to post:1:
INCRBY post:1:likes 3
INCRBY post:1:dislikes 2
Add 5 likes and 1 dislike to post:2:
INCRBY post:2:likes 5
INCRBY post:2:dislikes 1
Remove 1 like from post:1 and 2 dislikes from post:2:
DECRBY post:1:likes 1
DECRBY post:2:dislikes 2
Retrieve the current number of likes and dislikes for each post:
GET post:1:likes
GET post:1:dislikes
GET post:2:likes
GET post:2:dislikes
GET post:3:likes
GET post:3:dislikes
Thanks for your feedback!