Sha256: ffef7746a0e58ecf77b12766a1080cabd1334cf39e9ea5e7529d80d4cf37a755

Contents?: true

Size: 1.03 KB

Versions: 12

Compression:

Stored size: 1.03 KB

Contents

require 'time'
require "lru_redux"

module LogStash
  module Filters
    module Empow
      class ClassifierCache
        include LogStash::Util::Loggable

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

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

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

        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

        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
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
logstash-filter-threats_classifier-1.0.4 lib/logstash/filters/classifier-cache.rb
logstash-filter-empowclassifier-1.0.2 lib/logstash/filters/classifier-cache.rb
logstash-filter-empowclassifier-1.0.1 lib/logstash/filters/classifier-cache.rb
logstash-filter-empowclassifier-1.0.0 lib/logstash/filters/classifier-cache.rb
logstash-filter-empowclassifier-0.3.23 lib/logstash/filters/classifier-cache.rb
logstash-filter-empowclassifier-0.3.21 lib/logstash/filters/classifier-cache.rb
logstash-filter-empowclassifier-0.3.20 lib/logstash/filters/classifier-cache.rb
logstash-filter-empowclassifier-0.3.19 lib/logstash/filters/classifier-cache.rb
logstash-filter-empowclassifier-0.3.18 lib/logstash/filters/classifier-cache.rb
logstash-filter-empowclassifier-0.3.17 lib/logstash/filters/classifier-cache.rb
logstash-filter-empowclassifier-0.3.16 lib/logstash/filters/classifier-cache.rb
logstash-filter-empowclassifier-0.3.15 lib/logstash/filters/classifier-cache.rb