Module: Jekyll::Archimate::ArchimateCache

Defined in:
lib/jekyll/archimate/archimate_cache.rb

Class Method Summary collapse

Class Method Details

.cache_stale?(file_path) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
60
# File 'lib/jekyll/archimate/archimate_cache.rb', line 54

def cache_stale?(file_path)
  @@cache ||= {}
  rel = rel_path(file_path)
  mod_time = File.mtime(rel)
  !@@cache.key?(rel) ||
    @@cache[rel][:cache_time] < mod_time
end

.load_model(file_path) ⇒ Object

load an archimate model from either…

  1. Memory

  2. Cached & Marshaled File

  3. Archimate File

Defaulting to the Archimate file if it is newer than either cached version



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/jekyll/archimate/archimate_cache.rb', line 13

def load_model(file_path)
  @@cache ||= {}
  file_path.sub!(%r{^/}, "")
  mod_time = File.mtime(file_path)

  if @@cache.key?(file_path)
    model_info = @@cache[file_path]
    return model_info[:model] if model_info[:cache_time] >= mod_time
  end

  cache_file = marshal_cache_file(file_path)
  if File.exist?(cache_file) && File.mtime(cache_file) >= mod_time
    begin
      @@cache[file_path] = {
        cache_time: File.mtime(cache_file),
        model: File.open(cache_file, "rb") { |f| Marshal.load(f) }
      }
      return @@cache[file_path][:model]
  rescue ArgumentError
    puts "Cache file is invalid - removing cache #{cache_file} and loading #{file_path}"
    FileUtils.rm cache_file
  end
  end

  model = ::Archimate.read(file_path)
  File.open(cache_file, "wb") { |f| Marshal.dump(model, f) }
  @@cache[file_path] = {
    cache_time: File.mtime(cache_file),
    model: model
  }

  model
end

.marshal_cache_file(path) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/jekyll/archimate/archimate_cache.rb', line 63

def marshal_cache_file(path)
  file_path, file_name = File.split(path)
  cache_path = File.join("_cache", file_path).split("/").inject("") do |cpath, rel_dir|
    npath = cpath.empty? ? rel_dir : File.join(cpath, rel_dir)
    Dir.mkdir(File.absolute_path(npath)) unless Dir.exist?(File.absolute_path(npath))
    npath
  end
  File.join(cache_path, File.basename(file_name, ".archimate" + ".marshal"))
end

.rel_path(file_path) ⇒ Object



48
49
50
# File 'lib/jekyll/archimate/archimate_cache.rb', line 48

def rel_path(file_path)
  file_path.sub!(%r{^/}, "")
end