Move cache to a ramdisk

Reasons

Modern browsers (both Chrome and Firefox) tend to cache. Each action writes some files to the cache directory and then clears it when it expires. You can check the cache location and its current status in firefox by entering "about:cache" as the URL. On Linux, it is located at $HOME/.cache/mozilla/firefox/xxx.

Frequent overwrites are not healthy for SSDs, which are the standard these days. Reduced SSD life is the number one reason.

I do daily backups by taking a ZFS snapshot of my home directory and replicating that snapshot to a remote location for disaster recovery reasons. The content of the cache is not important for backup purposes. Excluding the cache directory saved me about 1G of backup traffic.

My home directory is a ZFS encrypted volume. Excluding the cache directory saves CPU and battery resources.

Would like to

$HOME/.cache is a common place for every (well-written) application to place cache files. This directory becomes a candidate to become "ramdisk" (in terms of Windows) or "tmpfs" in terms of linux. Additional RAM may be purchased as a prerequisite. Actually RAM was cheap at the time I bought my laptop and I have a lot of RAM to use it as ramdisk.

There is no problem to mount any directory as tmpfs in simple environment. Like:

# grep cache /etc/fstab
tmpfs		/home/voleg/.cache	tmpfs	uid=1000,gid=1000,mode=750 0 0

My problem was that this mount should exactly happen after ZFS mounts its file system.

Systemd mounts FS at time you want.

Once you let systemd take control of the boot process, the /etc/fstab file will be converted to systemd unit files. You can check this by:

 # find /run/systemd/generator -name "*.mount"

You can create your own new systemd mount unit file without changing the /etc/fstab file.

# cat /etc/systemd/system/home-voleg-.cache.mount
[Unit]
Description = Mount cache directory as tmpfs
After = zfs.target
Before = graphical.target
DefaultDependencies=no

[Mount]
Where=/home/voleg/.cache
What=tmpfs
Type=tmpfs
Options=uid=1000,gid=1000,mode=750

[Install]
WantedBy = multi-user.target

Note that the name of the unit matches the mount point, replacing "/" with "-" .

Once fle created, reload the systemd files and enable new unit:

# systemctl daemon-reload
# systemctl enable home-voleg-.cache.mount

Pay attention, that all information about source, target, options and timing persist at unit file. Once finished, reboot the system to check everything working.


Updated on Mon Aug 22 23:20:03 IDT 2022 More documentations here