Redis Datatypes and Commands-List

Redis Datatypes and Commands – List

Redis stores the data as key value pair. Keys can be any binary value and it is represented as ASCII strings for easily understandable and readability purpose. Redis supports five different datatypes and anyone of the datatype can be represented as value.

datatypes

Redis supports five different datatypes and they are given below. Each datatype can be used in Redis with different set of commands.

  • String
  • List
  • Set
  • Sorted Set
  • Hash

List

List is a one of the datatype in Redis. List contains the list of String and sorted by insertion order. The List commands are given below.

  • Pushing the element
  • Pop up the element
  • Finding the element by using Index
  • Finding the length of the List
  • Deleting the element

lpush

The LPUSH is a command used to push the elements into the on left side. The LPUSH command is used to prepend the values on the list. Execute the below commands to push the elements into the list by using LPUSH command.

Syntax
lpush <keyname> <value1> <value2> ...N

Example
lpush redis data1 data2 data3
lpush command

lrange

The LRANGE command is used to fetch the elements from the list by using start and stop index. Execute the below command to fetch the elements from the list.

Syntax
lrange <keyname> start stop

Example
lrange redis 0 1
lrange redis 0 2
lrange command

rpush

The RPUSH command is used to push the elements into the list on right side. The RPUSH command is used to append the elements into the list. Execute the below commands to push the elements into the list by using RPUSH command.

Syntax
rpush <keyname> <value1> <value2> .. N

Example
rpush redis data4
rpush command

Execute the below command to fetch the elements from the list and to see how the RPUSH command inserts the data into the list.

lrange redis 0 3
lrange command

llen

The LLEN command is used to find the length of the list. Execute the below command to find out the length of the list.

Syntax
llen <keyname>

Example
llen redis
llen command

LSET

The LSET command is used to set the value of the element based on the index. Use the below command to set the value of the element by using index.

Syntax
lset <keyname> <index> <value>

Example
lset redis 2 datavalue2
lset command

Execute the below command to fetch the elements from the list and to see how the LSET command inserts the data into the list.

lrange redis 0 3
lrange command

LPOP

The LPOP command is used to remove the first element in the list. Use the below command to remove the first element from the list.

Syntax
lpop <keyname>

Example
lpop redis
lpop command

Execute the below command to fetch the elements from the list and to see how the LPOP command removes the data into the list.

lrange redis 0 3
lrange command

rpop

The RPOP command is used to remove the last element in the list. Use the below command to remove the last element in the list.

Syntax
rpop <keyname>

Example
rpop redis
rpop command

Execute the below command to fetch the elements from the list and to see how the RPOP command removes the data into the list.

lrange redis 0 3
lrange command

LTRIM

The LTRIM is a command used to trim elements by specified index. Execute the below commands to trim the data from the left.

lpush numbers 1 2 3 4
lrange numbers 0 3

Syntax
ltrim <keyname> start stop

Example
ltrim numbers 1 2
ltrim command

Execute the below command to fetch the elements from the list and to see how the LTRIM command removes the data into the list.

lrange redis 0 3
lrange command

del

The DEL command is used to delete all the elements from the list. Execute the below command to delete all the elements from the list.

Syntax
del <keyname>

Example
del numbers
del command

Execute the below command to fetch the elements from the list and to see how the DEL command deletes all the elements from the list.

lrange redis 0 3
lrnage command

Redis List datatype and commands are learned and executed.

How useful was this post?

Click on a star to rate it!

As you found this post useful...

Follow us on social media!

Advertisements

Post your comments