hache

package module
v1.0.0-alpha Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 8, 2020 License: MIT Imports: 13 Imported by: 0

README

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 codecov Go Report Card License GoDoc

This package is used for caching your http request results from the server. Example how to use can be seen below.

Index

Support

You can file an Issue. See documentation in Godoc

Getting Started

Download
go get -u github.com/bxcodec/hache/v3

Example


Example how to use more details can be seen in the sample folder: /sample

Short example:


// 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)

Inspirations and Thanks

Contribution


To contrib to this project, you can open a PR or an issue.

Documentation

Index

Constants

View Source
const (
	HeaderAuthorization = "Authorization"
	HeaderCacheControl  = "Cache-Control"
)

Headers

Variables

View Source
var (
	// CacheAuthorizedRequest used for determine that a request with Authorization header should be cached or not
	CacheAuthorizedRequest = false // TODO(bxcodec): Need to revised about this feature
)

Functions

func New

func New(client *http.Client, cacheInteractor cache.Interactor) (err error)

New ...

func NewRoundtrip

func NewRoundtrip(defaultRoundTripper http.RoundTripper, cacheActor cache.Interactor) http.RoundTripper

NewRoundtrip will create an implementations of cache http roundtripper

func NewWithInmemoryCache

func NewWithInmemoryCache(client *http.Client, duration ...time.Duration) (err error)

NewWithInmemoryCache will create a complete cache-support of HTTP client with using inmemory cache. If the duration not set, the cache will use LFU algorithm

Types

type RoundTrip

type RoundTrip struct {
	DefaultRoundTripper http.RoundTripper
	CacheInteractor     cache.Interactor
}

RoundTrip custom plugable' struct of implementation of the http.RoundTripper

func (*RoundTrip) RoundTrip

func (r *RoundTrip) RoundTrip(req *http.Request) (resp *http.Response, err error)

RoundTrip the implementation of http.RoundTripper

Directories

Path Synopsis
sample
inmem command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL