Sha256: 8b4391920dafe56c80a401628846deac65678c9bf5d35072bea7d49312cd8b8c

Contents?: true

Size: 1.07 KB

Versions: 18

Compression:

Stored size: 1.07 KB

Contents

# Copyright (c) 2023 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
      # Initializes new Least Recently Used Cache Store
      #
      # @raise [StandardError] raises error if provided capacity is invalid or less or equal to zero
      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

18 entries across 18 versions & 1 rubygems

Version Path
contrast-agent-7.6.1 lib/contrast/utils/lru_cache.rb
contrast-agent-7.6.0 lib/contrast/utils/lru_cache.rb
contrast-agent-7.5.0 lib/contrast/utils/lru_cache.rb
contrast-agent-7.4.1 lib/contrast/utils/lru_cache.rb
contrast-agent-7.4.0 lib/contrast/utils/lru_cache.rb
contrast-agent-7.3.2 lib/contrast/utils/lru_cache.rb
contrast-agent-7.3.1 lib/contrast/utils/lru_cache.rb
contrast-agent-7.3.0 lib/contrast/utils/lru_cache.rb
contrast-agent-7.2.0 lib/contrast/utils/lru_cache.rb
contrast-agent-7.1.0 lib/contrast/utils/lru_cache.rb
contrast-agent-7.0.0 lib/contrast/utils/lru_cache.rb
contrast-agent-6.15.3 lib/contrast/utils/lru_cache.rb
contrast-agent-6.15.2 lib/contrast/utils/lru_cache.rb
contrast-agent-6.15.1 lib/contrast/utils/lru_cache.rb
contrast-agent-6.15.0 lib/contrast/utils/lru_cache.rb
contrast-agent-6.14.0 lib/contrast/utils/lru_cache.rb
contrast-agent-6.13.0 lib/contrast/utils/lru_cache.rb
contrast-agent-6.12.0 lib/contrast/utils/lru_cache.rb