module RSpec::Puppet
  class Cache

    MAX_ENTRIES = 16

    # @param [Proc] default_proc The default proc to use to fetch objects on cache miss
    def initialize(&default_proc)
      @default_proc = default_proc
      @cache = {}
      @lra = []
    end

    def get(*args, &blk)
      # decouple the hash key from whatever the blk might do to it
      key = Marshal.load(Marshal.dump(args))
      if !@cache.has_key? key
        @cache[key] = (blk || @default_proc).call(*args)
        @lra << key
        expire!
      end

      @cache[key]
    end

    private

    def expire!
      expired = @lra.slice!(0, @lra.size - MAX_ENTRIES)
      expired.each { |key| @cache.delete(key) } if expired
    end
  end
end

module RSpec::Puppet
  class Cache

    MAX_ENTRIES = 16

    # @param [Proc] default_proc The default proc to use to fetch objects on cache miss
    def initialize(&default_proc)
      @default_proc = default_proc
      @cache = {}
      @lra = []
    end

    def get(*args, &blk)
      key = Marshal.load(Marshal.dump(args))
      if @cache.has_key?(key)
        # Cache hit
        # move that entry last to make it "most recenty used"
        @lra.insert(-1, @lra.delete_at(@lra.index(args)))
      else
        # Cache miss
        # Ensure room by evicting least recently used if no space left
        expire!
        @cache[args] = (blk || @default_proc).call(*args)
        @lra << args
      end

      @cache[key]
    end

    private

    def expire!
      # delete one entry (the oldest) when there is no room in cache
      @cache.delete(@lra.shift) if @cache.size == MAX_ENTRIES
    end
  end
end

Make caching evict least recently used
Make caching evict least recently used

Before this, when the cache was full, it evicted max-cache-size number of entries from the list of entries.
It would also do eviction after addition.

With a cache size of 3, and using the sequence A, B, C, D, The old version would evict A, B, C. If again B or C was requested, then they would need to be recreated. The old would then have state D. The new will evict only A when it gets D. When it looks up B, it will also move B last, so that the state the is CDB. Next addition E, will the evict C, and state is DBE. If B is again requested, it is moved last and state s DEB.- i.e. the cache caches Most Recently Used.

I am making this change to improve the caching behavior, but also to see if it has effect on the tests that fail on catalog id being different.
