go-homedir library caches the detected home directory to avoid repeated system calls and command executions. This guide explains how caching works and when to use it.
Why Caching?
Detecting the home directory can be expensive because it may involve:- Reading environment variables
- Executing shell commands (
getent,dscl,sh) - Parsing command output
Dir() return instantly without re-executing these operations.
How Caching Works
The library uses two package-level variables for caching:homedir.go:19-20
homedirCache- Stores the detected home directory pathcacheLock- A read-write mutex for thread-safe access
Cache Check Flow
When you callDir(), it follows this flow:
homedir.go:26-34
- Check if caching is enabled - If
DisableCacheistrue, skip the cache entirely - Acquire read lock - Multiple goroutines can read simultaneously
- Read cached value - Check if a value exists in the cache
- Return immediately - If cached, return without any system calls
Cache Population
If the cache is empty or disabled, the library detects the home directory and populates the cache:homedir.go:36-52
- Acquire write lock - Ensures thread-safe cache updates
- Detect home directory - Calls platform-specific detection
- Store in cache - Only if detection succeeds
- Return result - The detected path or error
The cache is only populated on successful detection. If an error occurs, the cache remains empty, and the next call will retry detection.
Performance Impact
Caching provides significant performance improvements. Based on the benchmark in the test suite:homedir_test.go:25-35
- First call: Full detection (may execute shell commands)
- Subsequent calls: Instant return from cache (nanoseconds)
Disabling the Cache
You can disable caching globally using theDisableCache variable:
When to Disable Caching
Disable caching when:- Environment changes dynamically - If your application modifies
HOMEor other environment variables - Testing - To ensure each test gets fresh detection
- Long-running processes - If you expect the home directory to change during execution
- Security-sensitive contexts - To always get the current system state
Clearing the Cache
You can clear the cache without disabling it using theReset() function:
homedir.go:83-87
Example Usage
When to Use Reset
- After environment changes - When you modify
HOME,USERPROFILE, etc. - In test suites - Between tests that modify environment variables
- Dynamic configuration - When switching between user contexts
Thread Safety
The caching mechanism is fully thread-safe:- Read operations use
RLock()- Multiple goroutines can read simultaneously - Write operations use
Lock()- Exclusive access for cache updates - Reset operations use
Lock()- Exclusive access for cache clearing
The first call from multiple goroutines may result in multiple goroutines entering the detection phase, but the write lock ensures only one updates the cache. This is safe and doesn’t cause issues.
Best Practices
- Keep caching enabled - Unless you have a specific reason to disable it
- Call Reset() after environment changes - When modifying environment variables
- Disable in tests - To ensure predictable behavior with test fixtures
- Don’t worry about thread safety - The library handles it for you