Redis stores key and value pairs in data structure server. It contains unique keys to each data values.

Key only contains string type. values contains following data types in Redis.

  • Strings
  • Lists
  • Sets
  • SortedSet
  • Hashes
  • HyperLogLogs
  • Bitmaps(BitStrings)

Redis Strings

Redis strings contains raw strings that contains sequence of characters. Allowed maximum string size is 512 megabytes in length. It stores any sequence of data and it is binary safe. Following commands used to interact with strings process in Redis

Command   Description  
SET key value Assign the value to the key
GET key Retrieve the value for given key
DEL key Deletes the give key and value from a cache

Here is an add,get and delete the keys in Redis interactive shell> set name anderw
OK> get name
"anderw"> keys *
1) "name"> del name
(integer) 1

Lists in Redis

List is used to store sequence of strings as values in insertion order for a given key.

List elements can be inserts head and tail, with linked strings. It is also called LinkedList.

Adding and insertion to the list performs better than retrieving the elements.

Following are list of operations in redis list.

Command   Description  
LPUSH key value add an element to left end of an list
RPUSH key value add an element to right end of an list
RANGES key start end Retrieve the list of values with start and index items for a given key,0 is start and -1 is end index
LPOP key delete an element from left end of an list
RPOP key remove an element from right end of an list
LINDEX key index Retrieves the element from the given key at index position

Let’s see an examples

roles as keys values as admin,sales,hr,support,finance

let’s create an roles of type lists with adding left to end of list. Since It is empty list added admin as one of the element.> lpush roles admin
(integer) 1

use lrange to retrieve range of items in a list. 0 is starting index, -1 is end index of an list.> lrange roles 0 -1
1) "admin"

Lets add few more element to left and right end of a list> lpush roles sales
(integer) 2> lrange roles 0 -1
1) "sales"
2) "admin"> rpush roles finance
(integer) 3> lrange roles 0 -1
1) "sales"
2) "admin"
3) "finance"> rpush roles support
(integer) 4> rpush roles hr
(integer) 5> rpush roles billing
(integer) 6> lrange roles 0 -1
1) "sales"
2) "admin"
3) "finance"
4) "support"
5) "hr"
6) "billing"

Next, get an element from a list using index with lindex> lindex roles 0
"sales"> lindex roles 1

you can remove elements from list usig rpop or lpop> rpop roles
"billing"> lrange roles 0 -1

1) "sales"
2) "admin"
3) "finance"
4) "support"
5) "hr"