diff --git a/.gitignore b/.gitignore index c133121..2ebcb02 100644 --- a/.gitignore +++ b/.gitignore @@ -11,5 +11,4 @@ # Output of the go coverage tool, specifically when used with LiteIDE *.out .vscode -sample bin/ \ No newline at end of file diff --git a/README.md b/README.md index cf52745..45fd5b6 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,86 @@ -# hache -Cache on Go HTTP Client +# Docs + +Howdy there!!! + +Usually when we want to integrate with cache (let's say Redis), we usually have to do many changes in our code. +What if, we just inject the cache to the HTTP client. So we don't have to create many changes in each line of our code to get the data from Cache, do the validation etc. + +## Introduce Hache: Injecte-able HTTP Cache for Golang HTTP Client + +[![Build Status](https://travis-ci.org/bxcodec/hache.svg?branch=master)](https://travis-ci.org/bxcodec/hache) +[![codecov](https://codecov.io/gh/bxcodec/hache/branch/master/graph/badge.svg)](https://codecov.io/gh/bxcodec/hache) +[![Go Report Card](https://goreportcard.com/badge/github.com/bxcodec/hache)](https://goreportcard.com/report/github.com/bxcodec/hache) +[![License](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/bxcodec/hache/blob/master/LICENSE) +[![GoDoc](https://godoc.org/github.com/bxcodec/hache?status.svg)](https://godoc.org/github.com/bxcodec/hache) + +This package is used for caching your http request results from the server. Example how to use can be seen below. + +## Index + +* [Support](#support) +* [Getting Started](#getting-started) +* [Example](#example) +* [Limitation](#limitation) +* [Contribution](#contribution) + + +## Support + +You can file an [Issue](https://github.com/bxcodec/hache/issues/new). +See documentation in [Godoc](https://godoc.org/github.com/bxcodec/hache) + + +## Getting Started + +#### Download + +```shell +go get -u github.com/bxcodec/hache/v3 +``` +# Example + +--- + +Example how to use more details can be seen in the sample folder: [/sample](/sample) + +Short example: + +```go + +// Inject the HTTP Client with Hache +client := &http.Client{} +err := hache.NewWithInmemoryCache(client, time.Second*60) +if err != nil { + log.Fatal(err) +} + +// And your HTTP Client already supported for HTTP Cache +// To verify you can run a request in a loop + +for i:=0; i< 10; i++ { + startTime := time.Now() + req, err := http.NewRequest("GET", "https://bxcodec.io", nil) + if err != nil { + log.Fatal((err)) + } + + res, err := client.Do(req) + if err != nil { + log.Fatal(err) + } + + fmt.Printf("Response time: %vms\n", time.Since(startTime).Microseconds()) + fmt.Println("Status Code", res.StatusCode) +} +// See the response time, it will different on each request and will go smaller. +``` + +### Inject with your Redis Service +//TODO(bxcodec) + + +## Contribution + +--- + +To contrib to this project, you can open a PR or an issue. \ No newline at end of file diff --git a/go.mod b/go.mod index ee1506e..61d4c76 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,5 @@ module github.com/bxcodec/hache -go 1.12 +go 1.13 require github.com/bxcodec/gotcha v1.0.0-beta.2 diff --git a/roundtriper.go b/roundtriper.go index 868f78d..b8e1b93 100644 --- a/roundtriper.go +++ b/roundtriper.go @@ -143,9 +143,10 @@ func allowedToCache(req *http.Request, resp *http.Response) (ok bool) { func allowedFromCache(req *http.Request) (ok bool) { // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#Cacheability - return strings.ToLower(req.Header.Get(HeaderCacheControl)) != "no-cache" + return !strings.Contains(strings.ToLower(req.Header.Get(HeaderCacheControl)), "no-cache") || + !strings.Contains(strings.ToLower(req.Header.Get(HeaderCacheControl)), "no-store") } func requestMethodValid(req *http.Request) bool { - return req.Method == http.MethodGet || strings.ToLower(req.Method) == "get" + return strings.ToLower(req.Method) == "get" } diff --git a/sample/inmem/main.go b/sample/inmem/main.go new file mode 100644 index 0000000..9824ceb --- /dev/null +++ b/sample/inmem/main.go @@ -0,0 +1,32 @@ +package main + +import ( + "fmt" + "log" + "net/http" + "time" + + "github.com/bxcodec/hache" +) + +func main() { + client := &http.Client{} + err := hache.NewWithInmemoryCache(client, time.Second*60) + if err != nil { + log.Fatal(err) + } + + for i := 0; i < 10; i++ { + startTime := time.Now() + req, err := http.NewRequest("GET", "https://bxcodec.io", nil) + if err != nil { + log.Fatal((err)) + } + res, err := client.Do(req) + if err != nil { + log.Fatal(err) + } + fmt.Printf("Response time: %vms\n", time.Since(startTime).Microseconds()) + fmt.Println("Status Code", res.StatusCode) + } +}