Os.cacheDir
func Os.cacheDir(): Result[string, Error]Os.cacheDir returns the directory where the user's programs keep what can be made again: $XDG_CACHE_HOME, or $HOME/.cache when that is unset or empty, on Unix, $HOME/Library/Caches on macOS, %LocalAppData% on Windows. The directory need not exist.
Details
- The result is
Errwhen the variable it rests on is unset or empty, such as$HOMEon Unix and macOS, or when$XDG_CACHE_HOMEholds a relative path. - On Unix other than macOS, an empty
$XDG_CACHE_HOMEcounts as unset, giving$HOME/.cache. The directory need not exist, as on a new account. - The directory is a root shared by every program. Keep your files under a directory of your program's name, made with File.mkdir, which also creates any parents it lacks.
- Use it for what can be made again, such as downloads and computed results. Settings belong in Os.configDir.
Basic examples
Get the user's cache directory:
println(Os.cacheDir())On macOS this prints Ok{ value: "/Users/ann/Library/Caches" }, and on Linux, with $XDG_CACHE_HOME unset, Ok{ value: "/home/ann/.cache" }.
Unwrap it with try, and name the directory of your program under it with Path.join:
let dir = Path.join([try Os.cacheDir(), "wordcount"])
println(dir)Applications
Keep a computed result between runs. The first call counts the words and saves the count; the next call reads it back:
func cacheFile(name: string): Result[string, Error] {
let dir = Path.join([try Os.cacheDir(), "wordcount"])
try File.mkdir(dir)
Result.ok(Path.join([dir, name]))
}
func countWords(path: string): Result[int, Error] {
let cached = try cacheFile("${Path.base(path)}.count")
if File.exists(cached) {
let saved = (try File.read(cached)).toInt()
if saved != null { return Result.ok(saved) }
}
let words = (try File.read(path)).fields().length()
try File.write(cached, "${words}")
Result.ok(words)
}
try File.write("notes.txt", "buy milk and bread")
println(try countWords("notes.txt"))
println(try countWords("notes.txt"))Let one run of a script at a time do its work, with a lock file at a fixed path. The cache directory is the place for it, not the shared temporary directory, where someone else can take the name first:
let lock = try File.tryLock(Path.join([try Os.cacheDir(), "backup.lock"]))
if lock == null {
eprintln("backup: already running")
Os.exit(1)
}
defer { File.close(lock) }
println("backing up")Clear a program's cache. Everything in it can be made again, so removing it is safe:
let dir = Path.join([try Os.cacheDir(), "wordcount"])
try File.removeAll(dir)
println("cleared ${dir}")Properties and relations
The cache directory follows the platform's rule: ~/Library/Caches on macOS, %LocalAppData% on Windows, and $XDG_CACHE_HOME or ~/.cache elsewhere:
let dir = try Os.cacheDir()
let home = try Os.home()
let expected = if Os.platform() == "darwin" {
Path.join([home, "Library", "Caches"])
} else if Os.platform() == "windows" {
Os.env("LocalAppData")
} else {
Os.env("XDG_CACHE_HOME") ?? Path.join([home, ".cache"])
}
assert(dir == expected)Possible issues
A program started with a bare environment, as some services and containers are, may have no $HOME, and then Os.cacheDir is Err. A try at the top of a script ends it. When the cache only saves time, check the result and go on without it:
let root = Os.cacheDir()
let cache = if root is Ok {
Path.join([root.value, "wordcount"])
} else {
eprintln("wordcount: running without a cache: ${root.error.message}")
null
}
println(cache ?? "no cache")See also
Os.configDir · Os.home · Os.env · File.mkdir · File.tryLock · File.tempDir · Path.join
