Class: RSpec::Puppet::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec-puppet/cache.rb

Constant Summary collapse

MAX_ENTRIES =
16

Instance Method Summary collapse

Constructor Details

#initialize(&default_proc) ⇒ Cache

Returns a new instance of Cache.

Parameters:

  • default_proc (Proc)

    The default proc to use to fetch objects on cache miss



8
9
10
11
12
# File 'lib/rspec-puppet/cache.rb', line 8

def initialize(&default_proc)
  @default_proc = default_proc
  @cache = {}
  @lra = []
end

Instance Method Details

#get(*args, &blk) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rspec-puppet/cache.rb', line 14

def get(*args, &blk)
  key = Marshal.load(Marshal.dump(args))
  if @cache.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