Module: Redised

Defined in:
lib/redised.rb

Overview

Redised allows for the common patter of module access to redis, when included a .redis and .redis= method are provided

Constant Summary collapse

VERSION =
'0.3.2'

Class Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/redised.rb', line 82

def self.included(klass)
  klass.module_eval do

    # Accepts:
    #   1. A 'hostname:port' string
    #   2. A 'hostname:port:db' string (to select the Redis db)
    #   3. A 'hostname:port/namespace' string (to set the Redis namespace)
    #   4. A redis URL string 'redis://host:port'
    #   5. An instance of `Redis`, `Redis::Client`, `Redis::DistRedis`,
    #      or `Redis::Namespace`.
    #   6. A 'hostname:port:db:password' string (to select the Redis db & a password)
    def self.redis=(server)
      if server.respond_to? :split

        if server =~ /redis\:\/\//
          conn = ::Redised.redis_connection(:url => server)
        else
          server, namespace = server.split('/', 2)
          host, port, db, password = server.split(':')
          conn = ::Redised.redis_connection(Redised.redis_options.merge({
              :host => host,
              :port => port,
              :thread_safe => true,
              :db => db,
              :password => password
          }))
        end

        @_redis = namespace ? Redis::Namespace.new(namespace, :redis => conn) : conn
      else
        @_redis = server
      end
    end

    def self.redised_namespace(new_name = nil)
      if new_name
        @_namespace = new_name
        @_redis = nil
      else
        @_namespace
      end
    end

    # Returns the current Redis connection. If none has been created, will
    # create a new one.
    def self.redis
      return @_redis if @_redis
      if ::Redised.redised_config
        self.redis = if redised_namespace
          ::Redised.redised_config[redised_namespace][::Redised.redised_env]
        else
          ::Redised.redised_config[::Redis.redised_env]
        end
      else
        self.redis = 'localhost:6379'
      end
      @_redis
    rescue NoMethodError => e
      raise("There was a problem setting up your redis for redised_namespace #{redised_namespace} (from file #{@_redised_config_path}): #{e}")
    end

    def redis
      self.class.redis
    end
  end

end

.redis_connectObject

(Re)Connect all the redis connections



28
29
30
31
32
33
# File 'lib/redised.rb', line 28

def self.redis_connect
  @_redis_connections.collect do |params, redis|
    redis.client.connect
    [params, redis.client.connected?]
  end
end

.redis_connection(params) ⇒ Object

Get a reusable connection based on a set of params. The params are the same as the options you pass to ‘Redis.new`

This ensures that an app doesnt try to open multiple connections to the same redis server.



14
15
16
17
# File 'lib/redised.rb', line 14

def self.redis_connection(params)
  @_redis_connections ||= {}
  @_redis_connections[params] ||= Redis.new(params)
end

.redis_disconnectObject

Disconnect all the redis connections



20
21
22
23
24
25
# File 'lib/redised.rb', line 20

def self.redis_disconnect
  @_redis_connections.collect do |params, redis|
    redis.client.disconnect
    [params, redis.client.connected?]
  end
end

.redis_optionsObject



78
79
80
# File 'lib/redised.rb', line 78

def self.redis_options
  @_redised_redis_options || {}
end

.redis_options=(options) ⇒ Object

Set global redis options

Redised.redis_options = {:timeout => 1.0}


74
75
76
# File 'lib/redised.rb', line 74

def self.redis_options=(options)
  @_redised_redis_options = options || {}
end

.redised_configObject

Load/parse the YAML config setup at ‘redised_config_path`. If no config is setup, returns nil

Configs are in the format:

---
namespace:
  env: url


44
45
46
47
48
# File 'lib/redised.rb', line 44

def self.redised_config
  if @_redised_config_path
    @_redised_config ||= YAML.load_file(@_redised_config_path)
  end
end

.redised_config_pathObject

Return the config path for the YAML config.



51
52
53
# File 'lib/redised.rb', line 51

def self.redised_config_path
  @_redised_config_path
end

.redised_config_path=(new_path) ⇒ Object

Set the config path to load from.



56
57
58
59
# File 'lib/redised.rb', line 56

def self.redised_config_path=(new_path)
  @_redised_config_path = new_path
  @_redised_config = nil
end

.redised_envObject



61
62
63
# File 'lib/redised.rb', line 61

def self.redised_env
  @_redised_env ||= ENV['RAILS_ENV'] || ENV['RACK_ENV'] || nil
end

.redised_env=(new_env) ⇒ Object



65
66
67
68
# File 'lib/redised.rb', line 65

def self.redised_env=(new_env)
  @_redised_env = new_env
  @_redised_config = nil
end