Sha256: 588c0ca44637edca97c925addcc342bc0ddcea215d0f6a78619cf42ea6e9c5e3

Contents?: true

Size: 915 Bytes

Versions: 1

Compression:

Stored size: 915 Bytes

Contents

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

require 'contrast/components/logger'

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
contrast-agent-4.11.0 lib/contrast/utils/lru_cache.rb