Class: Nucleon::Util::Cache
Constant Summary collapse
- @@cache_lock =
Mutex.new
Instance Attribute Summary
Attributes inherited from Core
Instance Method Summary collapse
-
#base_path ⇒ Object
—.
-
#clear ⇒ Object
—.
-
#delete(keys, default = nil) ⇒ Object
—.
-
#directory_name ⇒ Object
—.
-
#file ⇒ Object
—.
-
#get(keys, default = nil, format = false) ⇒ Object
—.
-
#id ⇒ Object
—.
-
#import_base(properties, options = {}) ⇒ Object
—————————————————————————– Operations.
-
#initialize(root_path, id, cache_dir = '.cache', force = true) ⇒ Cache
constructor
—————————————————————————– Constructor / Destructor.
-
#load ⇒ Object
—.
-
#save ⇒ Object
—.
-
#set(keys, value, delete_nil = false) ⇒ Object
—.
-
#status ⇒ Object
—————————————————————————– Property accessors / modifiers.
-
#translator ⇒ Object
—.
Methods inherited from Core
#initialized?, logger, ui, ui_group, #ui_group
Methods included from Mixin::Colors
#black, #blue, #cyan, #green, #grey, #purple, #red, #yellow
Methods inherited from Config
#[], #[]=, #append, array, #array, #defaults, #empty?, ensure, #export, #filter, filter, #get_array, #get_hash, #has_key?, #hash, hash, #import, #init, init, init_flat, #keys, #prepend, string, #string, #string_map, string_map, symbol, #symbol, #symbol_array, symbol_map, #symbol_map, test, #test
Methods included from Mixin::ConfigOptions
#all_options, #clear_options, #contexts, #get_options, #set_options
Methods included from Mixin::ConfigCollection
#all_properties, #clear_properties, #delete_property, #get_property, #save_properties, #set_property
Constructor Details
#initialize(root_path, id, cache_dir = '.cache', force = true) ⇒ Cache
Constructor / Destructor
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# 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(:nucleon, :translator) @cache_filename = "#{id}.#{translator}" @cache_path = File.join(@cache_root, @cache_filename) unless File.exist?(file) parser = CORL.translator({}, translator) Disk.write(file, parser.generate({})) end load end |
Instance Method Details
#base_path ⇒ Object
44 45 46 |
# File 'lib/core/util/cache.rb', line 44 def base_path @cache_root end |
#clear ⇒ Object
103 104 105 106 107 |
# File 'lib/core/util/cache.rb', line 103 def clear result = super save if initialized? result end |
#delete(keys, default = nil) ⇒ Object
95 96 97 98 99 |
# File 'lib/core/util/cache.rb', line 95 def delete(keys, default = nil) result = super save if initialized? result end |
#directory_name ⇒ Object
50 51 52 |
# File 'lib/core/util/cache.rb', line 50 def directory_name @cache_dir end |
#file ⇒ Object
68 69 70 |
# File 'lib/core/util/cache.rb', line 68 def file @cache_path end |
#get(keys, default = nil, format = false) ⇒ Object
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/core/util/cache.rb', line 74 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 ⇒ Object
56 57 58 |
# File 'lib/core/util/cache.rb', line 56 def id @cache_id end |
#import_base(properties, options = {}) ⇒ Object
Operations
112 113 114 115 116 117 118 |
# File 'lib/core/util/cache.rb', line 112 def import_base(properties, = {}) config = Config.ensure() result = super save if initialized? && ! config.get(:no_save, false) result end |
#load ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/core/util/cache.rb', line 122 def load success = false @status = 255 @@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 @status = Nucleon.code.success end end success end |
#save ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/core/util/cache.rb', line 148 def save success = false @status = 255 @@cache_lock.synchronize do if renderer = CORL.translator({}, translator) rendering = renderer.generate(export) Nucleon.remove_plugin(renderer) if Disk.write(file, rendering) success = true @status = Nucleon.code.success end end end success end |
#set(keys, value, delete_nil = false) ⇒ Object
87 88 89 90 91 |
# File 'lib/core/util/cache.rb', line 87 def set(keys, value, delete_nil = false) result = super save if initialized? result end |
#status ⇒ Object
Property accessors / modifiers
38 39 40 |
# File 'lib/core/util/cache.rb', line 38 def status @status end |
#translator ⇒ Object
62 63 64 |
# File 'lib/core/util/cache.rb', line 62 def translator @cache_translator end |