Class: LogStash::Filters::Empow::ClassifierCache

Inherits:
Object
  • Object
show all
Includes:
Util::Loggable
Defined in:
lib/logstash/filters/classifier-cache.rb

Instance Method Summary collapse

Constructor Details

#initialize(cache_size, ttl) ⇒ ClassifierCache

Returns a new instance of ClassifierCache.



10
11
12
13
14
15
16
# File 'lib/logstash/filters/classifier-cache.rb', line 10

def initialize(cache_size, ttl)
  @logger ||= self.logger

  @logger.debug("cache size #{cache_size}")

  @lru_cache ||= LruRedux::TTL::ThreadSafeCache.new(cache_size, ttl)
end

Instance Method Details

#classify(key) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/logstash/filters/classifier-cache.rb', line 18

def classify(key)
  return nil if key.nil?

  tuple = @lru_cache[key]

  return nil if tuple.nil?

  expiration_time = tuple[:expiration_time]

  if Time.now > expiration_time
    @lru_cache.evict(key)
    return nil
  end

  res = tuple[:val]

  return res
end

#put(key, val, expiration_time) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/logstash/filters/classifier-cache.rb', line 37

def put(key, val, expiration_time)
  return if key.nil?

  @logger.debug("caching new entry", :key => key, :val => val)

  tuple = {}
  tuple[:val] = val
  tuple[:expiration_time] = expiration_time

  @lru_cache[key] = tuple
end