Class: Bosh::Cli::Cache

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cache_dir = nil) ⇒ Cache

Returns a new instance of Cache.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/cli/cache.rb', line 8

def initialize(cache_dir = nil)
@cache_dir = cache_dir || Bosh::Cli::DEFAULT_CACHE_DIR

  if File.exists?(@cache_dir) && !File.directory?(@cache_dir)
    raise CacheDirectoryError, "BOSH cache directory error: " +
                              "`#{@cache_dir}' is a file, not directory"
  end

  unless File.exists?(@cache_dir)
    FileUtils.mkdir_p(@cache_dir)
    File.chmod(0700, @cache_dir)
  end
end

Instance Attribute Details

#cache_dirObject (readonly)

Returns the value of attribute cache_dir.



6
7
8
# File 'lib/cli/cache.rb', line 6

def cache_dir
  @cache_dir
end

Instance Method Details

#read(key) ⇒ Object



22
23
24
25
26
# File 'lib/cli/cache.rb', line 22

def read(key)
  cached_file = path(key)
  return nil unless File.exists?(cached_file)
  File.read(cached_file)
end

#write(key, value) ⇒ Object



28
29
30
31
32
# File 'lib/cli/cache.rb', line 28

def write(key, value)
  File.open(path(key), "w") do |f|
    f.write(value)
  end
end