Class: Emb::Client
Instance Attribute Summary collapse
-
#pool ⇒ Object
readonly
Returns the value of attribute pool.
Instance Method Summary collapse
- #[](name) ⇒ Object
- #batch ⇒ Object
- #batch? ⇒ Boolean
-
#config ⇒ Object
Live view of the server's runtime configuration (CONFIG GET/SET).
- #help ⇒ Object
- #info(name) ⇒ Object
-
#initialize(pool: nil, batch: nil, **redis_options) ⇒ Client
constructor
A new instance of Client.
- #models ⇒ Object
- #multi {|mp| ... } ⇒ Object
- #ping ⇒ Object
- #ready ⇒ Object
- #ready? ⇒ Boolean
- #reset_registry! ⇒ Object
- #send_command(*args) ⇒ Object
Methods included from Commands
Constructor Details
#initialize(pool: nil, batch: nil, **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: nil, batch: nil, **) cfg = Emb.configuration @batch_enabled = batch.nil? ? cfg.batch : batch size = pool.nil? ? cfg.pool : pool url = extract_url!(, cfg) = (, cfg, url) @pool = ConnectionPool.new(size: size) do RedisClient.new(url: url, **) end @registry = {} end |
Instance Attribute Details
#pool ⇒ Object (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 |
#batch ⇒ Object
42 43 44 |
# File 'lib/emb/client.rb', line 42 def batch @batch ||= BatchProxy.new(self) end |
#batch? ⇒ Boolean
46 47 48 |
# File 'lib/emb/client.rb', line 46 def batch? @batch_enabled end |
#config ⇒ Object
Live view of the server's runtime configuration (CONFIG GET/SET).
51 52 53 |
# File 'lib/emb/client.rb', line 51 def config @config ||= RuntimeConfig.new(self) end |
#help ⇒ Object
73 |
# File 'lib/emb/client.rb', line 73 def help = send_command('EMB.HELP') |
#info(name) ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/emb/client.rb', line 64 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 |
#models ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/emb/client.rb', line 55 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
97 98 99 100 101 |
# File 'lib/emb/client.rb', line 97 def multi(&) mp = MultiProxy.new(self) yield mp mp.run end |
#ping ⇒ Object
75 |
# File 'lib/emb/client.rb', line 75 def ping = send_command('PING') |
#ready ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/emb/client.rb', line 77 def ready send_command('EMB.READY') 'ready' rescue RedisClient::CommandError => e e. end |
#ready? ⇒ Boolean
85 86 87 88 89 90 91 |
# File 'lib/emb/client.rb', line 85 def ready? ready true rescue RedisClient::CommandError false end |
#reset_registry! ⇒ Object
93 94 95 |
# File 'lib/emb/client.rb', line 93 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 |