Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Setup redis with ruby on rails

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 641
    Comment on it

    Installing Redis

    curl -O http://redis.googlecode.com/files/redis-2.2.2.tar.gz
    tar xzf redis-2.2.2.tar.gz
    cd redis-2.2.2
    make
    cp src/redis-server src/redis-cli /usr/bin
    

    Redis Server

    If you want to use redis-server locally,  just run the following, it will use the default Redis config file:

    redis-server
    

    If you want to run it on a server you want to use your own config file:

    redis-server /path/to/redis.conf
    

    Redis and Rails

    Now we have Redis installed and running, we want to use it with rails.

    Firstly, add redis-rb to the Gemfile:

    gem 'redis', '2.1.1' (or any latest version)
    

    Then install the gem:

    bundle install
    

    Lastly, create initializer in config/initializers/redis.rb with following:

    $redis = Redis.new(:host => 'localhost', :port => 6379)
    

    It will create an instance of Redis client, connects to localhost:6379 (which is default).

    To check if its working fine, let's use rails console:

    > $redis
    => #<Redis client v2.1.1 connected to redis://localhost:6379/0 (Redis v2.2.2)> 
    > $redis.set('key', 'value')
    => "OK" 
    > $redis.get('key')
    => "value"
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: