Change your umask so that only your user can read newly–created files. Note that this can be done in the shell before you start Emacs (shells usually provide a command called umask
for this purpose), or while Emacs is running by calling set-default-file-modes
. See chapter 26.7 Changing File Names and Attributes.
Note that all processes have a umask, so you can use this solution with all programs, not just Emacs.
Edit: your modification changes the nature of the question quite significantly. However, if you take another look at chapter 26.7 Changing File Names and Attributes of the Emacs Lisp manual, you will find a macro called with-file-modes
that allows you to do exactly what you want.
Also, NickD’s suggestion to simply change the permissions of the cache directory is a sound one. It doesn’t matter much if the files in a directory are readable or not if other users cannot traverse into it.