Class: Glim::Cache
- Inherits:
-
Object
- Object
- Glim::Cache
- Defined in:
- lib/cache.rb
Constant Summary collapse
- CACHE_PATH =
'.cache/glim/data.bin'
Class Method Summary collapse
- .getset(path, group = :default) ⇒ Object
- .load ⇒ Object
- .merge!(updates) ⇒ Object
- .save ⇒ Object
- .track_updates=(flag) ⇒ Object
- .updates ⇒ Object
Class Method Details
.getset(path, group = :default) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/cache.rb', line 35 def getset(path, group = :default) begin mtime = File.stat(path).mtime if record = cache.dig(group, path) if mtime == record['modified'] return record['data'] end end record = { 'modified' => mtime, 'data' => yield, } (cache[group] ||= {})[path] = record (@updates[group] ||= {})[path] = record if @updates record['data'] rescue Errno::ENOENT $log.warn("File does not exist: #{path}") nil end end |
.load ⇒ Object
8 9 10 |
# File 'lib/cache.rb', line 8 def load cache end |
.merge!(updates) ⇒ Object
29 30 31 32 33 |
# File 'lib/cache.rb', line 29 def merge!(updates) updates.each do |group, paths| (cache[group] ||= {}).merge!(paths) end end |
.save ⇒ Object
12 13 14 15 16 17 18 19 |
# File 'lib/cache.rb', line 12 def save unless @cache.nil? FileUtils.mkdir_p(File.dirname(CACHE_PATH)) open(CACHE_PATH, 'w') do |io| Marshal.dump(cache, io) end end end |
.track_updates=(flag) ⇒ Object
21 22 23 |
# File 'lib/cache.rb', line 21 def track_updates=(flag) @updates = flag ? {} : nil end |
.updates ⇒ Object
25 26 27 |
# File 'lib/cache.rb', line 25 def updates @updates end |