Skip to content

musinit/httpcache

 
 

Repository files navigation

httpcache, inject-able HTTP cache in Golang

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 every line of our code to support the cache features? With only less than 10 line of code, you can got a complete implementations of HTTP Cache based on RFC 7234

Build Status 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/httpcache

Example


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

Short example:

// Inject the HTTP Client with httpcache
client := &http.Client{}
_, err := httpcache.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.

TODOs

Inspirations and Thanks

  • pquerna/cachecontrol for the Cache-Header Extraction
  • bxcodec/gothca for in-memory cache. *Notes: if you find another library that has a better way for inmemm cache, please raise an issue or submit a PR

Contribution


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

About

Get a working HTTP Cache in Go (Golang) with only 3 line of code!!!!

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages

  • Go 99.1%
  • Makefile 0.9%