cachedPageDownloader

package module
v1.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 17, 2023 License: GPL-3.0 Imports: 14 Imported by: 0

README

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrStatusCodeNotOK = errors.New("status code not ok")
)

Functions

This section is empty.

Types

type Downloader

type Downloader struct {
	// contains filtered or unexported fields
}

func NewDownloader

func NewDownloader(options *Options) (downloader *Downloader, err error)

func (*Downloader) CacheDir

func (downloader *Downloader) CacheDir() string

func (*Downloader) Clean

func (downloader *Downloader) Clean() (err error)

func (*Downloader) Clear

func (downloader *Downloader) Clear() (err error)

func (*Downloader) Close

func (downloader *Downloader) Close() (err error)

func (*Downloader) Download

func (downloader *Downloader) Download(rawURL string) (content []byte, isFromCache bool, err error)

func (*Downloader) MaxCacheDuration

func (downloader *Downloader) MaxCacheDuration() time.Duration

func (*Downloader) ShouldKeepCacheOnClose

func (downloader *Downloader) ShouldKeepCacheOnClose() bool

type Options

type Options struct {
	CacheDir               string
	MaxCacheDuration       time.Duration
	ShouldKeepCacheOnClose bool
}

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL