Redis: The Basics
What is Redis ?
- Redis is an open source (BSD licensed), in-memory data structure store, used as database, cache and message broker
- Remote Dictionary Server
Installation
For Mac OS X:
1. Install XCode Developer Tools
2. Download the stable release on Redis official site: http://redis.io/download
3. Run the ff. command:
For Ubuntu:
1. Run the ff. command:
Data Types
List:
- collection of string elements, sorted according to the order of insertion. ex. 8, 9, 1, 2, 3
- implemented using a linked list, not using an array
- insertion of new elements to head or tail is at constant time regardless of the length of the list
Disadvantage:
- getting element by specific index is an extremely slow operation compared to list implemented using array
Advantage:
- can add elements to a very long list in a very fast way
- a list can be retrived at constant length in constant time
Common use cases for Lists
- Remember the latest updates posted by users into a social network.
Capped Lists
- List can be capped to only remember the latest N items and discard the old items
RPUSH - insert element in the tail of the list
LPUSH - insert element in the head of the list
LRANGE - gives a subset of the list
LLEN - returns the current length of the list
LPOP - removes element from the start of the list and returns it
RPOP - removes element from the start of the list and returns it
Sets:
- similar to List except elements are not in order and must be unique
SADD - adds given value to a set
SREM - removes given value from a set
SMEMBERS - returns list of all the members in a set
SISMEMBER - tests if a given value is in the set
SUNION - combines two or more sets and returns the list of all elements.
SINTER - Returns the members of the set resulting from the intersection of all the given sets.
Sorted Sets:
- similar to Set but strings are defined by score, which allows the retrieval of elements in an ordered list
- score provides sorting and ranking capabilities
Common use cases for Sorted Sets
- leaderboard system
ZADD - adds all the specified members with the specified scores to the sorted set stored at key
ZRANGE - returns the specified range of elements in the sorted set stored at key.
ZCOUNT - returns the number of elements in the sorted set at key with a score between min and max
ZREVRANK - returns the rank of member in the sorted set stored at key, with the scores ordered from high to low
Hashes:
- are maps between string fields and string values, so they are the perfect data type to represent objects
HSET - Sets field in the hash stored at key to value.
HGET - Returns the value associated with field in the hash stored at key.
HMSET - Sets the specified fields to their respective values in the hash stored at key.
HMGET - Returns the values associated with the specified fields in the hash stored at key.
HGETALL - Returns all fields and values of the hash stored at key.
- Redis is an open source (BSD licensed), in-memory data structure store, used as database, cache and message broker
- Remote Dictionary Server
Installation
For Mac OS X:
1. Install XCode Developer Tools
2. Download the stable release on Redis official site: http://redis.io/download
3. Run the ff. command:
wget http://download.redis.io/releases/redis-3.2.0.tar.gz
tar xzf redis-3.2.0.tar.gz
cd redis-3.2.0
# install and run globally (/usr/local/bin)
make install
redis-server
# install and run locally
make
cd src && ./redis-server
# install on another location
make install /opt/local/bin
For Ubuntu:
1. Run the ff. command:
sudo apt-get install redis-server
# run
redis-server
Data Types
List:
- collection of string elements, sorted according to the order of insertion. ex. 8, 9, 1, 2, 3
- implemented using a linked list, not using an array
- insertion of new elements to head or tail is at constant time regardless of the length of the list
Disadvantage:
- getting element by specific index is an extremely slow operation compared to list implemented using array
Advantage:
- can add elements to a very long list in a very fast way
- a list can be retrived at constant length in constant time
Common use cases for Lists
- Remember the latest updates posted by users into a social network.
Capped Lists
- List can be capped to only remember the latest N items and discard the old items
RPUSH - insert element in the tail of the list
127.0.0.1:6379> RPUSH char "B" "C"
(integer) 2
127.0.0.1:6379> LRANGE char 0 -1
1) "B"
2) "C"
LPUSH - insert element in the head of the list
127.0.0.1:6379> LPUSH char "A"
(integer) 3
127.0.0.1:6379> LRANGE char 0 -1
1) "A"
2) "B"
3) "C"
LRANGE - gives a subset of the list
127.0.0.1:6379> LRANGE char 0 1
1) "A"
2) "B"
LLEN - returns the current length of the list
127.0.0.1:6379> LLEN char
(integer) 3
LPOP - removes element from the start of the list and returns it
127.0.0.1:6379> LPOP char
"A"
RPOP - removes element from the start of the list and returns it
127.0.0.1:6379> RPOP char
"C"
Sets:
- similar to List except elements are not in order and must be unique
SADD - adds given value to a set
127.0.0.1:6379> SADD family "Jane" "John" "Paul" "Rachel"
(integer) 4
127.0.0.1:6379> SADD friends "Anna" "John" "Dave"
(integer) 3
SREM - removes given value from a set
127.0.0.1:6379> SREM family "Paul"
(integer) 1
SMEMBERS - returns list of all the members in a set
127.0.0.1:6379> SMEMBERS family
1) "John"
2) "Rachel"
3) "Jane"
SISMEMBER - tests if a given value is in the set
127.0.0.1:6379> SISMEMBER family "Paul"
(integer) 0
SUNION - combines two or more sets and returns the list of all elements.
# combine family and friends
127.0.0.1:6379> SUNION family friends
1) "Anna"
2) "John"
3) "Rachel"
4) "Jane"
5) "Dave"
SINTER - Returns the members of the set resulting from the intersection of all the given sets.
# getting the common friend and family
127.0.0.1:6379> SINTER family friends
1) "John"
Sorted Sets:
- similar to Set but strings are defined by score, which allows the retrieval of elements in an ordered list
- score provides sorting and ranking capabilities
Common use cases for Sorted Sets
- leaderboard system
ZADD - adds all the specified members with the specified scores to the sorted set stored at key
127.0.0.1:6379> ZADD grades 75 John 72 Maine 77 Michael 80 Abby 83 Becky 87 Dave 81 Elsa 84 Ginny 89 Ivan
(integer) 9
You can also add score and members individually:127.0.0.1:6379> ZADD users 1989 Jasmin
(integer) 1
127.0.0.1:6379> ZADD users 1970 Camille
(integer) 1
127.0.0.1:6379> ZADD users 1993 Jessica
(integer) 1
127.0.0.1:6379> ZADD users 1985 Tiffany
(integer) 1
ZRANGE - returns the specified range of elements in the sorted set stored at key.
127.0.0.1:6379> ZRANGE users 0 -1
1) "Camille"
2) "Tiffany"
3) "Jasmin"
4) "Jessica"
ZCOUNT - returns the number of elements in the sorted set at key with a score between min and max
# counting students with grades between 80 and 90
127.0.0.1:6379> ZCOUNT grades 80 90
(integer) 6
ZREVRANK - returns the rank of member in the sorted set stored at key, with the scores ordered from high to low
# getting the rank of Becky from highest to lowest grade
127.0.0.1:6379> ZREVRANK grades Becky
(integer) 3
Hashes:
- are maps between string fields and string values, so they are the perfect data type to represent objects
HSET - Sets field in the hash stored at key to value.
127.0.0.1:6379> HSET user:jane name Jane
(integer) 1
HGET - Returns the value associated with field in the hash stored at key.
127.0.0.1:6379> HGET user:jane name
"Jane"
HMSET - Sets the specified fields to their respective values in the hash stored at key.
127.0.0.1:6379> HMSET user:jane email "jane@gmail.com" hometown Laguna gender Female
OK
HMGET - Returns the values associated with the specified fields in the hash stored at key.
127.0.0.1:6379> HMGET user:jane name email
1) "Jane"
2) "jane@gmail.com"
HGETALL - Returns all fields and values of the hash stored at key.
127.0.0.1:6379> HGETALL user:jane
1) "name"
2) "Jane"
3) "email"
4) "jane@gmail.com"
5) "hometown"
6) "Laguna"
7) "gender"
8) "Female"
hey you remember me? we met in Dublin. Tell me your email to get in touch with you. Regards Mariano.
ReplyDelete