Class: Neuron::Client::MembaseConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/neuron-client/membase_connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(servers, opts = {}) ⇒ MembaseConnection

Returns a new instance of MembaseConnection.



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

def initialize(servers, opts={})
  @client = Dalli::Client.new(servers)
  max_items   = opts[:local_cache_max_items]    || 10_000
  ttl         = opts[:local_cache_ttl]          || 15.minutes
  soft_ttl    = [opts[:local_cache_soft_ttl]    || 1.minute, ttl].min
  retry_delay = [opts[:local_cache_retry_delay] || 1.second, soft_ttl].min
  @local_cache = LRUCache.new(
    :max_items => max_items,
    :ttl => ttl,
    :soft_ttl => soft_ttl,
    :retry_delay => retry_delay)
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



8
9
10
# File 'lib/neuron-client/membase_connection.rb', line 8

def client
  @client
end

#local_cacheObject (readonly)

Returns the value of attribute local_cache.



8
9
10
# File 'lib/neuron-client/membase_connection.rb', line 8

def local_cache
  @local_cache
end

Instance Method Details

#fetch(key, ttl = nil, options = nil, &callback) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/neuron-client/membase_connection.rb', line 35

def fetch(key, ttl=nil, options=nil, &callback)
  ttl = local_ttl(ttl)
  soft_ttl =[@local_cache.soft_ttl, ttl].compact.min
  retry_delay = [@local_cache.retry_delay, soft_ttl].compact.min
  @local_cache.fetch(key,
                     :ttl => ttl,
                     :soft_ttl => soft_ttl,
                     :retry_delay => retry_delay) do
    @client.fetch(key, ttl, options, &callback)
  end
end

#get(key, ttl = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/neuron-client/membase_connection.rb', line 23

def get(key, ttl=nil)
  ttl = local_ttl(ttl)
  soft_ttl =[@local_cache.soft_ttl, ttl].compact.min
  retry_delay = [@local_cache.retry_delay, soft_ttl].compact.min
  @local_cache.fetch(key,
                     :ttl => ttl,
                     :soft_ttl => soft_ttl,
                     :retry_delay => retry_delay) do
    @client.get(key)
  end
end