Class: Emb::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/emb/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pool: DEFAULTS[:pool], **redis_options) ⇒ Client

Returns a new instance of Client.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/emb/client.rb', line 12

def initialize(pool: DEFAULTS[:pool], **redis_options)
  url = extract_url!(redis_options)
  redis_options[:host] ||= DEFAULTS[:host] unless url
  redis_options[:port] ||= DEFAULTS[:port] unless url
  redis_options[:protocol] ||= 2
  redis_options[:reconnect_attempts] ||= 3

  @pool = ConnectionPool.new(size: pool) do
    RedisClient.new(url: url, **redis_options)
  end

  @registry = {}
end

Instance Attribute Details

#poolObject (readonly)

Returns the value of attribute pool.



10
11
12
# File 'lib/emb/client.rb', line 10

def pool
  @pool
end

Instance Method Details

#[](name) ⇒ Object



38
39
40
# File 'lib/emb/client.rb', line 38

def [](name)
  @registry[name] ||= Proxy.new(self, name.to_sym)
end

#helpObject



62
# File 'lib/emb/client.rb', line 62

def help = send_command('EMB.HELP')

#info(name) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/emb/client.rb', line 51

def info(name)
  raw = send_command('EMB.INFO', name.to_s)
  return {} if raw.nil?

  raw
    .each_slice(2)
    .to_h { |k, v| [k.to_sym, v] }
end

#modelsObject



42
43
44
45
46
47
48
49
# File 'lib/emb/client.rb', line 42

def models
  raw = send_command('EMB.MODELS')
  return [] if raw.nil?

  raw.map do |name, dim, status|
    { name: name, dim: dim.to_i, status: status }
  end
end

#multi {|mp| ... } ⇒ Object

Yields:

  • (mp)


86
87
88
89
90
# File 'lib/emb/client.rb', line 86

def multi(&)
  mp = MultiProxy.new(self)
  yield mp
  mp.run
end

#pingObject



64
# File 'lib/emb/client.rb', line 64

def ping = send_command('PING')

#readyObject



66
67
68
69
70
71
72
# File 'lib/emb/client.rb', line 66

def ready
  send_command('EMB.READY')

  "ready"
rescue RedisClient::CommandError => e
  e.message
end

#ready?Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
80
# File 'lib/emb/client.rb', line 74

def ready?
  ready

  true
rescue RedisClient::CommandError
  false
end

#reset_registry!Object



82
83
84
# File 'lib/emb/client.rb', line 82

def reset_registry!
  @registry = {}
end

#send_command(*args) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/emb/client.rb', line 26

def send_command(*args)
  return @pool.with { |r| r.call(*args) } unless Emb.debug?

  start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  result = @pool.with { |r| r.call(*args) }
  elapsed = (Process.clock_gettime(Process::CLOCK_MONOTONIC) - start) * 1000

  $stdout.puts "[EMB] #{args.map(&:inspect).join(' ')} (#{format('%.2f', elapsed)}ms)"

  result
end

#statsObject



60
# File 'lib/emb/client.rb', line 60

def stats = send_command('EMB.STATS')