This package makes it easy to download — or access previously cached versions of — webpages.
package main
import (
"fmt"
"time"
cachedPageDownloader "github.com/theTardigrade/golang-cachedPageDownloader"
)
const (
exampleURL = "https://google.com/"
)
func main() {
downloader, err := cachedPageDownloader.NewDownloader(&cachedPageDownloader.Options{
CacheDir: "./cache",
MaxCacheDuration: time.Minute * 5,
ShouldKeepCacheOnClose: false,
})
if err != nil {
panic(err)
}
defer downloader.Close()
// calling the function below will retrieve the content of the webpage from the internet
content, isFromCache, err := downloader.Download(exampleURL)
if err != nil {
panic(err)
}
fmt.Println(len(content))
fmt.Println(isFromCache) // false
fmt.Println("*****")
// calling the function again will retrieve the content of the webpage from our cache
content, isFromCache, err = downloader.Download(exampleURL)
if err != nil {
panic(err)
}
fmt.Println(len(content))
fmt.Println(isFromCache) // true
}
If you use this package, or find any value in it, please consider donating: