Class: Bsdkrun::Cache

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

Overview

Save and restore guest directories under a key, reached as Sandbox#cache.

Entries are keyed, so a rebuild can pick up where the last one left off:

hit = sbx.cache.restore(key: key, restore_keys: ["deps-"])
unless hit.restored
sbx.exec(["npm", "ci"])
sbx.cache.save("/app/node_modules", key: key)
end

Where entries live — host disk or S3 — is host configuration, not an SDK concern: set BSDKRUN_CACHE_BACKEND / BSDKRUN_CACHE_S3_*, or write ~/.config/bsdkrun/cache.toml.

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ Cache

Returns a new instance of Cache.

Parameters:

  • id (String)

    the machine's id.



41
42
43
# File 'lib/bsdkrun/cache.rb', line 41

def initialize(id)
  @id = id
end

Instance Method Details

#restore(key:, path: nil, restore_keys: []) ⇒ RestoreResult

Restore a stored tree.

Parameters:

  • key (String)
  • path (String, nil) (defaults to: nil)

    defaults to where the entry was saved from.

  • restore_keys (Array<String>) (defaults to: [])

    prefixes tried in order on a miss.

Returns:



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/bsdkrun/cache.rb', line 65

def restore(key:, path: nil, restore_keys: [])
  target = path ? "#{@id}:#{path}" : @id
  args = ["cache", "restore", target, "--key", key, "--json"]
  args += ["--restore-keys", *restore_keys] unless restore_keys.empty?
  row = json(args, "bsdkrun cache restore")
  RestoreResult.new(
    restored: !!row["restored"],
    requested_key: row["requested_key"].to_s,
    key: row["key"],
    path: row["path"],
    size: row["size"],
    compression: row["compression"],
    created: row["created"]
  )
end

#save(path, key:, compression: "gzip", force: false) ⇒ CacheEntry

Archive the guest directory at path under key.

Parameters:

  • path (String)

    absolute path in the guest.

  • key (String)

    key to store under.

  • compression (String) (defaults to: "gzip")

    gzip (default), zstd, estargz or none.

  • force (Boolean) (defaults to: false)

    replace an entry that already has this key.

Returns:



52
53
54
55
56
57
# File 'lib/bsdkrun/cache.rb', line 52

def save(path, key:, compression: "gzip", force: false)
  args = ["cache", "save", "#{@id}:#{path}", "--key", key, "--json"]
  args += ["--compression", compression] unless compression == "gzip"
  args << "--force" if force
  CacheEntry.from(json(args, "bsdkrun cache save"))
end