Class: Glim::Cache

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

Constant Summary collapse

CACHE_PATH =
'.cache/glim/data.bin'

Class Method Summary collapse

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

.loadObject



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

.saveObject



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

.updatesObject



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

def updates
  @updates
end