Redis, which got its name as a remote dictionary server, is a key-value data store that can be useful for temporary caching or as a NoSQL (‘not only SQL’) data store.
I recommend getting started with this post about Redis. It’s a little old, but covers some key use cases for Redis. In this post, I will provide an overview to get started with Redis.
Installation
Install Redis using Homebrew by running brew install redis
from the command line. Alternatively, complete download instructions can be found here.
Once installed, starting a Redis server is as simple as running redis-server
. You’ll know you have it working if you see some ASCII art like what’s depicted on the right.
Code example
You can view a full code example in this Github repo. It’s not really necessary to focus on the controller logic in sinatra_app.rb (it’s there to provide a user interface). This example can be viewed in the browser by running ruby redis_models.rb
, starting a Redis server (redis-server
), and visiting port 4567.
That said, we can look at the code without serving the example app. The snippet below gives a sense of some basics of Redis, which I’ll walk through line-by-line.
We start with the RedisClient
class. This class enables us to create a new instance of the Redis client, connected to localhost:6379, which is the default for Redis servers. (Notice when you run redis-server
from the console that we read “of the Redis client, connected to localhost:6379”). Instantiating a Redis client is necessary to work with the Redis server, thus we need the class on lines 6-11.
Side note: what’s happening in line 7 with include Singleton
isn’t terribly important at this point. Suffice it to say that the Singleton class is needed when it is necessary to have only one instance of the class. In this case, we need one and only one instance of Redis. The super
keyword uses the Redis
superclass’ initialize
method, which is written into the Redis gem.
So how is Redis being used? Notice some of the main keywords that are unique to Redis: lpush
, sadd
, and—two of the most fundamental ones—set
and get
, which we’ll see in more detail. I give an overview of the others at the end of this post.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
|
Interacting in IRB
Before we get too bogged down in the code, let’s try this from the command line. Everything I’ve covered up to this point can be done in IRB.
- In IRB:
require redis
redis = Redis.new(:host => 'localhost', :port => 6379)
- In a separate terminal window:
redis-server
- Below we see some of Redis’ magic in setting (Redis
SET
, aliased by the gem asset
) and getting (RedisGET
, aliased asget
) key-value pairs. We first set a new key-value pair (key of “hello”, value of “world”), then get it by getting that value (with key “hello”). Back in IRB:
redis.set("hello", "world")
redis.get("hello") #=> "world"
- But what other data types can Redis use? Here we see a hash:
redis.set(:and_a_hash?, {a:1,b:2,c:3})
redis.get(:and_a_hash?) #=> "{a:1,b:2,c:3}"
Note: this returns a string, but can be evaluated:eval("{a:1,b:2,c:3}") #=> {a:1,b:2,c:3}
. - Lists, sets, other data types? (http://redis.io/topics/data-types)
Redis data types and commands
There are a number key Redis commands. Below, I define each (Redis.io has more information about data types and commands). Ruby syntax is each Redis command downcase.
Redis Command |      | Command Definition |
---|---|---|
SET |      | Set the string value of a key |
GET |      | Get the value of a key |
INCR |      | Increment the integer value of a key by one |
DECR |      | Decrement the integer value of a key by one |
INCRBY |      | Increment the integer value of a key by the given amount |
APPEND |      | Append a value to a key |
GETRANGE |      | Get a substring of the string stored at a key |
SETRANGE |      | Overwrite part of a string at key starting at the specified offset |
GETBIT |      | Returns the bit value at offset in the string value stored at key |
SETBIT |      | Sets or clears the bit at offset in the string value stored at key |
Redis |      | Command |
---|---|---|
LPUSH |      | Prepend one or multiple values to a list |
RPUSH |      | Append one or multiple values to a list |
Redis |      | Command |
---|---|---|
SADD |      | Add one or more members to a set |
SINTER |      | Intersect multiple sets |
SPOP |      | Remove and return a random member from a set |
SRANDMEMBER |      | Get one or multiple random members from a set |
Redis |      | Command | ZADD |      | Add one or more members to a sorted set, or update its score if it already exists |
---|---|---|
ZRANGE |      | Return a range of members in a sorted set, by index |
ZRANK |      | Determine the index of a member in a sorted set |
ZRANGEBYSCORE |      | Return a range of members in a sorted set, by score |
Wrapping up
That’s a quick intro to Redis. You can try Redis using Redis’ own “Try Redis” command line tutorial, but I prefer setting it up in an application or IRB environment myself.