Sha256: eadbab01eb97a3a1db66b5502d28434222077b758c5f43b305116704ccdac965

Contents?: true

Size: 1.06 KB

Versions: 17

Compression:

Stored size: 1.06 KB

Contents

# -*- coding: utf-8; frozen_string_literal: true -*-
#
#--
# Copyright (C) 2009-2019 Thomas Leitner <t_leitner@gmx.at>
#
# This file is part of kramdown which is licensed under the MIT.
#++
#

module Kramdown
  module Utils

    # A simple least recently used (LRU) cache.
    #
    # The cache relies on the fact that Ruby's Hash class maintains insertion order. So deleting
    # and re-inserting a key-value pair on access moves the key to the last position. When an
    # entry is added and the cache is full, the first entry is removed.
    class LRUCache

      # Creates a new LRUCache that can hold +size+ entries.
      def initialize(size)
        @size = size
        @cache = {}
      end

      # Returns the stored value for +key+ or +nil+ if no value was stored under the key.
      def [](key)
        (val = @cache.delete(key)).nil? ? nil : @cache[key] = val
      end

      # Stores the +value+ under the +key+.
      def []=(key, value)
        @cache.delete(key)
        @cache[key] = value
        @cache.shift if @cache.length > @size
      end

    end

  end
end

Version data entries

17 entries across 17 versions & 4 rubygems

Version Path
kramdown-2.5.1 lib/kramdown/utils/lru_cache.rb
kramdown-2.5.0 lib/kramdown/utils/lru_cache.rb
getargv-0.3.3-universal-darwin vendor/bundle/ruby/3.3.0/gems/kramdown-2.4.0/lib/kramdown/utils/lru_cache.rb
kramdown-2.4.0 lib/kramdown/utils/lru_cache.rb
kramdown-2.3.2 lib/kramdown/utils/lru_cache.rb
kramdown-2.3.1 lib/kramdown/utils/lru_cache.rb
daqing_kramdown-2.3.2 lib/kramdown/utils/lru_cache.rb
daqing_kramdown-2.3.1 lib/kramdown/utils/lru_cache.rb
zine_brewer-1.5.0 vendor/bundle/ruby/2.7.0/gems/kramdown-2.3.0/lib/kramdown/utils/lru_cache.rb
zine_brewer-1.3.0 vendor/bundle/ruby/2.7.0/gems/kramdown-2.3.0/lib/kramdown/utils/lru_cache.rb
kramdown-2.3.0 lib/kramdown/utils/lru_cache.rb
kramdown-2.2.1 lib/kramdown/utils/lru_cache.rb
kramdown-2.2.0 lib/kramdown/utils/lru_cache.rb
kramdown-2.1.0 lib/kramdown/utils/lru_cache.rb
kramdown-2.0.0 lib/kramdown/utils/lru_cache.rb
kramdown-2.0.0.beta2 lib/kramdown/utils/lru_cache.rb
kramdown-2.0.0.beta1 lib/kramdown/utils/lru_cache.rb