项目作者: bake

项目描述 :
Simple HTTP cache
高级语言: Go
项目地址: git://github.com/bake/httpcache.git
创建时间: 2018-10-05T13:09:59Z
项目社区:https://github.com/bake/httpcache

开源协议:MIT License

下载


httpcache

GoDoc
Go Report Card

As simple as it gets HTTP cache. It is heavily inspired by gregjones/httpcache, but it ignores the response headers. If you need a cache that respects these, please take a look at gregjones/httpcache and lox/httpcache.

Usage

  1. cache := memcache.New()
  2. httpClient := httpcache.New(cache).Client()

Per default every response is cached, even POST with a status code outside the range [200-300[. You can specify if a response should get saved by providing a function using the Verify option.

  1. cache := memcache.New()
  2. client := httpcache.New(cache,
  3. httpcache.WithVerifier(func(req *http.Request, res *http.Response) bool {
  4. return res.StatusCode >= 200 && res.StatusCode < 300
  5. }),
  6. ).Client()

A common example, only cache GET requests that don’t fail, could look like the following.

  1. client := httpcache.New(memcache.New(),
  2. httpcache.WithVerifier(httpcache.StatusInTwoHundreds),
  3. httpcache.WithVerifier(httpcache.RequestMethod(http.MethodGet)),
  4. ).Client()

Cache

Currently there are two cache methods: in memory and on disk using diskv. Caches have to implement the Cache interface, which is basically a key-value-store with two functions Get and Set.