Class: Redis::Client

Inherits:
EventMachine::Connection
  • Object
show all
Includes:
Sender
Defined in:
lib/redis/client.rb,
lib/redis/synchrony.rb

Defined Under Namespace

Classes: Command

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Sender

#send_redis

Constructor Details

#initialize(*ignore_args) ⇒ Client

Returns a new instance of Client.



22
23
24
25
26
27
28
29
30
31
# File 'lib/redis/client.rb', line 22

def initialize *ignore_args
  if defined? Hiredis and defined? Hiredis::Reader
    @reader = Hiredis::Reader.new
  else
    @reader = Reader.new
  end
  @queue = []
  @multi = nil
  @pubsub_callback = proc{}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/redis/client.rb', line 71

def method_missing method, *args, &block
  if @multi and ![:multi, :exec].include? method
    for_queue = new_command true, false, method, *args
    command = new_command false, true, method
    callback_multi = @multi
    for_queue.callback do |status|
      callback_multi << command
    end
    for_queue.errback do |err|
      callback_multi << err
      command.fail err
    end
  else
    command = new_command true, true, method, *args
  end
  command.callback &block if block
  command
end

Class Method Details

.transformsObject

Some data is best transformed into a Ruby type. You can set up global transforms here that are automatically attached to command callbacks.

Redis::Client.transforms[:mycustom1] = Redis::Client.transforms[:exists] # boolean
Redis::Client.transforms[:mycustom2] = proc { |data| MyType.new data }
Redis::Client.transforms.delete :hgetall # if you prefer the array


129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/redis/client.rb', line 129

def self.transforms
  @@transforms ||= lambda {
    boolean = lambda { |tf| tf[0] == 1 ? true : false }
    hash = lambda { |hash| Hash[*hash] }
    pubsub = lambda { |msg| lambda { msg } }
    {
      #pubsub
      :subscribe => pubsub,
      :psubscribe => pubsub,
      :unsubscribe => pubsub,
      :punsubscribe => pubsub,
      #keys
      :exists => boolean,
      :expire => boolean,
      :expireat => boolean,
      :move => boolean,
      :persist => boolean,
      :renamenx => boolean,
      #strings
      :msetnx => boolean,
      :setnx => boolean,
      #hashes
      :hexists => boolean,
      :hgetall => hash,
      :hset => boolean,
      :hsetnx => boolean,
      #sets
      :sismember => boolean,
      :smove => boolean,
      :srem => boolean,
      #zsets
      :zrem => boolean,
    }
  }.call
end

Instance Method Details

#exec(*args) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/redis/client.rb', line 99

def exec *args
  redis_exec = method_missing :exec, *args
  callback_multi = @multi
  @multi = nil
  redis_exec.callback do |results|
    if results
      normalized_results = []
      callback_multi.each do |command|
        if Exception === command
          normalized_results << command
        else
          result = results.shift
          normalized_results << result
          if Exception === result
            command.fail result
          else
            command.succeed result
          end
        end
      end
      redis_exec.succeed normalized_results
    end
  end
end

#in_multi?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/redis/client.rb', line 90

def in_multi?
  !!@multi
end

#multi(*args) ⇒ Object



94
95
96
97
# File 'lib/redis/client.rb', line 94

def multi *args
  @multi ||= []
  method_missing :multi, *args
end

#on_pubsub(&block) ⇒ Object

This is simple and fast but doesn’t test for programming errors. Don’t send non-pubsub commands while subscribed and you’re fine. Subclass Client and/or create a defensive layer if you need to.



42
43
44
# File 'lib/redis/client.rb', line 42

def on_pubsub &block
  @pubsub_callback = block
end

#receive_data(data) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/redis/client.rb', line 46

def receive_data data
  @reader.feed data
  until (
    begin
      data = @reader.gets
    rescue Exception => e
      raise e if Interrupt === e
      @queue.shift.fail e unless @queue.empty?
      close_connection
      data = false
    end
  ) == false
    deferrable = @queue.shift
    if deferrable
      if Exception === data
        deferrable.fail data
      else
        deferrable.succeed data
      end
    else
      @pubsub_callback.call data
    end
  end
end

#synchronyObject



44
45
46
47
# File 'lib/redis/synchrony.rb', line 44

def synchrony
  raise 'you probably want Redis.synchrony instead' if block_given?
  @synchrony ||= Synchrony.new self
end

#unbindObject



33
34
35
36
37
# File 'lib/redis/client.rb', line 33

def unbind
  until @queue.empty?
    @queue.shift.fail RuntimeError.new 'connection closed'
  end
end