Getting Started With Redis in Ruby

Redis is an open source, BSD licensed, advanced key-value store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets and sorted sets.

To get started first we need is gem 'redis'. Open up irb console. Now working with redis is as simple as :

require 'redis'

redis_connection = Redis.new(:timeout => 0)
=> #<Redis client v3.0.7 for redis://127.0.0.1:6379/0>

redis_connection.set("key", "value")
=> "OK"

redis_connection.get("key")
=> "value"

redis_connection.del("key")
=> 1

redis_connection.get("key")
=> nil # As we have delete the key value from redis memory.

By default our redis connection is connected to localhost, but we can specify the server ip where we want redis connection to be established.

redis_connection = Redis.new(:timeout => 0, :host => 123.45.67.89)

Make sure redis on 123.45.67.89 is running and accepting connections from outside , other than localhost.

For more detailed list of commands for redis check here.

Comments