Skip to content

hinha/httpcache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Inject-able HTTP cache in Golang

Just inject the cache to the HTTP client, this will integrate with cache (Redis)

Quickstart

Make sure you have Go installed (download). Version 1.16 or higher is required.

Initialize your project by creating a folder and then running go mod init github.com/your/repo (learn more) inside the folder. Then install httpcache library with the go get command:

go get -u github.com/hinha/httpcache

Example

package main

import (
	"fmt"
	"log"
	"net/http"
	"time"

	"github.com/hinha/httpcache"
	"github.com/hinha/httpcache/cache"
)

var (
	url   = "https://google.com"
	token = "toke"
)

func main() {
	client := &http.Client{}
	// when webStatic is true can only be used static web ex: google.com
	// cause need cache-control
	webStatic := true
	_ = cache.NewRedisCache(client, &httpcache.RedisCacheOptions{
		Addr: "localhost:6379",
	}, time.Second*time.Duration(60))

	header := http.Header{}
	// header.Set("Authorization", token) // if need header

	for i := 0; i < 10; i++ {
		req, err := http.NewRequest("GET", url, nil)
		if err != nil {
			log.Fatal((err))
		}
		req.Header = header

		res, err := client.Do(req)
		if err != nil {
			log.Fatal(err)
		}
		fmt.Println("Status Code", res.StatusCode)
	}
}

Inspirations and Thanks

About

Golang HTTP Client Cache

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages