This example demonstrates using httpcache with persistent disk storage.
- Creating a disk-based cache
- Persistent storage across application restarts
- Cache directory management
- Multiple clients sharing the same cache
From the project root directory:
go run ./examples/diskcache/main.goOr from the examples/diskcache directory:
go run main.goDisk cache is ideal for:
- Desktop applications - Cache API responses between runs
- CLI tools - Speed up repeated commands
- Long-running services - Persist cache across restarts
- Large datasets - When memory cache would be too large
cache := diskcache.New("/path/to/cache/dir")
transport := httpcache.NewTransport(cache)The disk cache uses the diskv library, which provides:
- Efficient key-value storage
- Automatic directory structure
- Built-in compression support (optional)
- Cache directory must be writable
- Files are stored with MD5-hashed filenames
- No automatic cleanup - consider implementing TTL or size limits
- Thread-safe for concurrent access