Sha256: 77ea698da4aa028f6f3adfdab960dfab5db9ba4a0b7526883064325c9a0251f4

Contents?: true

Size: 925 Bytes

Versions: 5

Compression:

Stored size: 925 Bytes

Contents

# Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
# frozen_string_literal: true

module Contrast
  module Utils
    # A LRU(Least Recently Used) Cache store.
    class LRUCache
      def initialize capacity = 500
        raise StandardError 'Capacity must be bigger than 0' if capacity <= 0

        @capacity = capacity
        @cache = {}
      end

      def [] key
        val = @cache.delete(key)
        @cache[key] = val if val
        val
      end

      def []= key, value
        @cache.delete(key)
        @cache[key] = value
        @cache.shift if @cache.size > @capacity
        value # rubocop:disable Lint/Void
      end

      def keys
        @cache.keys
      end

      def key? key
        @cache.key?(key)
      end

      def values
        @cache.values
      end

      def clear
        @cache.clear
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
contrast-agent-4.14.1 lib/contrast/utils/lru_cache.rb
contrast-agent-4.14.0 lib/contrast/utils/lru_cache.rb
contrast-agent-4.13.1 lib/contrast/utils/lru_cache.rb
contrast-agent-4.13.0 lib/contrast/utils/lru_cache.rb
contrast-agent-4.12.0 lib/contrast/utils/lru_cache.rb