I am making a simple helper application that changes the brightness of my screen, but I need to cache a value. How do I get the path for the user’s .cache directory so I can write a file there?
Getting the cache directory is platform specific.
XDG Base Directory Specification specifies:
$XDG_CACHE_HOME
defines the base directory relative to which user-specific non-essential data files should be stored. If$XDG_CACHE_HOME
is either not set or empty, a default equal to$HOME
/.cache should be used.
Macos uses: ~/Library/Caches
Windows uses: %LOCALAPPDATA%\Temp
You can use the known-folders library to get the .cache
folder or other well known folders.
Thank you. I was messing with std.fs.realpath and felt like something was wrong lol.
Here’s what I use to get a path to a subdir within the cache directory FWIW:
(it’s basically the same as what @dimdin describes and what known-folders
does)
known-folders
is probably a better solution, but if you only need the cache directory, you can just yank this function from the compiler.
Note that this code is for getting the zig cache directory.
I’m a bit confused about the preferred path to cache on Windows.
Both the Go std and Zig compiler use %LocalAppData%
, but resinator
and known-folders
use %LocalAppData%/Temp
.
This usage is not documented in Environment variable - Wikipedia and, personally, the last path should be Cache
, instead of Temp
.
Saving directly in %LOCALAPPDATA%
is wrong. Use %LOCALAPPDATA%\Temp
for cache or temporary files.
The related environment variables are:
%LOCALAPPDATA%
= %USERPROFILE%\AppData\Local
%TEMP%
= %LOCALAPPDATA%\Temp
%TMP%
= %LOCALAPPDATA%\Temp
There are a lot of subfolders, such as:
%LOCALAPPDATA%\Programs
%LOCALAPPDATA%\ProgramData
%LOCALAPPDATA%\Desktop
%LOCALAPPDATA%\Documents
@dimdin Thanks.
Do you know an official reference from Microsoft?
And does it means that Go and Zig use the %LOCALAPPDATA%
incorrectly?
UPDATE:
I just checked on a Windows 12 PC.
Firefox, Chromium and OneDrive (and others) use just %LOCALAPPDATA%.
Temp
seems to only used for temporary data, and not cache.
Unofficial reference: Redirecting
New API: KNOWNFOLDERID (Knownfolders.h) - Win32 apps | Microsoft Learn
Old (deprecated) API: CSIDL (Shlobj.h) - Win32 apps | Microsoft Learn
Using subfolders in Company\Product format is correct and common usage.
For example: %LOCALAPPDATA%\Microsoft\Windows\Temporary Internet Files
I don’t know if the use a subfolder or not.
Saving files in the %LOCALAPPDATA%
folder in incorrect; it is like saving files in C:\
.
As for the Microsoft reference on the usage:
Store and retrieve settings and other app data - Windows apps | Microsoft Learn
Cache data is meant to be temporary data, though (i.e. it can be cleared without causing a problem). From the link that @pachde posted:
Temporary app data
The temporary app data store works like a cache.
I’d suggest checking XDG_*
on macOS as well, for the various relevant paths. It’s Unix after all.
I do set those variables, and it’s always nice when programs respect that.
Makes sense. I think I was trying to follow known-folders
’ lead, where xdg_on_mac
defaults to false.
EDIT: Made that change and updated the link in my previous comment.