Search:   Dictionary All Posts
Store Your Knowledge at the Brain Bank!

Redis

By Jamie in Lessons / Programming - General  2.21.19  
Summary - Learn about Redis installation, commands for storing and getting data types: strings, hashes, lists, sets, sorted sets.
Redis is in-memory database used for storing small pieces of data in key:value format. It is perfect for storing sessions or small objects to cache. It is very fast since it is in-memory, but it also can lose data upon system crash or reboot.

Data types used by Redis: strings, hashes, lists, sets, sorted sets

Troubleshoot - when it says already running on port 6379, try this...
ps aux | grep redis
then look to see what # redis is being serviced on, then kill it, like this...
kill -9 8831
kill dash 9 then the service #
then back to redis-server


Installing Redis
You mostly use Redis within a server or Docker environment, but you can still install it on your computer...

install redis
brew install redis

start redis
brew services start redis

get into redis
redis-server

now that redis is running, open a new shell...

run redis cli commands
redis-cli

test if running/working
redis-cli ping    or just   ping

Redis CLI Commands  - Strings

SET key "value"     #  sets the word key to a value of value
GET key                    # will return "value"
EXISTS key               # will return 1, because 1 thing stored as key exists
DEL key                    # will delete all things with key of key

SET session "Jamie"        # creates a key of session with a value of Jamie
EXPIRE session 10            # sets an expiration in seconds of the given key

SET counter 100
INC counter 
GET counter               # 101

SET counter 100
DEC counter 33  
GET counter               # 99

SET counter 100
INCRBY counter 33  
GET counter               # 133

SET counter 100
DECRBY counter 33  
GET counter               # 100

set and get multiple items in one command...
MSET a 2 b 5
GET a                      # 2
GET b                     # 5

MGET a b                #   1) 2    2) 5

Redis CLI Commands  - Hashes

think of Hashes in Redis like objects in Javascript

HMSET user id 45 name "Jamie"
HGET user id                        # 45
HGET user name                # "Jamie"
HGETALL user                    # 1) "id"    2) "45"    3) "name"    4) "Jamie"

Redis CLI Commands  - Lists

Lists are a data structure that are useful if you have really long lists and you want to simply add a new item to a list. It will work really fast in this way.  However, it is not optimized for searching for an item in the list.

LPUSH ourlist 10                        # creates list called ourlist and adds 10
RPUSH ourlist "hello"              # adds to the end of the list
LRANGE ourlist 0 1                    #  1) 10    2) "hello"
LPUSH ourlist 55                        # adds 55 to beginning of the list
RPUSH ourlist "Jamie"            # adds "Jamie" to the right of the list
LRANGE ourlist 0 3                    # 1) 55   2) 10   3) "hello"   4) "Jamie"
RPOP ourlist                                # "Jamie"
LRANGE ourlist 0 3                    # 1) 55   2) 10   3) "hello" 

Redis CLI Commands  - Sets & Sorted Sets

Sets are the same as lists, but they don't allow duplicates.

SADD ourset 1 2 3 4 5                 # creates a set with said values
SADD ourset 3                              # tries to add 3, but won't work b/c already exists
SMEMBERS ourset                     #    1) "1"      2) "2 "     3) "3"      4) "4"      5) "5"
SMEMBER ourset 33                  #  0      # returns 0 because 33 does not exist in set
SMEMBER ourset 3                    #  1      # returns 1 because 3 exists in set

Sorted Sets stores a score and a value for each item in a set

ZADD team 30 "Eagles"        # creates team with "Eagles" and score of 30
ZADD team 20 "Browns"      #  adds "Browns" and score of 20 to team set
ZRANGE team 0 1                    # 1) "Browns"   2) "Eagles"  - returns list sorted order
ZADD team 6 "Bolts"             # adds new team to set
ZRANK team "Browns"        # 1  - returns position of team (note - starts with 0)