hiredis-rb icon indicating copy to clipboard operation
hiredis-rb copied to clipboard

Doc update or clarification - using hiredis with redis

Open tonydehnke opened this issue 4 years ago • 8 comments

Hi, I'm a new rails user, so please forgive me if this is basic or off topic. I'm adding redis to my app and have be reviewing the Rails Guide and Readme here, as well as a doc from Engine Yard. Each one lays out a slightly different way to use Hi-Redis. I'm wondering which is correct, and if the Readme here could be updated to be a bit clearer - or explain why to do it as indicated here vs the Rails Guide.

From the Rails Guide: https://guides.rubyonrails.org/caching_with_rails.html#activesupport-cache-rediscachestore

From the Guide regarding Gem installation:
` To get started, add the redis gem to your Gemfile: gem 'redis'

You can enable support for the faster hiredis connection library by additionally adding its ruby wrapper to your Gemfile: gem 'hiredis'

Redis cache store will automatically require & use hiredis if available. No further configuration is needed.

Finally, add the configuration in the relevant config/environments/*.rb file: config.cache_store = :redis_cache_store, { url: ENV['REDIS_URL'] } `

This seems to differ from the Readme here, which is a bit confusing for me as it has two sections that start the same To use hiredis from redis-rb,....

One shows loading the two Gems. The next shows only the one gem with the require section that Rails Guides doesn't show.

Then the Engine Yard article here: https://www.engineyard.com/blog/rails-5-2-redis-cache-store says that the require is not needed as well, but they do show having to put driver in the config line.

So I'm wondering - which is correct, and is there a way we can make the Usage - redis-rb section of the readme a bit easier to understand, and clearer what to do?

Thanks for your time and patience.

tonydehnke avatar Oct 09 '19 02:10 tonydehnke

Wonder if you got any closer to solving this? Noticed the same thing - Rails and Heroku docs suggest that just installing hiredis is enough, but hiredis-rb and Engineyard docs suggest otherwise.

dwightwatson avatar May 28 '20 04:05 dwightwatson

I don't honestly recall at this point. I think we ended up going another route. If I find some notes about it I'll come back and update this.

tonydehnke avatar May 28 '20 04:05 tonydehnke

From what I can see, the explicit require is not needed. You can check this for your own setup by calling _client.driver on your Redis instance:

# Gemfile
gem 'redis'
gem 'hiredis'
irb(main):022:0> Redis.new(url: "redis://127.0.0.1:6379")._client.driver
=> Redis::Connection::Hiredis

lipanski avatar Mar 05 '21 09:03 lipanski

Cool find @lipanski. Curiously, I get the opposite without the explicit require:

irb(main):001:0> Redis.new(url: "redis://127.0.0.1:6379")._client.driver
=> Redis::Connection::Ruby

Though, actually, the Rails cache store seems to pick it up automatically:

>> Rails.cache.redis._client.driver
=> Redis::Connection::Hiredis

dwightwatson avatar Mar 05 '21 22:03 dwightwatson

@dwightwatson wonder if this depends on the rails/redis/hiredis version. I'm using:

rails (6.1.3)
redis (4.2.5)
hiredis (0.6.3)

lipanski avatar Mar 10 '21 06:03 lipanski

I’m on the same. Ruby 2.7.2.

dwightwatson avatar Mar 10 '21 10:03 dwightwatson

For future reference, this is order-dependent.

gem "redis", '< 4.6.0'
gem "hiredis", "~> 0.6.0"

or

gem "hiredis", "~> 0.6.0"
gem "redis", '< 4.6.0', require: ["redis", "redis/connection/hiredis"]

TheSmartnik avatar Mar 28 '22 09:03 TheSmartnik

No, it does not depend on order.

I use this in Gemfile

gem "hiredis", "~> 0.6.0"
gem "redis", "~> 4.7"

And get Hiredis:

Redis.new(url: "redis://127.0.0.1:6379")._client.driver
# Redis::Connection::Hiredis < Object

Clarification would be nice.

Maybe it's just the way Rails 6+ loads the classes with Zeitwerk

viktorianer avatar Jul 05 '22 10:07 viktorianer