Class: Tair::Client
- Inherits:
-
Object
- Object
- Tair::Client
- Defined in:
- lib/tair/client.rb
Constant Summary collapse
- WRITE_OPERATIONS =
[:put, :incr, :set_count, :delete]
Instance Attribute Summary collapse
-
#data_servers ⇒ Object
writeonly
Sets the attribute data_servers.
-
#group ⇒ Object
readonly
Returns the value of attribute group.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#namespace ⇒ Object
Returns the value of attribute namespace.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Class Method Summary collapse
Instance Method Summary collapse
- #init_cluster(retries: 5) ⇒ Object
-
#initialize(host, port, group: nil, namespace: 0xab) ⇒ Client
constructor
A new instance of Client.
- #operate(op, request, key: nil, &block) ⇒ Object
-
#watch_cluster_config(interval = 120) ⇒ Object
配置有变化时,需要更新到客户端.
- #watch_cluster_config!(interval = 120) ⇒ Object
Methods included from Log
#colorize, included, #log_bytes, #log_time, #logger, #logger_colorize, #tair_bytes_log_file
Methods included from Operation
#count, #decr, #delete, #fetch_data_servers, #get, #incr, #put, #valid_count_step?
Methods included from Key
Constructor Details
#initialize(host, port, group: nil, namespace: 0xab) ⇒ Client
Returns a new instance of Client.
31 32 33 |
# File 'lib/tair/client.rb', line 31 def initialize(host, port, group: nil, namespace: 0xab) @host, @port, @group, @namespace = host, port, group, namespace end |
Instance Attribute Details
#data_servers=(value) ⇒ Object (writeonly)
Sets the attribute data_servers
29 30 31 |
# File 'lib/tair/client.rb', line 29 def data_servers=(value) @data_servers = value end |
#group ⇒ Object (readonly)
Returns the value of attribute group.
28 29 30 |
# File 'lib/tair/client.rb', line 28 def group @group end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
28 29 30 |
# File 'lib/tair/client.rb', line 28 def host @host end |
#namespace ⇒ Object
Returns the value of attribute namespace.
16 17 18 |
# File 'lib/tair/client.rb', line 16 def namespace @namespace end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
28 29 30 |
# File 'lib/tair/client.rb', line 28 def port @port end |
Class Method Details
.create(host, port, opts = {}, &block) ⇒ Object
21 22 23 24 25 |
# File 'lib/tair/client.rb', line 21 def self.create(host, port, opts={}, &block) new(host, port, opts).tap do |client| block && block.call(client) end end |
Instance Method Details
#init_cluster(retries: 5) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/tair/client.rb', line 62 def init_cluster(retries: 5) count = retries begin cluster rescue => e error { "init cluster error: #{e}" } count -= 1 if count >= 0 retry else raise end end end |
#operate(op, request, key: nil, &block) ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'lib/tair/client.rb', line 52 def operate(op, request, key: nil, &block) request.namespace = namespace will_write_data = WRITE_OPERATIONS.include? op conn = data_conn(key, will_write_data: will_write_data) log_time "#{op} #{key.inspect} on #{conn.host}" do conn.operate(request, &block) end end |
#watch_cluster_config(interval = 120) ⇒ Object
配置有变化时,需要更新到客户端
37 38 39 |
# File 'lib/tair/client.rb', line 37 def watch_cluster_config(interval=120) Thread.new { watch_cluster_config!(interval) } end |
#watch_cluster_config!(interval = 120) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/tair/client.rb', line 42 def watch_cluster_config!(interval=120) while true sleep interval @cluster = fetch_data_servers(group) end end |