Class: Redis

Inherits:
Object
  • Object
show all
Includes:
SaneShutdown
Defined in:
lib/beetle/redis_ext.rb

Overview

Redis convenience and compatibility layer

Defined Under Namespace

Modules: SaneShutdown

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_server_string(server_string, options = {}) ⇒ Object

:nodoc:



3
4
5
6
7
8
# File 'lib/beetle/redis_ext.rb', line 3

def self.from_server_string(server_string, options = {})
  host, port = server_string.split(':')
  options = {:host => host, :port => port}.update(options)
  options.delete(:logger) if Redis::VERSION >= "5.0"
  new(options)
end

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/beetle/redis_ext.rb', line 29

def available?
  info_with_rescue != {}
end

#broken_shutdownObject

redis 2.2.2 shutdown implementation does not disconnect from the redis server. this leaves the connection in an inconsistent state and causes the next command to silently fail. this in turn breaks our cucumber test scenarios. fix this here, until a new version is released which fixes the problem.



65
# File 'lib/beetle/redis_ext.rb', line 65

alias_method :broken_shutdown, :shutdown

#hostObject

Redis 2 removed some useful methods. add them back.



11
# File 'lib/beetle/redis_ext.rb', line 11

def host; @client.host; end

#info_with_rescueObject



23
24
25
26
27
# File 'lib/beetle/redis_ext.rb', line 23

def info_with_rescue
  info
rescue Exception
  {}
end

#inspectObject

Redis 2 tries to establish a connection on inspect. this is evil!



56
57
58
# File 'lib/beetle/redis_ext.rb', line 56

def inspect
  super
end

#master!Object



15
16
17
# File 'lib/beetle/redis_ext.rb', line 15

def master!
  slaveof("no", "one")
end

#master?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/beetle/redis_ext.rb', line 37

def master?
  role == "master"
end

#msetnx(*values) ⇒ Object



79
80
81
# File 'lib/beetle/redis_ext.rb', line 79

def msetnx(*values)
  super != 0
end

#portObject



12
# File 'lib/beetle/redis_ext.rb', line 12

def port; @client.port; end

#roleObject



33
34
35
# File 'lib/beetle/redis_ext.rb', line 33

def role
  info_with_rescue["role"] || "unknown"
end

#serverObject



13
# File 'lib/beetle/redis_ext.rb', line 13

def server; "#{host}:#{port}"; end

#shutdownObject

Synchronously save the dataset to disk and then shut down the server.



68
69
70
71
72
73
74
75
76
77
# File 'lib/beetle/redis_ext.rb', line 68

def shutdown
  synchronize do
    begin
      @client.call [:shutdown]
    rescue Errno::ECONNREFUSED
    ensure
      @client.disconnect
    end
  end
end

#slave?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/beetle/redis_ext.rb', line 41

def slave?
  role == "slave"
end

#slave_of!(host, port) ⇒ Object



19
20
21
# File 'lib/beetle/redis_ext.rb', line 19

def slave_of!(host, port)
  slaveof(host, port)
end

#slave_of?(host, port) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
# File 'lib/beetle/redis_ext.rb', line 45

def slave_of?(host, port)
  info = info_with_rescue
  info["role"] == "slave" && info["master_host"] == host && info["master_port"] == port.to_s
end