Class: Portage::Cli::ProbeCache

Inherits:
Object
  • Object
show all
Defined in:
lib/portage/cli/probe_cache.rb

Overview

Remembers which origins answered /.well-known/ucp and which didn't.

Without this, every URL-less search re-probes the same dozen hosts, and a tool that fans out one unsolicited request per host per invocation is a crawler wearing a CLI's clothes. Negative results are cached too — and for longer — because "this shop doesn't speak UCP" is the answer that would otherwise be re-asked most often and changes least.

Constant Summary collapse

PATH =
File.join(Dir.home, ".portage", "discovery-cache.json").freeze
HIT_TTL =
6 * 60 * 60
MISS_TTL =
24 * 60 * 60

Instance Method Summary collapse

Constructor Details

#initialize(path: PATH, now: Time.now) ⇒ ProbeCache

Returns a new instance of ProbeCache.



18
19
20
21
# File 'lib/portage/cli/probe_cache.rb', line 18

def initialize(path: PATH, now: Time.now)
  @path = path
  @now = now.to_i
end

Instance Method Details

#fetch(origin) ⇒ Boolean?

Returns cached verdict, or nil when unknown/expired.

Returns:

  • (Boolean, nil)

    cached verdict, or nil when unknown/expired.



24
25
26
27
28
29
30
31
32
# File 'lib/portage/cli/probe_cache.rb', line 24

def fetch(origin)
  entry = store[origin]
  return nil unless entry.is_a?(Hash) && entry.key?("ucp")

  ttl = entry["ucp"] ? HIT_TTL : MISS_TTL
  return nil if @now - entry["at"].to_i > ttl

  entry["ucp"]
end

#record(origin, ucp) ⇒ Object



34
35
36
37
38
# File 'lib/portage/cli/probe_cache.rb', line 34

def record(origin, ucp)
  store[origin] = { "ucp" => ucp, "at" => @now }
  write
  ucp
end