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



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

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

Instance Method Details

#get(*args, &blk) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rspec-puppet/cache.rb', line 13

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