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_size = nil, pool_opts = {}) ⇒ Client

Returns a new instance of Client.



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

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

Instance Attribute Details

#call_cacheObject (readonly)

Returns the value of attribute call_cache.



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

def call_cache
  @call_cache
end

#connection_classObject

Returns the value of attribute connection_class.



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

def connection_class
  @connection_class
end

Instance Method Details

#cached_call(*data) ⇒ Object



39
40
41
# File 'lib/fastbeans/client.rb', line 39

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

#call(*data) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/fastbeans/client.rb', line 31

def call(*data)
  Fastbeans.benchmark("Calling: #{data.inspect}") do
    pool.with do |conn|
      conn.call(*data)
    end
  end
end

#clear_call_cache!Object



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

def clear_call_cache!
  @call_cache.clear
end

#poolObject



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

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