Class: DepsGrapher::CacheFile

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/deps_grapher/cache_file.rb

Instance Method Summary collapse

Methods included from Logging

#error, #info, #verbose, #warn

Constructor Details

#initialize(file:, ttl:) ⇒ CacheFile

Returns a new instance of CacheFile.



7
8
9
10
# File 'lib/deps_grapher/cache_file.rb', line 7

def initialize(file:, ttl:)
  @file = file
  @ttl = ttl
end

Instance Method Details

#clean!(force: false) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/deps_grapher/cache_file.rb', line 40

def clean!(force: false)
  return unless File.exist?(@file)
  return unless force || stale?

  FileUtils.rm_f @file
  info { "Removed stale cache file: #{@file}" }
end

#readObject



24
25
26
27
28
29
30
31
32
# File 'lib/deps_grapher/cache_file.rb', line 24

def read
  return nil unless File.exist?(@file)

  info { "Reading cache from #{@file} (#{File.size(@file)} bytes)" }

  File.open(@file) do |f|
    return Marshal.load f
  end
end

#stale?Boolean

Returns:

  • (Boolean)


34
35
36
37
38
# File 'lib/deps_grapher/cache_file.rb', line 34

def stale?
  return false unless File.exist?(@file)

  File.mtime(@file).to_i < (Time.now.to_i - @ttl)
end

#write(target) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/deps_grapher/cache_file.rb', line 12

def write(target)
  return if File.exist?(@file)

  FileUtils.mkdir_p File.dirname(@file)

  info { "Writing cache to #{@file}" }

  File.open(@file, "w") do |f|
    Marshal.dump target, f
  end
end