Posted 4 months ago. Visible to the public.

Version 5 of the Ruby Redis gem removes Redis.current

Redis.current will be removed without replacement in redis-rb 5.0.
Version 4.6.0 adds deprecation warnings for Redis.current and Redis.current=.

If your application still uses Redis.current, you can only fix it by no longer using it. Here is how.

Redis.new when you need it

You can easily instantiate a Redis client when you need it.

There is probably already a constant like REDIS_URL that you use to configure Sidekiq or similar. So just use that one.

Copy
Redis.new(url: REDIS_URL).get('example') # instead of Redis.current.get('example')

The performance impact of instantiating is negligible.
You may obviously assign that to a local or instance variable, if you want to.

Test support

If you access Redis in tests, I suggest introducing a helper method. Example for RSpec:

Copy
module RedisHelpers def redis @redis ||= Redis.new(url: REDIS_URL) end end RSpec.configure do |config| config.include(RedisHelpers) end

Your Tests may then say redis.get('example').

But I still want to use a global

While not suggested, you can obviously still use a global object.

Copy
$redis = Redis.new(url: REDIS_URL)

That way, it's at least clear that $redis is a shared global state.

Note

If your code uses threads, or if you're using a multi-threadeded web server like Puma, sharing $redis that way means all threads use a single Redis connection and must wait for each other Archive . The old Redis.current used one connection per thread. To do that yourself, a bulky Thread.current[:redis] should work but you have to initialize it per thread which may or may not be cumbersome in your case.

Your development team has a full backlog of feature requests, chores and refactoring coupled with deadlines? We are familiar with that. With our "DevOps as a Service" offering, we support developer teams with infrastructure and operations expertise.

Owner of this card:

Avatar
Arne Hartherz
Last edit:
about 1 month ago
by Arne Hartherz
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Arne Hartherz to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more