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: nil, batch: nil, **redis_options) ⇒ Client

Returns a new instance of Client.



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

def initialize(pool: nil, batch: nil, **redis_options)
  cfg = Emb.configuration
  @batch_enabled = batch.nil? ? cfg.batch : batch
  size = pool.nil? ? cfg.pool : pool
  url = extract_url!(redis_options, cfg)
  redis_options = merged_redis_options(redis_options, cfg, url)

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

  @registry = {}
end

Instance Attribute Details

#poolObject (readonly)

Returns the value of attribute pool.



8
9
10
# File 'lib/emb/client.rb', line 8

def pool
  @pool
end

Instance Method Details

#[](name) ⇒ Object



36
37
38
# File 'lib/emb/client.rb', line 36

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

#batchObject



40
41
42
# File 'lib/emb/client.rb', line 40

def batch
  @batch ||= BatchProxy.new(self)
end

#batch?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/emb/client.rb', line 44

def batch?
  @batch_enabled
end

#helpObject



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

def help = send_command('EMB.HELP')

#info(name) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/emb/client.rb', line 57

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



48
49
50
51
52
53
54
55
# File 'lib/emb/client.rb', line 48

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)


92
93
94
95
96
# File 'lib/emb/client.rb', line 92

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

#pingObject



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

def ping = send_command('PING')

#readyObject



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

def ready
  send_command('EMB.READY')

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

#ready?Boolean

Returns:

  • (Boolean)


80
81
82
83
84
85
86
# File 'lib/emb/client.rb', line 80

def ready?
  ready

  true
rescue RedisClient::CommandError
  false
end

#reset_registry!Object



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

def reset_registry!
  @registry = {}
end

#send_command(*args) ⇒ Object



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

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



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

def stats = send_command('EMB.STATS')