Class: Nutrella::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/nutrella/cache.rb

Overview

Provides a cache of the most recently used items.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, capacity) ⇒ Cache

Returns a new instance of Cache.



12
13
14
15
# File 'lib/nutrella/cache.rb', line 12

def initialize(path, capacity)
  @path = path
  @capacity = capacity
end

Instance Attribute Details

#capacityObject (readonly)

Returns the value of attribute capacity.



10
11
12
# File 'lib/nutrella/cache.rb', line 10

def capacity
  @capacity
end

#pathObject (readonly)

Returns the value of attribute path.



10
11
12
# File 'lib/nutrella/cache.rb', line 10

def path
  @path
end

Instance Method Details

#fetch(key) ⇒ Object



17
18
19
20
21
# File 'lib/nutrella/cache.rb', line 17

def fetch(key)
  value = lookup(key) || yield
  write(key, value)
  value
end

#search(search_reg_exp) ⇒ Object



23
24
25
26
27
# File 'lib/nutrella/cache.rb', line 23

def search(search_reg_exp)
  cache_contents.find { |k, _v| search_reg_exp.match?(k) }.last
rescue
  nil
end