Sha256: 82ce8746337ce57fd2a969aedf36fb2b288e917b851a4c174a4ee79e7757835e

Contents?: true

Size: 462 Bytes

Versions: 27

Compression:

Stored size: 462 Bytes

Contents

# frozen_string_literal: true

module Logster
  class Cache
    def initialize(age = 2)
      @age = age
      @hash = {}
    end

    def fetch(key)
      if !@hash.key?(key) || @hash[key][:created_at] + @age < Process.clock_gettime(Process::CLOCK_MONOTONIC)
        @hash[key] = { data: yield, created_at: Process.clock_gettime(Process::CLOCK_MONOTONIC) }
      end
      @hash[key][:data]
    end

    def clear(key)
      @hash.delete(key)
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
logster-2.12.2 lib/logster/cache.rb
logster-2.12.1 lib/logster/cache.rb
logster-2.11.4 lib/logster/cache.rb
logster-2.11.3 lib/logster/cache.rb
logster-2.11.2 lib/logster/cache.rb
logster-2.11.1 lib/logster/cache.rb
logster-2.11.0 lib/logster/cache.rb
logster-2.10.1 lib/logster/cache.rb
logster-2.10.0 lib/logster/cache.rb
logster-2.9.8 lib/logster/cache.rb
logster-2.9.7 lib/logster/cache.rb
logster-2.9.6 lib/logster/cache.rb
logster-2.9.5 lib/logster/cache.rb
logster-2.9.4 lib/logster/cache.rb
logster-2.9.3 lib/logster/cache.rb
logster-2.9.2 lib/logster/cache.rb
logster-2.9.1 lib/logster/cache.rb
logster-2.9.0 lib/logster/cache.rb
logster-2.8.0 lib/logster/cache.rb
logster-2.7.1 lib/logster/cache.rb