Module: Redised

Defined in:
lib/redised.rb

Overview

a .redis and .redis= method are provided

Constant Summary collapse

VERSION =
'0.2.1'

Class Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
# File 'lib/redised.rb', line 68

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({
              :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



26
27
28
29
30
31
# File 'lib/redised.rb', line 26

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.



12
13
14
15
# File 'lib/redised.rb', line 12

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

.redis_disconnectObject

Disconnect all the redis connections



18
19
20
21
22
23
# File 'lib/redised.rb', line 18

def self.redis_disconnect
  @_redis_connections.collect do |params, redis|
    redis.client.disconnect
    [params, redis.client.connected?]
  end
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


42
43
44
45
46
# File 'lib/redised.rb', line 42

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.



49
50
51
# File 'lib/redised.rb', line 49

def self.redised_config_path
  @_redised_config_path
end

.redised_config_path=(new_path) ⇒ Object

Set the config path to load from.



54
55
56
57
# File 'lib/redised.rb', line 54

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

.redised_envObject



59
60
61
# File 'lib/redised.rb', line 59

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

.redised_env=(new_env) ⇒ Object



63
64
65
66
# File 'lib/redised.rb', line 63

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