Class: Ec2Meta::Cache

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

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache.



5
6
7
# File 'lib/ec2_meta/cache.rb', line 5

def initialize
  @cache = {}
end

Instance Method Details

#clearObject



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

Returns:

  • (Boolean)


26
27
28
# File 'lib/ec2_meta/cache.rb', line 26

def exist?(key)
  @cache.key?(key.to_s)
end

#fetch(key) ⇒ Object

Raises:

  • (ArgumentError)


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