https://github.com/hinha/httpcache
Golang HTTP Client Cache
https://github.com/hinha/httpcache
cache golang http http-client redis
Last synced: 3 months ago
JSON representation
Golang HTTP Client Cache
- Host: GitHub
- URL: https://github.com/hinha/httpcache
- Owner: hinha
- License: apache-2.0
- Created: 2022-07-16T07:14:53.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2022-07-17T14:08:30.000Z (almost 3 years ago)
- Last Synced: 2025-01-08T09:11:04.651Z (5 months ago)
- Topics: cache, golang, http, http-client, redis
- Language: Go
- Homepage:
- Size: 37.1 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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](https://golang.org/dl/)). 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](https://blog.golang.org/using-go-modules)) inside the folder. Then install httpcache library with the [`go get`](https://golang.org/cmd/go/#hdr-Add_dependencies_to_current_module_and_install_them) command:
```sh
go get -u github.com/hinha/httpcache
```## Example
```go
package mainimport (
"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 headerfor i := 0; i < 10; i++ {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Fatal((err))
}
req.Header = headerres, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
fmt.Println("Status Code", res.StatusCode)
}
}
```## Inspirations and Thanks
- [pquerna/cachecontrol](https://github.com/pquerna/cachecontrol) for the Cache-Header Extraction