Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 920 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
$redis global variable with ruby on rails

#1
I am using redis as a read cache. I have created an initializer

config/initializer/redis.rb

$redis = Redis.new(:host => ENV["REDIS_HOST"], :port => ENV["REDIS_PORT"])

I am using this global in my unicorn.rb to create a new connection whenever a new worker is created.

before_fork do |server, worker|
# clear redis connection
$redis.quit unless $redis.blank?
end

# Give each child process its own Redis connection
after_fork do |server, worker|
$redis = Redis.new(:host => ENV["REDIS_HOST"], :port => ENV["REDIS_PORT"])
end

I am also using this global variable whenever I need to access my redis servers. But I am not comfortable using this global variable. Are there any better options than using global variable?
Reply

#2
There is `Redis.current`, which you can use to store your one-and-only `Redis` instance.

So instead of using `$redis`, you can assign your instance as follows:

Redis.current = Redis.new(:host => ENV["REDIS_HOST"], :port => ENV["REDIS_PORT"])

`Redis.current` was [introduced to redis-rb in 2010](

[To see links please register here]

) as a standard way to grab a redis connection, so I was surprised that no other answer mentioned it.

**Update:** Starting with version 4.6.0 `Redis.current` [has been deprecated](

[To see links please register here]

). The author notes that typical multi-threaded applications will find a lot of locking around a shared redis client. They recommend to define an own place to get a redis client, but also suggest to use a connection pool.

The accepted answer is therefore the simplest solution to achieve something comparable to `Redis.current`, but may not perform optimal in multi-threaded environments.
Reply

#3
expanding further on mestachs suggestion, namespacing a module in your initializer as below

config/initializers/redis.rb

module ReadCache
class << self
def redis
@redis ||= Redis.new(:url => (ENV["REDIS_URL"] || 'redis://127.0.0.1:6379'))
end
end
end

then in unicorn.rb

before_fork do |server, worker|
...
if defined?(ReadCache.redis)
ReadCache.redis.quit
end
...
end

after_fork do |server, worker|
...
if defined?(ReadCache.redis)
ReadCache.redis.client.reconnect
end
...
end
Reply

#4
According to [this](

[To see links please register here]

) Heroku, you don't need to add `$redis` to your Unicorn:
> No special setup is required when using Redis Cloud with a Unicorn server. Users running Rails apps on Unicorn should follow the instructions in the [Configuring Redis from Rails](

[To see links please register here]

) section and users...

Here's all the "[Configuring Redis from Rails](

[To see links please register here]

) section" has for before Rails 4 (besides the Gemfile and some other pre-Rails 3 stuff):

# config/initalizers/redis.rb

if ENV["REDISCLOUD_URL"]
uri = URI.parse(ENV["REDISCLOUD_URL"])
$redis = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
end

It doesn't really give an explanation as to why "no special setup is required".
Reply

#5
try this out:-

you can use `constant` instead of `global variable`.like in
config/initializer/redis.rb

REDIS = Redis.new(:host => ENV["REDIS_HOST"], :port => ENV["REDIS_PORT"])

and in unicorn.rb

before_fork do |server, worker|
# clear redis connection
REDIS.quit if defined?(REDIS)
end

# Give each child process its own Redis connection

after_fork do |server, worker|
REDIS ||= Redis.new(:host => ENV["REDIS_HOST"], :port => ENV["REDIS_PORT"])
end
Reply

#6
A more **namespaced** option to replace your global variable, you can create a method in a module

<pre>
module Caching
def self.redis
... initialize/memoize/reconnect here...
end
end
</pre>

You than then call it with:

```
Caching.redis
```
Reply

#7
if you don't already use another Rails.cache I advise you to just use that mechanism with redis.

The gem redis-store makes this realy easy (

[To see links please register here]

)

This way you can just do `Rails.cache.reconnect` and all is dandy

[To see links please register here]


It also allows you to use the awesome Rails.cache API, which has some neat features:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through