Local distfiles cache

When several machines are running Gentoo on the same local area network, it is a good idea to cache distfiles on one of them and let the rest download from that one. This saves bandwidth for both the network owner and the public source mirrors.

Setting up the server
Select the machine that is going to serve the local distfiles cache and set it up. It will need to have enough free disk space to store all of the packages that are used by any machines on the network. The server machine will also need to be available whenever any other computer emerges a new package, and other computers on the network will need to be able to reach it using a static IP address or hostname.

There are a number of ways in which the cache can be implemented. This page describes a simple setup using.

An alternative is to configure to act as a proxy server for any available mirror, a configuration example can be found below.

Installing software
Install net-misc/apt-cacher-ng:

Configuring for Gentoo
Apt-cacher-ng includes support for the Gentoo source mirrors, but some configuration is still needed. Since it is designed primarily for use with the Debian package management system, there are some types of files used in Gentoo distfiles which by default it refuses to download. To change this behavior, create the file :

Configuring mirrors
Configure the list of public Gentoo mirrors from which apt-cacher-ng will download source packages:

Starting the service
Now start the cache service:

To start the service at boot:

Using
Instead of using and in case  is already available, the latter can be used as a caching proxy for LAN clients.

A major disadvantage of this setup is that the proxy server, if it uses itself, stores all packages twice, once in the nginx proxy cache and once in.

Setting up nginx VHost
Add the following to or some other file included into nginx server configuration:

This configuration retains downloaded packages for at most 7 days until they would be redownloaded by nginx. Also, the maximum disk space used by the cache is up to 10GB and nginx will pause all subsequent requests for the same file until either one request has been completed by downloading from the mirror server or the one-minute-timeout has been reached. These values could and should be modified to local necessities, especially so, if downstream is slower to deliver large package files.

Restart nginx server
If everything is reported ok, nginx can be forced to be restarted or to reload its configuration:

After this, clients can use nginx as a caching proxy as outlined below.

Setting up clients
On all machines which are to use the distfiles cache (including the cache server itself), add the following to :

Enhancements

 * Emerge net-misc/apt-cacher-ng with the  use flag and mount  on  on the cache server. This avoids storing two copies of all of the packages installed on that server.

Open issues

 * Apt-cacher-ng installs a cron job to delete unreferenced files from the cache. Since the Gentoo cache contains no index files, this probably deletes either everything or nothing from it.


 * If  is set to   in, apt-cacher-ng will cache the snapshots. This is good if multiple machines are set up to use webrsync, but in the preferable case that a local rsync mirror is being used, it is just a waste of space.

External resources

 * [//serverfault.com/a/559076 Running a Gentoo distfiles caching mirror on Debian]
 * [//www.unix-ag.uni-kl.de/~bloch/acng/html/ Apt-Cacher-NG User Manual]