/golang-cachedPageDownloader

Go package to download webpages, or access previously cached versions of them.

Primary LanguageGoGNU General Public License v3.0GPL-3.0

golang-cachedPageDownloader

This package makes it easy to download — or access previously cached versions of — webpages.

Go Reference Go Report Card

Example

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
}

Support

If you use this package, or find any value in it, please consider donating:

ko-fi