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.



3
4
5
# File 'lib/ec2_meta/cache.rb', line 3

def initialize
  @cache = {}
end

Instance Method Details

#clearObject



32
33
34
# File 'lib/ec2_meta/cache.rb', line 32

def clear
  @cache.clear
end

#delete(key) ⇒ Object



28
29
30
# File 'lib/ec2_meta/cache.rb', line 28

def delete(key)
  @cache.delete(key)
end

#exist?(key) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/ec2_meta/cache.rb', line 24

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

#fetch(key) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/ec2_meta/cache.rb', line 15

def fetch(key)
  return read(key) if exist?(key)

  fail ArgumentError, 'require block' unless block_given?

  value = yield
  write(key, value)
end

#read(key) ⇒ Object



11
12
13
# File 'lib/ec2_meta/cache.rb', line 11

def read(key)
  @cache[key.to_s]
end

#write(key, value) ⇒ Object



7
8
9
# File 'lib/ec2_meta/cache.rb', line 7

def write(key, value)
  @cache[key.to_s] = value
end