Sha256: 64ce4c8b9ee01cc2944856048dd48f3fa9b18543a4a036ce9574fba6db7fea15

Contents?: true

Size: 1.03 KB

Versions: 6

Compression:

Stored size: 1.03 KB

Contents

# -*- encoding: utf-8 -*-
#
#--
# Copyright (C) 2014-2017 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

6 entries across 6 versions & 3 rubygems

Version Path
motion-kramdown-1.16.2 lib/kramdown/utils/lru_cache.rb
files.com-1.0.55 docs/vendor/bundle/ruby/2.5.0/gems/kramdown-1.17.0/lib/kramdown/utils/lru_cache.rb
kramdown-1.17.0 lib/kramdown/utils/lru_cache.rb
kramdown-1.16.2 lib/kramdown/utils/lru_cache.rb
kramdown-1.16.1 lib/kramdown/utils/lru_cache.rb
kramdown-1.16.0 lib/kramdown/utils/lru_cache.rb