Module: Promiscuous::Redis

Defined in:
lib/promiscuous/redis.rb

Defined Under Namespace

Classes: Mutex, Script

Class Method Summary collapse

Class Method Details

.connectObject



6
7
8
9
# File 'lib/promiscuous/redis.rb', line 6

def self.connect
  disconnect
  @master = new_connection
end

.disconnectObject



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

def self.disconnect
  @master.quit if @master
  @slave.quit  if @slave
  @master = nil
  @slave  = nil
end

.ensure_connectedObject



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/promiscuous/redis.rb', line 65

def self.ensure_connected
  Promiscuous.ensure_connected

  @master.nodes.each do |node|
    begin
      node.ping
    rescue Exception => e
      raise lost_connection_exception(node, :inner => e)
    end
  end
end

.ensure_slaveObject



21
22
23
24
25
26
# File 'lib/promiscuous/redis.rb', line 21

def self.ensure_slave
  # ensure_slave is called on the first publisher declaration.
  if Promiscuous::Config.redis_slave_url
    self.slave = new_connection(Promiscuous::Config.redis_slave_url)
  end
end

.lost_connection_exception(node, options = {}) ⇒ Object



77
78
79
# File 'lib/promiscuous/redis.rb', line 77

def self.lost_connection_exception(node, options={})
  Promiscuous::Error::Connection.new("redis://#{node.location}", options)
end

.masterObject



11
12
13
14
# File 'lib/promiscuous/redis.rb', line 11

def self.master
  ensure_connected unless @master
  @master
end

.new_blocking_connectionObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/promiscuous/redis.rb', line 49

def self.new_blocking_connection
  # This removes the read/select loop in redis, it's weird and unecessary when
  # blocking on the connection.
  new_connection.tap do |redis|
    redis.nodes.each do |node|
      node.client.connection.instance_eval do
        @sock.instance_eval do
          def _read_from_socket(nbytes)
            readpartial(nbytes)
          end
        end
      end
    end
  end
end

.new_connection(url = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/promiscuous/redis.rb', line 35

def self.new_connection(url=nil)
  url ||= Promiscuous::Config.redis_urls
  redis = ::Redis::Distributed.new(url, :tcp_keepalive => 60)

  redis.info.each do |info|
    version = info['redis_version']
    unless Gem::Version.new(version) >= Gem::Version.new('2.6.0')
      raise "You are using Redis #{version}. Please use Redis 2.6.0 or later."
    end
  end

  redis
end

.slaveObject



16
17
18
19
# File 'lib/promiscuous/redis.rb', line 16

def self.slave
  ensure_connected unless @slave
  @slave
end