theTardigrade / golang-cachedPageDownloader

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

About

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

License:GNU General Public License v3.0


Languages

Language:Go 100.0%