Class: Fluent::Plugin::LdapClient::CacheTTL

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/plugin/ldap_client/cache_ttl.rb

Constant Summary collapse

CACHE_SIZE =
1000
TTL_POSITIVE =
14 * 3600
TTL_NEGATIVE =
3600

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size: CACHE_SIZE, ttl_positive: TTL_POSITIVE, ttl_negative: TTL_NEGATIVE) ⇒ CacheTTL

Returns a new instance of CacheTTL.



13
14
15
16
17
18
19
20
# File 'lib/fluent/plugin/ldap_client/cache_ttl.rb', line 13

def initialize(size: CACHE_SIZE, ttl_positive: TTL_POSITIVE, ttl_negative: TTL_NEGATIVE)
  @size = size
  @ttl_positive = ttl_positive
  @ttl_negative = ttl_negative

  @cache = {}
  @access_order = []
end

Instance Attribute Details

#sizeObject (readonly)

Returns the value of attribute size.



11
12
13
# File 'lib/fluent/plugin/ldap_client/cache_ttl.rb', line 11

def size
  @size
end

#ttl_negativeObject (readonly)

Returns the value of attribute ttl_negative.



11
12
13
# File 'lib/fluent/plugin/ldap_client/cache_ttl.rb', line 11

def ttl_negative
  @ttl_negative
end

#ttl_positiveObject (readonly)

Returns the value of attribute ttl_positive.



11
12
13
# File 'lib/fluent/plugin/ldap_client/cache_ttl.rb', line 11

def ttl_positive
  @ttl_positive
end

Instance Method Details

#get(key) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fluent/plugin/ldap_client/cache_ttl.rb', line 22

def get(key)
  cleanup_expired_entries

  if @cache.key?(key) && !expired?(@cache[key])
    refresh_access_order(key)
    return @cache[key][:value]
  end

  result = yield key
  store(key, result)
  result
end