Class: EM::Tycoon::Client

Inherits:
Connection
  • Object
show all
Defined in:
lib/em/tycoon.rb

Overview

Kyoto Tycoon binary protocol handler

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



29
30
31
# File 'lib/em/tycoon.rb', line 29

def initialize
  super
end

Instance Method Details

#get(*keys, &cb) ⇒ Object

Use KT binary protocol “get_bulk” command to get values for the given keys The callback will be called upon completion and passed a hash with the returned values and expirations (see EM::Tycoon::Client#set), or nil on error

Raises:

  • (ArgumentError)


81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/em/tycoon.rb', line 81

def get(*keys,&cb)
  raise ArgumentError.new("No block given") unless block_given?
  msg = Protocol::Message.generate(:get, keys)
  job = Protocol::Parser.new(REQUEST_TIMEOUT)
  job.callback { |result|
    cb.call(result) if block_given?
  }
  job.errback { |result|
    cb.call(nil) if block_given?
  }
  @jobs << job
  send_data(msg)
end

#play_script(script_name, args = {}, &cb) ⇒ Object

Use KT binary protocol “play_script” command to execute the script function passed in the script_name argument, with the arguments passed in args The callback will be called upon completion and passed a hash containing the key/value pairs returned by the script (see EM::Tycoon::Client#set), or nil on error



116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/em/tycoon.rb', line 116

def play_script(script_name, args={}, &cb)
  msg = Protocol::Message.generate(:play_script, [script_name,args])
  if block_given?
    job = Protocol::Parser.new(REQUEST_TIMEOUT)
    job.callback { |result|
      cb.call(result) if block_given?
    }
    job.errback { |result|
      cb.call(nil) if block_given?
    }
    @jobs << job
    send_data(msg)
  end
end

#post_initObject



33
34
35
# File 'lib/em/tycoon.rb', line 33

def post_init
  @jobs = []
end

#receive_data(data) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/em/tycoon.rb', line 37

def receive_data(data)
  bytes_parsed = 0
  while @jobs.any? && (bytes_parsed < data.bytesize)
    bytes_parsed += @jobs.first.parse_chunk(data[bytes_parsed..-1])
    @jobs.shift if @jobs.first.message.parsed?
  end
end

#remove(keys = [], &cb) ⇒ Object

Use KT binary protocol “remove_bulk” command to remove the given keys The callback will be called upon completion and passed the number of keys deleted, or nil on error



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/em/tycoon.rb', line 97

def remove(keys=[],&cb)
  msg = Protocol::Message.generate(:remove, keys)
  if block_given?
    job = Protocol::Parser.new(REQUEST_TIMEOUT)
    job.callback { |result|
      cb.call(result) if block_given?
    }
    job.errback { |result|
      cb.call(nil) if block_given?
    }
    @jobs << job
    send_data(msg)
  end
end

#set(data = {}, &cb) ⇒ Object

Use KT binary protocol “set_bulk” command to set keys and values passed in the data argument, with the option to pass an optional expiration time by specifying it in the value, in the following format:

"my_key1"          => "my value for my_key1",
"my_key2"          => "my value for my_key2",
"my_key3_with_60s_xt"  => {:value => "my value for my_key3_with_60s_xt", :xt => 60

}

Expiration times can be specified either as an integer representing the number of seconds the key should persist after it is created, or as a Time object containing the absolute time at which the key should expire.

On completion, the callback block will be called (if provided) with the number of records stored as returned by Kyoto Tycoon, or nil on error. If no callback is specified, the no-reply option will be passed to Kyoto Tycoon.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/em/tycoon.rb', line 63

def set(data={},&cb)
  msg = Protocol::Message.generate(:set, data, {:no_reply => !(block_given?)})
  if block_given?
    job = Protocol::Parser.new(REQUEST_TIMEOUT)
    job.callback { |result|
      cb.call(result) if block_given?
    }
    job.errback { |result|
      cb.call(nil) if block_given?
    }
    @jobs << job
    send_data(msg)
  end
end

#unbindObject



45
46
# File 'lib/em/tycoon.rb', line 45

def unbind
end