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

Commit

Permalink
chore(roundtripper): simplify condition and add readme.md (#12)
Browse files Browse the repository at this point in the history
* chore(roundtripper): simplify condition

* docs: add documentations
  • Loading branch information
bxcodec authored Sep 24, 2019
1 parent 468ac0d commit 4fd5a09
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 6 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
.vscode
sample
bin/
88 changes: 86 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -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
5 changes: 3 additions & 2 deletions roundtriper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
32 changes: 32 additions & 0 deletions sample/inmem/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 4fd5a09

Please sign in to comment.