~mariusor/cache

A package to cache HTTP responses compatible with the Go standard library http.RoundTripper API.
If HEAD request doesn't match the cached version do a full refresh
Expand the validation logic
Add a way to encode a parsed HeaderCacheControl instance to the corresponding string value

refs

master
browse  log 

clone

read-only
https://git.sr.ht/~mariusor/cache
read/write
git@git.sr.ht:~mariusor/cache

You can also use your local clone with git send-email.

#Go HTTP Cache Transport

This package implements http transport functionality compatible with that of the Go standard library http.RoundTripper interface but which saves responses to disk locally based on very basic (at the moment) caching logic.

The simplest way to use is by overriding the http.DefaultTransport.

package main

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

	"git.sr.ht/~mariusor/cache"
)

const uri = "https://example.com"

func main() {
	// Use std library http.DefaultTransport, and save cached requests to /tmp
	http.DefaultTransport = cache.Shared(http.DefaultTransport, cache.FS("/tmp"))

	r, err := http.Get(uri)
	if err != nil {
		panic(err)
	}

	fmt.Printf("Get %s: %s\n", uri, r.Status)
	fmt.Printf("From cache: %t\n", cache.FromStorage(r))
	if age := cache.Age(r); age > 0 {
		fmt.Printf("Age: %s\n", age)
	}
}