Cache downloaded snaps

Add support for caching snaps.

Many people have been asking that snapd has some minimal caching of snaps. This is about doing that.

1 Like

Idea we quickly mentioned last week:

  1. When starting a download, check if it exists in /var/lib/snapd/cache/<digest>
  2. If found, update its mtime, hardlink into target location, and return success
  3. If not found, download the snap
  4. On success, hardlink into /var/lib/snapd/cache/<digest>
  5. If cache dir has more than N entries, remove oldest mtimes until it has N
1 Like

Sugested implementation based on the above ideas: https://github.com/snapcore/snapd/pull/4017

1 Like

This looks cool and it’s absolutely sufficient as first implementation but I’d like to propose two later improvements:

  • Make the N parameter configurable (perhaps via coreconfig). Ideally it would not be a simple counter as that is hard to control but instead a amount of space we are willing to use (e.g. 1GB).
  • When considering a snap to discard from cache compute the set of cached snaps and sort them by some meaningful criteria, e.g. the fewest items to discard that makes us respect the size limit.

What do you think?

@mvo Thank you! Just reviewed it. Minor items only.

@zyga It’s already being sorted by some meaningful criteria, I think? Size is irrelevant for this, though… we want the LRU item instead of keeping largest/smallest around forever.

well, kinda. When we get around to implementing Out-of-disk-space protection we’ll want to have a policy WRT how it interacts with this cache.

Indeed, but we’d still not order them according to the file size. We’d simply cut down further into the cache until it satisfies some criteria, or just disable it altogether.

1 Like