Sha256: 84a077253c25d2d43ca0e7a95e99f4ca07800cf6fad677febfa0ea8e742eee8f

Contents?: true

Size: 707 Bytes

Versions: 1

Compression:

Stored size: 707 Bytes

Contents

# frozen_string_literal: true

require 'ddtelemetry'

class Cache
  attr_reader :counter

  def initialize
    @map = {}
    @counter = DDTelemetry::Counter.new
  end

  def []=(key, value)
    @counter.increment(type: :set)

    @map[key] = value
  end

  def [](key)
    if @map.key?(key)
      @counter.increment(type: :get_hit)
    else
      @counter.increment(type: :get_miss)
    end

    @map[key]
  end
end

cache = Cache.new

cache['greeting']
cache['greeting']
cache['greeting'] = 'Hi there!'
cache['greeting']
cache['greeting']
cache['greeting']

p cache.counter.get(type: :set)
# => 1

p cache.counter.get(type: :get_hit)
# => 3

p cache.counter.get(type: :get_miss)
# => 2

puts cache.counter

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ddtelemetry-1.0.0a3 samples/cache.rb