Skip to content
This repository was archived by the owner on Mar 8, 2024. It is now read-only.

Commit 4fd5a09

Browse files
authored
chore(roundtripper): simplify condition and add readme.md (#12)
* chore(roundtripper): simplify condition * docs: add documentations
1 parent 468ac0d commit 4fd5a09

5 files changed

Lines changed: 122 additions & 6 deletions

File tree

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,4 @@
1111
# Output of the go coverage tool, specifically when used with LiteIDE
1212
*.out
1313
.vscode
14-
sample
1514
bin/

README.md

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,86 @@
1-
# hache
2-
Cache on Go HTTP Client
1+
# Docs
2+
3+
Howdy there!!!
4+
5+
Usually when we want to integrate with cache (let's say Redis), we usually have to do many changes in our code.
6+
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.
7+
8+
## Introduce Hache: Injecte-able HTTP Cache for Golang HTTP Client
9+
10+
[![Build Status](https://travis-ci.org/bxcodec/hache.svg?branch=master)](https://travis-ci.org/bxcodec/hache)
11+
[![codecov](https://codecov.io/gh/bxcodec/hache/branch/master/graph/badge.svg)](https://codecov.io/gh/bxcodec/hache)
12+
[![Go Report Card](https://goreportcard.com/badge/github.com/bxcodec/hache)](https://goreportcard.com/report/github.com/bxcodec/hache)
13+
[![License](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/bxcodec/hache/blob/master/LICENSE)
14+
[![GoDoc](https://godoc.org/github.com/bxcodec/hache?status.svg)](https://godoc.org/github.com/bxcodec/hache)
15+
16+
This package is used for caching your http request results from the server. Example how to use can be seen below.
17+
18+
## Index
19+
20+
* [Support](#support)
21+
* [Getting Started](#getting-started)
22+
* [Example](#example)
23+
* [Limitation](#limitation)
24+
* [Contribution](#contribution)
25+
26+
27+
## Support
28+
29+
You can file an [Issue](https://github.com/bxcodec/hache/issues/new).
30+
See documentation in [Godoc](https://godoc.org/github.com/bxcodec/hache)
31+
32+
33+
## Getting Started
34+
35+
#### Download
36+
37+
```shell
38+
go get -u github.com/bxcodec/hache/v3
39+
```
40+
# Example
41+
42+
---
43+
44+
Example how to use more details can be seen in the sample folder: [/sample](/sample)
45+
46+
Short example:
47+
48+
```go
49+
50+
// Inject the HTTP Client with Hache
51+
client := &http.Client{}
52+
err := hache.NewWithInmemoryCache(client, time.Second*60)
53+
if err != nil {
54+
log.Fatal(err)
55+
}
56+
57+
// And your HTTP Client already supported for HTTP Cache
58+
// To verify you can run a request in a loop
59+
60+
for i:=0; i< 10; i++ {
61+
startTime := time.Now()
62+
req, err := http.NewRequest("GET", "https://bxcodec.io", nil)
63+
if err != nil {
64+
log.Fatal((err))
65+
}
66+
67+
res, err := client.Do(req)
68+
if err != nil {
69+
log.Fatal(err)
70+
}
71+
72+
fmt.Printf("Response time: %vms\n", time.Since(startTime).Microseconds())
73+
fmt.Println("Status Code", res.StatusCode)
74+
}
75+
// See the response time, it will different on each request and will go smaller.
76+
```
77+
78+
### Inject with your Redis Service
79+
//TODO(bxcodec)
80+
81+
82+
## Contribution
83+
84+
---
85+
86+
To contrib to this project, you can open a PR or an issue.

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module github.com/bxcodec/hache
22

3-
go 1.12
3+
go 1.13
44

55
require github.com/bxcodec/gotcha v1.0.0-beta.2

roundtriper.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,10 @@ func allowedToCache(req *http.Request, resp *http.Response) (ok bool) {
143143

144144
func allowedFromCache(req *http.Request) (ok bool) {
145145
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#Cacheability
146-
return strings.ToLower(req.Header.Get(HeaderCacheControl)) != "no-cache"
146+
return !strings.Contains(strings.ToLower(req.Header.Get(HeaderCacheControl)), "no-cache") ||
147+
!strings.Contains(strings.ToLower(req.Header.Get(HeaderCacheControl)), "no-store")
147148
}
148149

149150
func requestMethodValid(req *http.Request) bool {
150-
return req.Method == http.MethodGet || strings.ToLower(req.Method) == "get"
151+
return strings.ToLower(req.Method) == "get"
151152
}

sample/inmem/main.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"net/http"
7+
"time"
8+
9+
"github.com/bxcodec/hache"
10+
)
11+
12+
func main() {
13+
client := &http.Client{}
14+
err := hache.NewWithInmemoryCache(client, time.Second*60)
15+
if err != nil {
16+
log.Fatal(err)
17+
}
18+
19+
for i := 0; i < 10; i++ {
20+
startTime := time.Now()
21+
req, err := http.NewRequest("GET", "https://bxcodec.io", nil)
22+
if err != nil {
23+
log.Fatal((err))
24+
}
25+
res, err := client.Do(req)
26+
if err != nil {
27+
log.Fatal(err)
28+
}
29+
fmt.Printf("Response time: %vms\n", time.Since(startTime).Microseconds())
30+
fmt.Println("Status Code", res.StatusCode)
31+
}
32+
}

0 commit comments

Comments
 (0)