Sha256: 12294c436681e16b459baf11c1347fc0617b012ba10dec60505e0a4cd0bd7275

Contents?: true

Size: 1.32 KB

Versions: 28

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

# Copyright The OpenTelemetry Authors
#
# SPDX-License-Identifier: Apache-2.0

module OpenTelemetry
  module Instrumentation
    module PG
      # A simple LRU cache for the postgres instrumentation.
      class LruCache
        # Rather than take a dependency on another gem, we implement a very, very basic
        # LRU cache here. We can take advantage of the fact that Ruby hashes are ordered
        # to always keep the recently-accessed keys at the top.
        def initialize(size)
          raise ArgumentError, 'Invalid size' if size < 1

          @limit = size
          @store = {}
        end

        def [](key)
          # We need to check for the key explicitly, because `nil` is a valid hash value.
          return unless @store.key?(key)

          # Since the cache contains the item, we delete and re-insert into the hash.
          # This guarantees that hash keys are ordered by access recency.
          value = @store.delete(key)
          @store[key] = value

          value
        end

        def []=(key, value)
          # We remove the value if it's already present, so that the hash keys remain ordered
          # by access recency.
          @store.delete(key)
          @store[key] = value
          @store.shift if @store.length > @limit
        end
      end
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
opentelemetry-instrumentation-pg-0.21.0 lib/opentelemetry/instrumentation/pg/lru_cache.rb
opentelemetry-instrumentation-pg-0.20.0 lib/opentelemetry/instrumentation/pg/lru_cache.rb
opentelemetry-instrumentation-pg-0.19.2 lib/opentelemetry/instrumentation/pg/lru_cache.rb
opentelemetry-instrumentation-pg-0.19.1 lib/opentelemetry/instrumentation/pg/lru_cache.rb
opentelemetry-instrumentation-pg-0.19.0 lib/opentelemetry/instrumentation/pg/lru_cache.rb
opentelemetry-instrumentation-pg-0.18.1 lib/opentelemetry/instrumentation/pg/lru_cache.rb
opentelemetry-instrumentation-pg-0.18.0 lib/opentelemetry/instrumentation/pg/lru_cache.rb
opentelemetry-instrumentation-pg-0.17.1 lib/opentelemetry/instrumentation/pg/lru_cache.rb