Class: RightApiHelper::Cache

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

Instance Method Summary collapse

Methods inherited from Base

#api_url, #log_level, #logger

Constructor Details

#initialize(cache_uuid, cache_tmp_dir = ".") ⇒ Cache

Returns a new instance of Cache.



21
22
23
24
# File 'lib/right_api_helper/cache.rb', line 21

def initialize(cache_uuid, cache_tmp_dir=".")
  logger
  @cache_file = File.join(cache_tmp_dir, "#{cache_uuid}.yml")
end

Instance Method Details

#clearObject



43
44
45
46
47
48
49
50
# File 'lib/right_api_helper/cache.rb', line 43

def clear
  if File.exists?(@cache_file)
    @log.info("removing cache file at #{@cache_file}")
    File.delete(@cache_file)
  else
    @log.info("no cache file to remove at #{@cache_file}")
  end
end

#getObject

Get all instances for all clouds registered in account



27
28
29
30
31
32
33
34
35
36
# File 'lib/right_api_helper/cache.rb', line 27

def get
  hash = nil
  if File.exists?(@cache_file)
    @log.info "Reading cache from #{@cache_file}"
    hash = YAML::load(File.open(@cache_file))
  else
    @log.info "No cache found at #{@cache_file}"
  end
  hash
end

#set(hash) ⇒ Object



38
39
40
41
# File 'lib/right_api_helper/cache.rb', line 38

def set(hash)
  @log.info "Writing cache to #{@cache_file}"
  File.open(@cache_file, "w") { |f| f.write(YAML.dump(hash)) }
end