Class: Redis::Client
- Inherits:
-
EventMachine::Connection
- Object
- EventMachine::Connection
- Redis::Client
- Includes:
- Sender
- Defined in:
- lib/redis/client.rb,
lib/redis/synchrony.rb
Defined Under Namespace
Classes: Command
Class Method Summary collapse
-
.transforms ⇒ Object
Some data is best transformed into a Ruby type.
Instance Method Summary collapse
- #close ⇒ Object
- #connection_completed ⇒ Object
- #exec(*args, &block) ⇒ Object
- #in_multi? ⇒ Boolean
-
#initialize(*ignore_args) ⇒ Client
constructor
A new instance of Client.
- #method_missing(method, *args, &block) ⇒ Object
- #multi(*args, &block) ⇒ Object
-
#on_pubsub(&block) ⇒ Object
This is simple and fast but doesn’t test for programming errors.
- #receive_data(data) ⇒ Object
- #synchrony ⇒ Object
- #unbind ⇒ Object
Methods included from Sender
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
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/redis/client.rb', line 86 def method_missing method, *args, &block if @multi for_queue = new_command true, false, method, *args command = new_command false, true, method, &block 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, &block end command end |
Class Method Details
.transforms ⇒ Object
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
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/redis/client.rb', line 145 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
#close ⇒ Object
38 39 40 41 |
# File 'lib/redis/client.rb', line 38 def close @closing_connection = true close_connection_after_writing end |
#connection_completed ⇒ Object
33 34 35 36 |
# File 'lib/redis/client.rb', line 33 def connection_completed @connected = true @port, @host = Socket.unpack_sockaddr_in(get_peername) end |
#exec(*args, &block) ⇒ Object
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 |
# File 'lib/redis/client.rb', line 113 def exec *args, &block redis_exec = new_command true, true, :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 redis_exec.callback &block if block_given? redis_exec end |
#in_multi? ⇒ Boolean
104 105 106 |
# File 'lib/redis/client.rb', line 104 def in_multi? !!@multi end |
#multi(*args, &block) ⇒ Object
108 109 110 111 |
# File 'lib/redis/client.rb', line 108 def multi *args, &block @multi ||= [] new_command true, true, :multi, *args, &block 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.
58 59 60 |
# File 'lib/redis/client.rb', line 58 def on_pubsub &block @pubsub_callback = block end |
#receive_data(data) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/redis/client.rb', line 62 def receive_data data @reader.feed data until ( begin data = @reader.gets rescue StandardError => 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 |
#synchrony ⇒ Object
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 |
#unbind ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/redis/client.rb', line 43 def unbind unless !@connected || @closing_connection EM.add_timer(1) do reconnect(@host, @port) end else until @queue.empty? @queue.shift.fail RuntimeError.new 'connection closed' end end end |