Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/crhntr/httpcache
A lightweight http cache.
https://github.com/crhntr/httpcache
go golang http
Last synced: about 1 month ago
JSON representation
A lightweight http cache.
- Host: GitHub
- URL: https://github.com/crhntr/httpcache
- Owner: crhntr
- License: mit
- Created: 2020-12-01T23:23:50.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2020-12-02T00:45:15.000Z (about 4 years ago)
- Last Synced: 2024-06-20T17:30:29.450Z (6 months ago)
- Topics: go, golang, http
- Language: Go
- Homepage:
- Size: 8.79 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# httpcache
Example
```go
package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
"time""github.com/crhntr/httpcache"
)func main() {
const cacheFileName = "/tmp/http_cache"
cache := new(httpcache.HTTPCache)
if err := cache.LoadFromFile(cacheFileName); err != nil {
log.Fatal(err)
}
defer func() {
if err := cache.SaveToFile(cacheFileName); err != nil {
log.Fatal(err)
}
}()http.DefaultClient.Transport = cache
req, _ := http.NewRequest(http.MethodGet, "https://crhntr.com/ip", nil)
last := time.Now()
for i := 0; i < 10; i++ {
res, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Println(err)
continue
}
_, _ = io.Copy(os.Stdout, res.Body)
_ = res.Body.Close()
fmt.Println(time.Since(last), res.StatusCode)
last = time.Now()
}
}```