Class: RSpec::Puppet::Cache
- Inherits:
-
Object
- Object
- RSpec::Puppet::Cache
- Defined in:
- lib/rspec-puppet/cache.rb
Constant Summary collapse
- MAX_ENTRIES =
16
Instance Method Summary collapse
- #get(*args, &blk) ⇒ Object
-
#initialize(&default_proc) ⇒ Cache
constructor
A new instance of Cache.
Constructor Details
#initialize(&default_proc) ⇒ Cache
Returns a new instance of Cache.
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 |