Back
Featured image of post Redis List

Redis List

Redis Lists are simply lists of strings, sorted by insertion order. It is possible to add elements to a Redis List pushing new elements on the head (LPUSH) or on the tail (RPUSH) of the list.

We will be covered few basic commands for this List data type. We also will use the go-redis package for some having some example code wrriten in Golang.

Setup and connect redis server in Golang

var Client = redis.NewClient(&redis.Options{
	Addr:        "localhost:6379" //your redis address with the port. Default is localhost:6379
	Password:    "", //password if applicable
	DB:          1, //Redis offering 16 DB in total. 0 ~ 15 will be alright
	ReadTimeout: 10 * time.Minute,
})

LRANGE

redis list

LRANGE command basically takes in 3 parameters, which are the key, start position and end position. 0 being the first element of the list (the head of the list), 1 being the next element, and so on. These offsets can also be negative numbers indicating offsets starting at the end of the list. For example, -1 is the last element of the list, -2 the penultimate, and so on. Let’s say now we have multiple data are storing in the same key called allList, we can use LRANGE command to get them all or get them according to specific position

LRANGE allList 1 2 > sad > happy

LRANGE allList 0 -1 > upset > sad > happy

In go-redis (LRange)

//Client.LRange("key", start, end)
Client.LRange("allList", 1, 2) /*return [sad, happy]*/
Client.LRange("allList", 0, -1) /*return [upset, sad, happy]*/

LPUSH && RPUSH

LPUSH >> Insert all the specified values at the head of the list stored at key.
RPUSH >> Insert all the specified values at the back/ tail of the list stored at key.
If key does not exist, it is created as empty list before performing the push operations.

LPUSH allList “angry” LRANGE allList 0 -1 > angry > upset > sad > happy

RPUSH allList “angry” LRANGE allList 0 -1 > upset > sad > happy > angry

In go-redis (LPUSH && RPUSH)

Client.LPush("allList", "angry") 
Client.LRange("allList", 0, -1) /*return [angry, upset, sad, happy]*/
Client.LPush("allList", "satisfied", "grateful") /*allList > [grateful, satisfied, angry, upset, sad, happy]*/

/* ----------------------------------------------------------------------------------------- */
Client.RPush("allList", "angry") 
Client.LRange("allList", 0, -1) /*return [upset, sad, happy, angry]*/
Client.LPush("allList", "satisfied", "grateful") /*allList > [upset, sad, happy, angry, satisfied, grateful]*/

LPOP && RPOP

LPOP >> Remove the first element store in the list at selected key
RPOP >> Remove the last element store in the list at selected key

LRANGE allList 0 -1 > angry > upset > sad > happy

LPOP allList LRANGE allList 0 -1 > upset > sad > happy

RPOP allList LRANGE allList 0 -1 > angry > upset > sad

In go-redis (LPOP && RPOP)

Client.LRange("allList", 0, -1) /*return [angry, upset, sad, happy]*/
Client.LPop("allList") /*allList > [upset, sad, happy]*/

/* ----------------------------------------------------------------------------------------- */

Client.LRange("allList", 0, -1) /*return [angry, upset, sad, happy]*/
Client.RPop("allList") /*allList > [angry, upset, sad]*/

LSET

Update the element inside the list by specifying the index LRANGE allList 0 -1 > angry > upset > sad > happy

LSET allList 0 ‘determined’ LSET allList -1 ‘disappointed’ LRANGE allList 0 -1 > determined > upset > sad > disappointed

In go-redis (LPOP && RPOP)

Client.LRange("allList", 0, -1) /*return [angry, upset, sad, happy]*/
Client.LSet("allList", 0, "determined") /*allList > [determined, upset, sad, happy]*/
Client.LSet("allList", -1, "disappointed") /*allList > [determined, upset, sad, disappointed]*/
comments powered by Disqus