class Nucleon::Util::Cache

Public Class Methods

new(root_path, id, cache_dir = '.cache', force = true) click to toggle source
# File lib/core/util/cache.rb, line 16
def initialize(root_path, id, cache_dir = '.cache', force = true)
  super({}, {}, force)
  
  @cache_dir  = cache_dir    
  @cache_root = File.join(root_path, cache_dir)    
  FileUtils.mkdir_p(base_path) unless File.directory?(base_path)
  
  @cache_id         = id.to_sym
  @cache_translator = Nucleon.type_default(:translator)
  @cache_filename   = "#{id}.#{translator}"
  @cache_path       = File.join(@cache_root, @cache_filename)
end

Public Instance Methods

base_path() click to toggle source
# File lib/core/util/cache.rb, line 32
def base_path
  @cache_root
end
clear() click to toggle source
# File lib/core/util/cache.rb, line 91
def clear
  result = super
  save if initialized?
  result
end
delete(keys, default = nil) click to toggle source
# File lib/core/util/cache.rb, line 83
def delete(keys, default = nil)
  result = super
  save if initialized?
  result
end
directory_name() click to toggle source
# File lib/core/util/cache.rb, line 38
def directory_name
  @cache_dir
end
file() click to toggle source
# File lib/core/util/cache.rb, line 56
def file
  @cache_path
end
get(keys, default = nil, format = false) click to toggle source
# File lib/core/util/cache.rb, line 62
def get(keys, default = nil, format = false)
  result = super(keys, nil)
  
  if result.nil?
    load
    result = super(keys, nil)
  end
  result = filter(default, format) if result.nil?
  result
end
id() click to toggle source
# File lib/core/util/cache.rb, line 44
def id
  @cache_id
end
import_base(properties, options = {}) click to toggle source
# File lib/core/util/cache.rb, line 100
def import_base(properties, options = {})
  config = Config.ensure(options)
  
  result = super
  save if initialized? && ! config.get(:no_save, false)
  result
end
set(keys, value) click to toggle source
# File lib/core/util/cache.rb, line 75
def set(keys, value)
  result = super
  save if initialized?
  result
end
translator() click to toggle source
# File lib/core/util/cache.rb, line 50
def translator
  @cache_translator
end

Protected Instance Methods

load() click to toggle source
# File lib/core/util/cache.rb, line 110
def load
  success = false
  
  @@cache_lock.synchronize do  
    logger.info("Loading #{translator} translated cache from #{file}")
        
    parser = CORL.translator({}, translator)
    raw    = Disk.read(file)    
      
    if parser && raw && ! raw.empty?
      logger.debug("Cache file contents: #{raw}")            
      parse_properties = Data.hash(parser.parse(raw))
      
      Nucleon.remove_plugin(parser)
      
      import(parse_properties, { :no_save => true }) unless parse_properties.empty?
      success = true
    end
  end  
  success   
end
save() click to toggle source
# File lib/core/util/cache.rb, line 135
def save
  success = false
  
  @@cache_lock.synchronize do
    if renderer = CORL.translator({}, translator)
      rendering = renderer.generate(export)
      
      Nucleon.remove_plugin(renderer)
        
      if Disk.write(file, rendering)
        success = true
      end
    end
  end
  success   
end