DEV Community

Hrittik Bhattacharjee for espelar.dev

Posted on

Redis Commands Cheat Sheet Part 2 - Hashes and Lists

Redis Icon


Following are some of the commonly used Redis commands and data structures, use this list as a quick cheat sheet or a ready reference for your day-to-day Redis usage as needed 😄

This is Part 2 of this series, containing commands pertaining to Hashes and Lists in Redis.


Hashes

# create a hash
> HSET key field value [field value ...]
> HSET player:1000 name Artexius race Elf level 5
(integer) 3

# returns the number of fields saved in the hash, so 3 in the above case

# if field does not exist, HSET will create the field and set it's value as specified

# to set a field value only if it does not yet exist
> HSETNX key field value

# get all fields and values of the hash
> HGETALL key
> HGETALL player:1000

# get the value of a specific field in the hash
> HGET key field
> HGET player:1000 level

# get the value of a multiple specific fields in the hash
> HMGET key field [field ...]
> HMGET player:1000 level name race

# get all field names for a key
> HKEYS key

# get all field values for a key
> HVALS key

# update a field in the hash
# HSET overwrites the values of specified fields that exist in the hash. If key doesn't exist, a new key holding a hash is created
> HSET field
> HSET player:1000 level 15

# set a field in the hash only if field does not yet exist
> HSETNX key field value

# delete a field(s) in the hash
> HDEL key field [field ...] 
> HDEL player:1000 level

# increment the value of a hash field
> HINCRBY key field increment
> HINCRBYFLOAT key field increment

# determine if a hash field exists
> HEXISTS key field

# returns 1 if exists, 0 if does not

# iterate over fields that match a pattern
> HSCAN key cursor [MATCH pattern] [COUNT count]

# the first HSCAN called with cursor 0
# this returns a new cursor that can be used in subsequent iterations
# HSCAN Is more efficient than HGETALL
Enter fullscreen mode Exit fullscreen mode

Lists

# push elements (to right side) of list
> RPUSH key element [element ...]

# push elements (to left side) of list
> LPUSH key element [element ...]

# pop elements (from left side) of list
# default, the command pops a single element from the beginning of the list
# but when provided with the optional count argument, the reply will consist of up to count elements, depending on the list's length
> LPOP key [count]

# pop elements (from right side) of list
> RPOP key [count]

# get a range of elements from the list 
> LRANGE key start stop

# if you have a list of numbers from 0 to 100, LRANGE list 0 10 will return 11 elements, that is, the rightmost item is included
# -1 is rightmost value

# check length of the list
> LLEN key

# if key does not exist, it is interpreted as an empty list and 0 is returned
# error is returned when the value stored at key is not a list

# return element at a specified index
> LINDEX key index

# add an element before/after an element
> LINSERT key BEFORE|AFTER pivot-element new-element

# set value at a specified index
> LSET key index value

# remove the first 'count' occurrences of elements equal to 'element' from the list stored at key
# the 'count' argument influences the operation in the following ways:
#   - 'count' > 0: Remove elements equal to 'element' moving from head to tail
#   - 'count' < 0: Remove elements equal to 'element' moving from tail to head
#   - 'count' = 0: Remove all elements equal to 'element'
> LREM key count element

# trim list to a specified range
> LTRIM key start stop

# example: 
> LTRIM foobar 0 2 # only the first three elements of the list will remain in the list
Enter fullscreen mode Exit fullscreen mode

Hope you found this useful! Save the post for reference, here's Part 1 of the series. Part 3 will be out soon!

Do follow espelar.dev we're working on some pretty cool stuff and have exciting content for you in the works.😊
Cheers!

Top comments (0)