Class: Ec2Meta::Cache
- Inherits:
-
Object
- Object
- Ec2Meta::Cache
- Defined in:
- lib/ec2_meta/cache.rb
Instance Method Summary collapse
- #clear ⇒ Object
- #delete(key) ⇒ Object
- #exist?(key) ⇒ Boolean
- #fetch(key) ⇒ Object
-
#initialize ⇒ Cache
constructor
A new instance of Cache.
- #read(key) ⇒ Object
- #write(key, value) ⇒ Object
Constructor Details
#initialize ⇒ Cache
Returns a new instance of Cache.
5 6 7 |
# File 'lib/ec2_meta/cache.rb', line 5 def initialize @cache = {} end |
Instance Method Details
#clear ⇒ Object
34 35 36 |
# File 'lib/ec2_meta/cache.rb', line 34 def clear @cache.clear end |
#delete(key) ⇒ Object
30 31 32 |
# File 'lib/ec2_meta/cache.rb', line 30 def delete(key) @cache.delete(key) end |
#exist?(key) ⇒ Boolean
26 27 28 |
# File 'lib/ec2_meta/cache.rb', line 26 def exist?(key) @cache.key?(key.to_s) end |
#fetch(key) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/ec2_meta/cache.rb', line 17 def fetch(key) return read(key) if exist?(key) raise ArgumentError, 'require block' unless block_given? value = yield write(key, value) end |
#read(key) ⇒ Object
13 14 15 |
# File 'lib/ec2_meta/cache.rb', line 13 def read(key) @cache[key.to_s] end |
#write(key, value) ⇒ Object
9 10 11 |
# File 'lib/ec2_meta/cache.rb', line 9 def write(key, value) @cache[key.to_s] = value end |