user5349758
user5349758

Reputation:

Configure Redis connection on initialize

I'm using Predictor gem and when I attempt to start the gem shows:

"redis not configured! - Predictor.redis = Redis.new" (RuntimeError)

So, how to configure Redis Connection on initialize?

thank's

Upvotes: 0

Views: 3280

Answers (1)

Lahiru Jayaratne
Lahiru Jayaratne

Reputation: 1762

This is how Redis is initialized in general.

Firstly, a good practice would be adding this to your config/environments/[environment_name].rb. So you can maintain different locations for Redis when you change environments.

config.redis_host   = "localhost"

Then in your application's config/initializers path create redis.rb and place the code below to initialize Redis.

require 'redis'

## Added rescue condition if Redis connection is failed
begin
  $redis = Redis.new(:host => Rails.configuration.redis_host, :port => 6379) 
rescue Exception => e
  puts e
end

Then you'll be able to use the global variable $redis within your application for Redis-related commands.

$redis.hset "my_hash", item.id, business.id

Here is a helpful article with more details.


Now in your case as this documentation suggests, here is what you should do:

In config/initializers/predictor.rb,

Predictor.redis = Redis.new(:url => ENV["PREDICTOR_REDIS"])

Or, to improve performance, add hiredis as your driver (you'll need to install the hiredis gem first)

Predictor.redis = Redis.new(:url => ENV["PREDICTOR_REDIS"], :driver => :hiredis)

Then, be sure to include include Predictor::Base in all models you want to use it,

class CourseRecommender
  include Predictor::Base
  ...
end

Here is the code responsible for the error you getting.

Upvotes: 2

Related Questions