Class: Tebako::CacheCatalog

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

Overview

Readable, bounded view over all fast-build caches.

Defined Under Namespace

Classes: Entry

Constant Summary collapse

CACHE_TYPES =
{
  "application" => "application-cache",
  "bundle" => "bundle-cache",
  "deployment" => "deployment-cache",
  "finalized_runtime" => "finalized-runtime-cache",
  "filesystem" => "filesystem-cache",
  "native_gem" => "native-gem-cache",
  "runtime_deployment" => "runtime-deployment-cache"
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(deps) ⇒ CacheCatalog

Returns a new instance of CacheCatalog.



43
44
45
# File 'lib/tebako/cache_catalog.rb', line 43

def initialize(deps)
  @deps = File.expand_path(deps)
end

Instance Method Details

#cleanObject



85
86
87
88
89
90
# File 'lib/tebako/cache_catalog.rb', line 85

def clean
  CACHE_TYPES.each_value do |directory|
    path = File.join(@deps, directory)
    FileUtils.rm_rf(path, secure: true)
  end
end

#entriesObject



47
48
49
# File 'lib/tebako/cache_catalog.rb', line 47

def entries
  CACHE_TYPES.flat_map { |type, directory| scan(type, File.join(@deps, directory)) }
end

#prune(max_size: nil, older_than: nil) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/tebako/cache_catalog.rb', line 68

def prune(max_size: nil, older_than: nil)
  all = entries.sort_by(&:last_used)
  cutoff = older_than && (Time.now - older_than)
  total = all.sum(&:byte_size)
  removed = []
  all.each do |entry|
    eligible_by_age = cutoff && entry.last_used < cutoff
    eligible_by_size = max_size && total > max_size
    next unless eligible_by_age || eligible_by_size
    next unless remove_unless_locked(entry)

    removed << entry
    total -= entry.byte_size
  end
  removed
end

#remove(key) ⇒ Object



92
93
94
95
# File 'lib/tebako/cache_catalog.rb', line 92

def remove(key)
  matches = entries.select { |entry| entry.key == key || "#{entry.type}:#{entry.key}" == key }
  matches.select { |entry| remove_unless_locked(entry) }
end

#statsObject



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/tebako/cache_catalog.rb', line 51

def stats
  all = entries
  {
    "entries" => all.length,
    "bytes" => all.sum(&:byte_size),
    "valid" => all.count(&:valid),
    "invalid" => all.count { |entry| entry.valid == false },
    "by_type" => all.group_by(&:type).transform_values do |typed|
      { "entries" => typed.length, "bytes" => typed.sum(&:byte_size) }
    end
  }
end

#verifyObject



64
65
66
# File 'lib/tebako/cache_catalog.rb', line 64

def verify
  entries.select { |entry| entry.valid == false }
end