Redis - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Redis List

Redis List

shape Description

Lists are a common data type in many different programming languages and lists in Redis aren't much different from the traditional list data structure. Lists are basically lists of strings that are sorted by the order they had been inserted. New list items can be added to either the beginning  or the end of the lists. It is very fast in Redis to insert or delete an item from near the front or back of the list. This also supports trimmings operations which can be used to keep the first or last x number of elements of the lists and discard the rest and consisting of a list of some of the common commands that are available in the Redis that are operated on the lists. LPUSH and RPUSH which add new items to the left side or right side of a list and we have LREM, which can be used to remove from the list. LSET will be set a values in the list based on the index that specify. LINDEX will return an element by its index, LRANGE will get a range of elements from the list. LLEN returns to length to length of the list. LPOP and RPOP remove and get the left most or right most item from the list. LTRIM will trim the list to a specified range.

Commands in the lists

shape Description

The list commands describes the list of key value pairs in the Redis server database.
S.NO Command Description
1 LLEN key Get the length of a list
2 LPOP Remove and get the first element in a list
3 LPUSH key value1 Prepend one or multiple values to a list
4 LPUSH key value Prepend a value to a list
5 LREM key count stop Get a range of elements from a list
6 LSET key index value Set the values of an elements in a list by its index
7 LTRIM key start stop Set the values of an element in a list by its index
8 RPOP key Remove and get the last element in a list
9 RPUSH key value1 Append one or multiple values to a list
10 RPUSHX key values Append a value to a list, only if the list exists
11 LINDEX key index Get an element from a list by its index
12 BRPOP key1[key2] timeout Remove and get the last element in a list, or block until one is available
13 BRPOPLPUSH source destination timeout POP a value from a list, push it to another list and return it.

shape Examples

The below example describes the basic functionality of list command in the Redis database. [c]redis 127.0.0.1:6379> LPUSH tutorials redis (integer) 1 redis 127.0.0.1:6379> LPUSH tutorials mongodb (integer) 2 redis 127.0.0.1:6379> LPUSH tutorials mysql (integer) 3 redis 127.0.0.1:6379> LRANGE tutorials 0 10 "mysql" "mongodb" "redis" [/c]

Summary

shape Key Points

  • Lists - Are the different programming language in list data types.
  • Commands in the lists - Describes the list of key values pairs in the Redis.