Guide to Using the Commands in Redis Cheat Sheet Part 2

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

If you’re just getting started in Redis, you may feel overwhelmed by the number of commands that are available for use. With a bit of practice, you’ll find that it’s easy to master these commands and harness the full power of Redis. However, a Redis cheat sheet can serve as a handy reference while you’re learning the ropes. In this second installment of our article series, we continue our guide to common Redis commands and provide examples of each command’s use.

Prerequisites

Before you dive into this Redis cheat sheet, make sure you have Redis installed on your machine.

List commands in Redis

In Redis, the list data type is just a string list that is ordered in insertion and sorted.

RPUSH

  • This command allows you to push a specified value onto the end of the list.
1
2
3
4
127.0.0.1:6379> RPUSH list "object"
(integer) 1
127.0.0.1:6379> RPUSH list "rocket"
(integer) 2

RPUSHX

  • This command appends a value to the list if the key exists.
1
2
127.0.0.1:6379> RPUSHX list "Tutorial"
(integer) 3

LPUSH

  • This command pushes the specified value at the start of the list.
1
2
3
4
127.0.0.1:6379> LPUSH newlist "There"
(integer) 1
127.0.0.1:6379> LPUSH newlist "Hey"
(integer) 2

LRANGE

  • The LRANGE command returns a subset of the list based on the specified parameters.
1
2
3
4
5
6
7
127.0.0.1:6379> LRANGE list 0 -1
1) "object"
2) "rocket"
3) "Tutorial"
127.0.0.1:6379> LRANGE newlist 0 -1
1) "Hey"
2) "There"

LINDEX

  • The LINDEX command returns an element of the list at a particular index of the list stored at the key.
1
2
3
4
5
6
7
8
127.0.0.1:6379> LINDEX newlist 0
"There"
127.0.0.1:6379> LINDEX newlist 1
"Hey"
127.0.0.1:6379> LINDEX newlist 2
(nil)
127.0.0.1:6379> LINDEX newlist 3
(nil)

LINSERT

  • LINSERT is used to insert an element into a list either before or after a specified reference value.
1
2
127.0.0.1:6379> LINSERT list BEFORE "rocket" "Basic"
(integer) 4

LLEN

  • This command displays the length of a list that is stored at the key.
1
2
127.0.0.1:6379> LLEN list
(integer) 4

LPOP

  • This command “pops”, or removes, the first element from the list and returns it as a value.
1
2
127.0.0.1:6379> LPOP newlist
"Hey"

LSET

  • The LSET command is used to set the value of an element at the specified index in the list.
1
2
3
4
127.0.0.1:6379> LSET list1 0 "d"
OK
127.0.0.1:6379> LSET list1 -3 "f"
OK

LTRIM

  • This command trims a list, leaving only the elements in the specified range.
1
2
127.0.0.1:6379> LTRIM list 1 -1
OK

RPOP

  • This command removes the last element from the specified list and returns it as a value.
1
2
127.0.0.1:6379> RPOP list
"Tutorial"

Set commands in Redis

The Redis set datatype differs from a list in that each string element must be unique with no repeated values.

SADD

  • The SADD command adds an item to the specified set that is stored at the key.
1
2
3
4
5
6
SADD set "Redis"
(integer) 1
SADD set "Cheat"
(integer) 1
SADD set "Sheet"
(integer) 1

SCARD

  • This command returns the number of elements contained in the set that is stored in the key.
1
2
SCARD set
(integer) 3

SREM

  • This command simply removes the specified value from the set.
1
2
3
4
SREM set "Cheat"
(integer) 1
SREM set "Cheat"
(integer) 0

SISMEMBER

  • This command returns the value of the member if it exists in the set.
1
2
3
4
SISMEMBER set "Redis"
(integer) 1
SISMEMBER set "Cheat"
(integer) 0

SMEMBERS

  • The SMEMBERS command returns all of the items or members in the set.
1
2
3
SMEMBERS set
1) "Redis"
2) "Sheet"

SUNION

  • This command will return the members of the set that would result from a union of all the specified sets.
1
2
3
4
5
6
7
8
9
10
SADD newset1 ten
(integer) 1
SADD newset2 nine
(integer) 1
SADD newset3 eight
(integer) 1
SUNION newset1 newset2 newset3
1) "eight"
2) "nine"
3) "ten"

SINTER

  • This command returns the members of the set that would result from the intersection of the specified sets.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
SADD sv "z"
(integer) 1
SADD sv "x"
(integer) 1
SADD sv "y"
(integer) 1
SADD sv1 "y"
(integer) 1
SADD sv1 "w"
(integer) 1
SADD sv1 "u"
(integer) 1
SINTER sv sv1
1) "y"

SMOVE

  • This command moves the member of a specified set to another specified set.
1
2
SMOVE sv1 sv "w"
(integer) 1

Sorted set commands in Redis

A sorted set in Redis consists of a unique collection of strings, just like a regular set. However, each member of a sorted set has a score associated with it, and members of the set are sorted in order of that score, from the smallest to the largest score.

ZADD

  • This command adds a specified member to a sorted set and updates the sorted set if the member already exists.
1
2
3
4
ZADD player 1 "Dominic"
(integer) 1
ZADD player 2 "Juan"
(integer) 1

ZCARD

  • This command returns the total number of members in a sorted set.
1
2
ZCARD player
(integer) 2

ZINCRYBY

  • This command increments a member’s score within a sorted set by a given value.
1
2
3
4
5
6
ZADD znum 1 "ten"
(integer) 1
ZADD znum 2 "eleven"
(integer) 1
ZINCRBY znum 2 "ten"
"3"

NOTE: The returned value of 3 shows that Redis increased the original score of 1 by adding the specified amount of 2.

ZRANGE

  • This command returns the range of a subset element in a sorted set.
1
2
ZRANGE player 1 2
1) "Juan"

ZRANK

  • The ZRANK command returns the rank of a specified member of the sorted set.
1
2
3
4
5
6
7
8
ZADD zset 1 "five"
(integer) 1
ZADD zset 2 "six"
(integer) 1
ZADD zset 3 "seven"
(integer) 1
ZRANK zset "seven"
(integer) 2

ZREM

  • This command removes a member from the sorted set.
1
2
ZREM zset "five"
(integer) 1

ZREMRANGEBYRANK

  • This command removes all members of the sorted set with a rank that falls between the given start and stop values.
1
2
ZREMRANGEBYRANK zset 0 1
(integer) 2

ZSCORE

  • This command returns the score of a particular member of a sorted set.
1
2
3
4
ZADD zset 1 "twelve"
(integer) 1
ZSCORE zset "twelve"
"1"

Conclusion

When you’re just getting acquainted with the various commands available in Redis, having a Redis cheat sheet at your fingertips can help you work more efficiently. In this article, we presented the second installment of our guide to common Redis commands. With this handy guide to use as a reference, you’ll be able to perform all your Redis tasks quickly and efficiently.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.