Class: Fastbeans::Client

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

Constant Summary collapse

CALL_CACHE_SIZE =
100

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = '127.0.0.1', port = 12345, cache = nil, pool_opts = {}) ⇒ Client

Returns a new instance of Client.



15
16
17
18
19
20
# File 'lib/fastbeans/client.rb', line 15

def initialize(host='127.0.0.1', port=12345, cache=nil, pool_opts={})
  @host, @port = host, port
  @call_cache = cache || Rufus::Lru::SynchronizedHash.new(CALL_CACHE_SIZE)
  @pool_opts =  {:size => 5, :timeout => 5}.update(pool_opts)
  @connection_class = Fastbeans::Connection
end

Instance Attribute Details

#cache_classObject

Returns the value of attribute cache_class.



13
14
15
# File 'lib/fastbeans/client.rb', line 13

def cache_class
  @cache_class
end

#call_cacheObject (readonly)

Returns the value of attribute call_cache.



11
12
13
# File 'lib/fastbeans/client.rb', line 11

def call_cache
  @call_cache
end

#connection_classObject

Returns the value of attribute connection_class.



12
13
14
# File 'lib/fastbeans/client.rb', line 12

def connection_class
  @connection_class
end

Instance Method Details

#cached_call(*data) ⇒ Object



45
46
47
# File 'lib/fastbeans/client.rb', line 45

def cached_call(*data)
  @call_cache[data] ||= call(*data)
end

#call(*data) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/fastbeans/client.rb', line 32

def call(*data)
  Fastbeans.benchmark("Calling: #{data.first.inspect}") do
    pool.with do |conn|
      if data.last.is_a?(Hash) and (data.last.keys.to_set & Fastbeans::Request::OPTION_KEYS).size > 0
        opts = data.pop
      else
        opts = {}
      end
      conn.call(data, opts)
    end
  end
end

#clear_call_cache!Object



28
29
30
# File 'lib/fastbeans/client.rb', line 28

def clear_call_cache!
  @call_cache.clear
end

#poolObject



22
23
24
25
26
# File 'lib/fastbeans/client.rb', line 22

def pool
  @pool ||= ConnectionPool.new(@pool_opts) do
    @connection_class.new(@host, @port)
  end
end