xxx

Saturday 12 March 2022

Making the .cache folder a temporary folder under linux

HDD and Memory chip

 A lot of people complain about the size of the ~/.cache folder and many people ask if the folder can be deleted. The answer is "Yes, with some caveats". If that folder can be deleted, why is it a permanent folder and not a temporary one?

The answer usually is "Because storage space is cheap and computing power isn't". And that is a good answer. However, that is only valid if you are accessing the same files over and over day after day. For most of us, we do different things every day, browse different websites, see different pictures, read different emails, etc. So new files are cached and the old ones remain just taking space.

Here I will show two ways to make the .cache folder temporary: Deleting old files regularly and storing the .cache folder in ram.

Neither of these methods require sudo or root access. However, they require that you use systemd.

In reality you could use this method to make any other folder temporary, for example,  a user's temp folder or the Downloads folder.


Delete old files regularly

Advantages

  • The folder does not keep ballooning

Caveats

  • If you have an application which incorrectly assumes .cache is permanent, it may malfunction
  • If you have an application which generates a lot of cache files for things that you regularly use it may take a bit more time (usually negligible), next time it needs the files as it needs to recreate them.

How to cleanup .cache regularly

Get the user identifiers by executing 

 id

Create a file under ~/.local/share/user-tmpfiles.d with a rule to clean up the folder at a particular interval, for example, every week. Replace <uid>, <groupname> and <username> with the numeric user DI, the user group and the user name respectively.

mkdir -p ~/.local/share/user-tmpfiles.d
echo "D /run/user/<uid> /cache 0700 <groupname> <username> 1w" > ~/.local/share/user-tmpfiles.d/cachetmp.conf

For example, to clean john's cache every week:

mkdir -p ~/.local/share/user-tmpfiles.d
echo "D /run/user/1000 /cache 0700 john john 1w" > ~/.local/share/user-tmpfiles.d/cachetmp.conf

Note: You can find more information about the parameters in the tmpfiles.d man page 

man tmpfiles.d

Enable and start the cleanup service

systemctl --user enable systemd-tmpfiles-clean.timer
systemctl --user start  systemd-tmpfiles-clean.timer

And that's it.

 

Storing the .cache folder in ram

Advantages

  • Browsing is faster as all files are cached in memory
  • If you use SDD, it doesn't wear that much
  • If you use HDD, it doesn't trash the disk that much
  • It extends your battery time as HDD usage is reduced
  • The folder doesn't keep growing with crud from previous sessions as all the files are gone when the computer shuts down.
  • Increased privacy. All the cached files are gone when you shutdown. This includes thumb images.

Caveats

  • If a program caches large files you may consume your ram. This may not be noticeable most of the time but you need to be aware of it
  • If you have an application which incorrectly assumes .cache is permanent, it may malfunction
  • All the If you have an application which generates a lot of cache files for things that you regularly use it may take a bit more time (usually negligible), next time it needs the files as it needs to recreate them. Given that the files are stored in ram, generation is usually very fast.

How to create the .cache folder in ramdisk

Get the user identifiers by executing 

 id

 Create a file under ~/.local/share/user-tmpfiles.d with a rule to create the folder and clean it up at a particular interval, for example, every week. (the cleanup is only necesary if you don't reboot your computer frequently. Replace <uid>, <groupname> and <username> with the numeric user DI, the user group and the user name respectively.

mkdir -p ~/.local/share/user-tmpfiles.d
echo "D /run/user/<uid>/cache 0700 <groupname> <username> 1w
L+ /home/<username>/.cache - - - - /run/user/
<uid>/cache" > ~/.local/share/user-tmpfiles.d/cachetmp.conf

For example

echo "D /run/user/1000/cache 0700 john john 1w
L+ /home/john/.cache - - - - /run/user/1000/cache" > ~/.local/share/user-tmpfiles.d/cachetmp.conf

Note: You can find more information about the parameters in the tmpfiles.d man page

man tmpfiles.d

For example, the age parameter can be a number and ms,mu,s,m,h,d,w for miliseconds, microseconds, seconds, minutes, hours, days and weeks.

Enable and start the creation and cleanup services

systemctl --user enable systemd-tmpfiles-setup.service systemd-tmpfiles-clean.timer
systemctl --user start  systemd-tmpfiles-setup.service systemd-tmpfiles-clean.timer

 

Restoring the .cache folder to it's original state

If an application is giving you trouble or find that your cache is using all your memory, you just need to disable the services and delete the cachetmp.conf file

systemctl --user stop    systemd-tmpfiles-setup.service systemd-tmpfiles-clean.timer
systemctl --user disable systemd-tmpfiles-setup.service systemd-tmpfiles-clean.timer
rm ~/.local/share/user-tmpfiles.d/cachetmp.conf

If at next boot the .cache folder is not created, just create it manually.

And that's it.

No comments:
Write comments

© 2014 Using FOSS. Designed by Bloggertheme9
Powered by Blogger.