Snippets Redis / Cheat Sheet

By Marcelo Fernandes Aug 12, 2017

SET / GET / SETNX (set if not exists)

The data in redis can only be retrieved if we know exactly which key we used for storing the data.

It is common to use a variable namespace (also called schema), in order to get a better notion about the data being retrieved.



SET server:name "fido"

GET server:name

# set if not exists (SETNX)
SETNX server:name "wow"

GET server:name
# returns fido


DEL

Deletes a given key and its associated value



SET attribute 10
DEL attribute

GET attribute
# Returns (nil) which is the same as NULL


INCR (increment)

This is an atomic operation. Therefore, if there is more than one client using the application, the atomicity of the operation will guarantee that the data is well incremented.



SET generic:variable 10

INCR generic:variable

GET generic:variable
# Returns '11'


EXPIRE / TTL (Time to live) / PERSIST

One of the most important functions from redis. Redis can be todt that a key should only exist for a certain length of time, and we might also check how long does this key is going to live


The current metric for the argument is seconds.

It might return -1 for the keys that never expire,
or it might return -2 for keys that do not exist anymore.



SET small:life "i wont live longer"

EXPIRE small:life 5

TTL small:life
# returns 4 after 1 second

TTL small:life
# returns -2 after 5 seconds, this means the object is gone.


SET mykey "Hello"
# "OK"

EXPIRE mykey 10
# (integer) 1

TTL mykey
# (integer) 10

PERSIST mykey
# (integer) 1

TTL mykey
# (integer) -1


RPUSH / LPUSH / LRANGE

The LPUSH and RPUSH keys insert a new value to a list, either from the head or from the tail respectively.

The LRANGE retrieves a subset of the list, works as python indexing.



LPUSH my:list 'hey'
LPUSH my:list 'beginner-word'
RPUSH my:list 'end-word'

LRANGE my:list 0 -1
# returns beginner-word hey end-word


RPOP / LPOP / LLEN

THE LPOP AND RPOP keys remove and retrieve a list value, either from the head or from the tail, respectively.

LLEN retrieves the length of the list



LPUSH my:other-list 'first-word'
RPUSH my:other-list 'second-word'
RPUSH my:other-list 'third-word'

LRANGE my:other-list 0 -1
# "first-word" "second-word" "third-word"

LPOP my:other-list
# "first-word"

LRANGE my:other-list 0 -1
# "second-word" "third-word"


SADD / SREM

Remember: Set very similar to a list, except it does not have a specific order and each element may only appear once

SADD and SREM will either add or remove an value from a set, respectively.



SADD sales:car "ford"
SADD sales:car "nissan"
SADD sales:car "honda"

SMEMBERS sales:car
# "ford" "nissan" "honda"

SREM sales:car "ford"

SMEMBERS sales:car
# "nissan" "honda"


SISMEMBER / SMEMBERS / SUNION

SISMEMBER and SMEMBERS work in order to get either if a value is a member of a set or to show all members of a given set, respectively

SUNION merge two sets considering only those values that are unique to both sets.


SADD sales:generic "ford"
SADD sales:generic "horse"
SADD sales:generic "doll"

SISMEMBER sales:generic "horse"
# returns 1 (it means it a member)

SREM sales:generic "horse"

SISMEMBER sales:generic "horse"
# returns 0 (it means it is not a member)

SUNION sales:car sales:generic
# returns "ford" "honda" "doll" "nissan"


ZADD / ZREM / ZRANGE

Remember: Sorted Sets are very similar to Sets, but they have a "score" for each entry, which gives them the ability to be sorted by this same "score".


ZADD hackers 1940 "Alan Kay"
ZADD hackers 1940 "Anonymous"
ZADD hackers 1906 "Grace Hopper"
ZADD hackers 1953 "Richard Stallman"
ZADD hackers 1965 "Yukihiro Matsumoto"
ZADD hackers 1916 "Claude Shannon"
ZADD hackers 1969 "Linus Torvalds"
ZADD hackers 1957 "Sophie Wilson"
ZADD hackers 1912 "Alan Turing"

ZREM hackers "Anonymous"

ZRANGE hackers 0 -1
# "Claude Shannon", 2) "Alan Kay", 3) "Richard Stallman"


HSET / HMSET / HGET / HGETALL

Hashes are maps between string fields and string values, so they are the perfect data type to represent objects (eg: A User with a number of fields like name, surname, age, and so forth):


HSET user:1 name "Marcelo"
HSET user:1 email "marcelo@example.com"
HSET user:1 password "secr3t"

# To return all data from this user use HGETALL
HGETALL user:1
# 1) "name" 2) "Marcelo" 3) "email" 4) "marcelo@example.com" 5) "password" 6) "secr3t"

# Set multiple fields at once
HMSET user:1001 name "Mary Jones" password "hidden" email "mjones@example.com"

# Get only a single field value
HGET user:1 name
# "Marcelo"




Notes


Reference

Documentation