Module: RedisKit

Defined in:
lib/redis-kit/resque.rb,
lib/redis-kit.rb,
lib/redis-kit/cache.rb,
lib/redis-kit/railtie.rb,
lib/redis-kit/version.rb,
lib/redis-kit/cache/helper.rb

Overview

Resque handles its own Redis connection, reconnecting after forks. If $redis is a separate connection, however, we’ll disconnect it automatically after a fork so that the job can use the connection if it wants. If Resque is using the $redis connection, we let Resque handle it.

Defined Under Namespace

Modules: Resque Classes: Cache, InvalidConfigSyntaxError, MissingConfigError, Railtie

Constant Summary collapse

CUSTOM_CONFIG_KEYS =
[:mock]
VERSION =
"0.1.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.redisObject



16
17
18
# File 'lib/redis-kit.rb', line 16

def redis
  @redis || $redis
end

Class Method Details

.config_from_yml(path, env) ⇒ Object

Load the Redis configuration for the given environment. The YAML file will be passed through ERB before being parsed.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/redis-kit.rb', line 56

def self.config_from_yml( path, env )
  require 'erb'
  config = YAML.load ERB.new( IO.read( path ) ).result

  if config == false
    raise MissingConfigError.new "The Redis configuration file at " \
      "#{path} is empty. See the RedisKit README for information about how " \
      "to format this file: https://github.com/stvp/redis-kit"
  elsif config.key?( env )
    symbolize_keys( config[env] )
  else
    raise MissingConfigError.new "There is no Redis config for the " \
      "#{env.inspect} environment in #{path}. Either add one or set your "\
      "Redis URL with an ENV variable like \"REDIS_URL\"."
  end
rescue Errno::ENOENT
  raise MissingConfigError.new "#{path} doesn't exist. Please add a Redis " \
    "config YAML file or supply an ENV variable like \"REDIS_URL\"."
rescue Exception => e
  if Object.const_defined?('Psych') && e.class == Psych::SyntaxError
    raise InvalidConfigSyntaxError.new "A YAML syntax error occurred while " \
      "parsing #{path}. Please note that YAML must be consistently indented " \
      "using spaces. Tabs are not allowed.\nError: #{e.message}"
  else
    raise
  end
end

.load_config(path, env) ⇒ Object

Try loading the Redis config from an environment variable or, if there isn’t one, a YAML config file.



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/redis-kit.rb', line 33

def self.load_config( path, env )
  opts = {}
  opts[:driver] = :hiredis if use_hiredis?

  if url = url_from_env
    opts.merge( url: url )
  else
    config = config_from_yml( path, env )
    opts.merge( config )
  end
end

.new_redis(path, env) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/redis-kit.rb', line 21

def self.new_redis( path, env )
  config = load_config( path, env )
  if config[:mock]
    require 'mock_redis'
    MockRedis.new
  else
    Redis.new( config.reject{ |k, v| CUSTOM_CONFIG_KEYS.include?( k ) } )
  end
end

.url_from_envObject

Try to find a Redis URL in one of the usual ENV variables.



46
47
48
49
50
51
52
# File 'lib/redis-kit.rb', line 46

def self.url_from_env
  %w{ REDIS_URL REDISGREEN_URL OPENREDIS_URL REDISTOGO_URL }.map do |var|
    ENV[var]
  end.find do |url|
    url
  end
end