Class: Enc::Cache::NodeCache

Inherits:
Object
  • Object
show all
Includes:
Utils::Logging
Defined in:
lib/enc/cache/node_cache.rb

Instance Method Summary collapse

Methods included from Utils::Logging

configure_logger, convert_log_level, #logger, logger_for, set_config, set_log_file, set_log_level

Constructor Details

#initialize(config) ⇒ NodeCache

Returns a new instance of NodeCache.



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

def initialize(config)
  ensure_cache_dir(config.get('jive_enc_cache_dir'))
end

Instance Method Details

#delete!(hostname) ⇒ Object



30
31
32
33
# File 'lib/enc/cache/node_cache.rb', line 30

def delete!(hostname)
  logger.debug("Deleting cache #{cache_path(hostname)}")
  File.delete(cache_path(hostname)) if File.exist?(cache_path(hostname))
end

#exists?(hostname) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
# File 'lib/enc/cache/node_cache.rb', line 10

def exists?(hostname)
  logger.debug("Checking to see if cache file #{cache_path(hostname)} exists")
  File.exist?(cache_path(hostname))
end

#read(hostname) ⇒ Object



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

def read(hostname)
  logger.debug("Attempting to read cache #{cache_path(hostname)}")
  begin
    File.open(cache_path(hostname), 'r') { |file| Marshal.load(file.read)}
  rescue Errno::ENOENT
    logger.error("The node #{hostname} does not exist in the cache.")
    raise CacheDoesNotExist, "The node #{hostname} does not exist in the cache."
  end
end

#write(hostname, node) ⇒ Object



25
26
27
28
# File 'lib/enc/cache/node_cache.rb', line 25

def write(hostname, node)
  logger.debug("Writing to cache #{cache_path(hostname)}")
  File.open(cache_path(hostname), 'w') { |file| file.write(Marshal.dump(node)) }
end