redis-rb guide (Ruby)
Connect your Ruby application to a Redis database
redis-rb is the Ruby client for Redis.
The sections below explain how to install redis-rb and connect your application
to a Redis database.
redis-rb requires a running Redis server. See here for Redis Open Source installation instructions.
Install
To install redis-rb, run the following command:
gem install redis
Connect and test
Connect to localhost on port 6379:
require 'redis'
r = Redis.new
r.set 'foo', 'bar'
value = r.get('foo')
puts value # >>> bar
r.hset 'user-session:123', 'name', 'John'
r.hset 'user-session:123', 'surname', 'Smith'
r.hset 'user-session:123', 'company', 'Redis'
r.hset 'user-session:123', 'age', 29
hash_value = r.hgetall('user-session:123')
puts hash_value
# >>> {"name"=>"John", "surname"=>"Smith", "company"=>"Redis", "age"=>"29"}
r.close()
Store and retrieve a simple string.
require 'redis'
r = Redis.new
r.set 'foo', 'bar'
value = r.get('foo')
puts value # >>> bar
r.hset 'user-session:123', 'name', 'John'
r.hset 'user-session:123', 'surname', 'Smith'
r.hset 'user-session:123', 'company', 'Redis'
r.hset 'user-session:123', 'age', 29
hash_value = r.hgetall('user-session:123')
puts hash_value
# >>> {"name"=>"John", "surname"=>"Smith", "company"=>"Redis", "age"=>"29"}
r.close()
Store and retrieve a dict.
require 'redis'
r = Redis.new
r.set 'foo', 'bar'
value = r.get('foo')
puts value # >>> bar
r.hset 'user-session:123', 'name', 'John'
r.hset 'user-session:123', 'surname', 'Smith'
r.hset 'user-session:123', 'company', 'Redis'
r.hset 'user-session:123', 'age', 29
hash_value = r.hgetall('user-session:123')
puts hash_value
# >>> {"name"=>"John", "surname"=>"Smith", "company"=>"Redis", "age"=>"29"}
r.close()
Close the connection when you're done.
require 'redis'
r = Redis.new
r.set 'foo', 'bar'
value = r.get('foo')
puts value # >>> bar
r.hset 'user-session:123', 'name', 'John'
r.hset 'user-session:123', 'surname', 'Smith'
r.hset 'user-session:123', 'company', 'Redis'
r.hset 'user-session:123', 'age', 29
hash_value = r.hgetall('user-session:123')
puts hash_value
# >>> {"name"=>"John", "surname"=>"Smith", "company"=>"Redis", "age"=>"29"}
r.close()
More information
The
GitHub repository for redis-rb has a
set of examples
and further information about using redis-rb.