Sha256: 5c03d3aeac8529d9bc5a8b02f09380c2125c622b07b063c7b69381c9eb3f6500

Contents?: true

Size: 780 Bytes

Versions: 20

Compression:

Stored size: 780 Bytes

Contents

# Based on code by Sam Saffron: https://stackoverflow.com/a/16161783/181916
module Skylight
  module Util
    class LruCache
      def initialize(max_size)
        @max_size = max_size
        @data = {}
      end

      def max_size=(size)
        raise ArgumentError, :max_size if @max_size < 1

        @max_size = size
        @data.shift while @data.size > @max_size
      end

      # Individual hash operations here are atomic in MRI.
      def fetch(key)
        found = true
        value = @data.delete(key) { found = false }

        value = yield if !found && block_given?

        @data[key] = value if value

        @data.shift if !found && value && @data.length > @max_size

        value
      end

      def clear
        @data.clear
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
skylight-6.1.0.beta lib/skylight/util/lru_cache.rb
skylight-6.0.4 lib/skylight/util/lru_cache.rb
skylight-6.0.3 lib/skylight/util/lru_cache.rb
skylight-5.3.5 lib/skylight/util/lru_cache.rb
skylight-6.0.2 lib/skylight/util/lru_cache.rb
skylight-6.0.1 lib/skylight/util/lru_cache.rb
skylight-6.0.0 lib/skylight/util/lru_cache.rb
skylight-6.0.0.beta2 lib/skylight/util/lru_cache.rb
skylight-6.0.0.beta lib/skylight/util/lru_cache.rb
skylight-5.3.4 lib/skylight/util/lru_cache.rb
skylight-5.3.3 lib/skylight/util/lru_cache.rb
skylight-5.3.2 lib/skylight/util/lru_cache.rb
skylight-5.3.1 lib/skylight/util/lru_cache.rb
skylight-5.3.0 lib/skylight/util/lru_cache.rb
skylight-5.2.0 lib/skylight/util/lru_cache.rb
skylight-5.2.0.beta2 lib/skylight/util/lru_cache.rb
skylight-5.2.0.beta lib/skylight/util/lru_cache.rb
skylight-5.1.1 lib/skylight/util/lru_cache.rb
skylight-5.1.0.beta3 lib/skylight/util/lru_cache.rb
skylight-5.1.0.beta2 lib/skylight/util/lru_cache.rb