Documentation
¶
Overview ¶
Example (InMemoryStorageDefault) ¶
package main import ( "fmt" "log" "net/http" "time" "github.com/jose-correia/httpcache" ) func main() { client := &http.Client{} handler, err := httpcache.NewWithInmemoryCache(client, true, time.Second*15) if err != nil { log.Fatal(err) } for i := 0; i < 100; 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: %v micro-second\n", time.Since(startTime).Microseconds()) fmt.Println("Status Code", res.StatusCode) time.Sleep(time.Second * 1) fmt.Println("Sequence >>> ", i) if i%5 == 0 { err := handler.CacheInteractor.Flush() if err != nil { log.Fatal(err) } } } // Example Output: /* 2020/06/21 13:14:51 Cache item's missing failed to retrieve from cache, trying with a live version Response time: 940086 micro-second Status Code 200 Sequence >>> 0 2020/06/21 13:14:53 Cache item's missing failed to retrieve from cache, trying with a live version Response time: 73679 micro-second Status Code 200 Sequence >>> 1 Response time: 126 micro-second Status Code 200 Sequence >>> 2 Response time: 96 micro-second Status Code 200 Sequence >>> 3 Response time: 102 micro-second Status Code 200 Sequence >>> 4 Response time: 94 micro-second Status Code 200 Sequence >>> 5 */ }
Output:
Example (WithCustomStorage) ¶
package main import ( "fmt" "log" "net/http" "time" "github.com/bxcodec/httpcache" "github.com/bxcodec/httpcache/cache" patrickCache "github.com/patrickmn/go-cache" ) type customInMemStorage struct { cacheHandler *patrickCache.Cache } // NewCustomInMemStorage will return a custom in memory cache func NewCustomInMemStorage() cache.ICacheInteractor { return &customInMemStorage{ cacheHandler: patrickCache.New(patrickCache.DefaultExpiration, time.Second*10), } } func (c customInMemStorage) Set(key string, value cache.CachedResponse) error { c.cacheHandler.Set(key, value, patrickCache.DefaultExpiration) return nil } func (c customInMemStorage) Get(key string) (res cache.CachedResponse, err error) { cachedRes, ok := c.cacheHandler.Get(key) if !ok { err = cache.ErrCacheMissed return } res, ok = cachedRes.(cache.CachedResponse) if !ok { err = cache.ErrInvalidCachedResponse return } return } func (c customInMemStorage) Delete(key string) error { c.cacheHandler.Delete(key) return nil } func (c customInMemStorage) Flush() error { c.cacheHandler.Flush() return nil } func (c customInMemStorage) Origin() string { return "MY-OWN-CUSTOM-INMEMORY-CACHED" } func main() { client := &http.Client{} handler, err := httpcache.NewWithCustomStorageCache(client, true, NewCustomInMemStorage()) if err != nil { log.Fatal(err) } for i := 0; i < 100; 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: %v micro-second\n", time.Since(startTime).Microseconds()) fmt.Println("Status Code", res.StatusCode) time.Sleep(time.Second * 1) fmt.Println("Sequence >>> ", i) if i%5 == 0 { err := handler.CacheInteractor.Flush() if err != nil { log.Fatal(err) } } } // Example Output: /* 2020/06/21 13:14:51 Cache item's missing failed to retrieve from cache, trying with a live version Response time: 940086 micro-second Status Code 200 Sequence >>> 0 2020/06/21 13:14:53 Cache item's missing failed to retrieve from cache, trying with a live version Response time: 73679 micro-second Status Code 200 Sequence >>> 1 Response time: 126 micro-second Status Code 200 Sequence >>> 2 Response time: 96 micro-second Status Code 200 Sequence >>> 3 Response time: 102 micro-second Status Code 200 Sequence >>> 4 Response time: 94 micro-second Status Code 200 Sequence >>> 5 */ }
Output:
Index ¶
- Constants
- type CacheHandler
- func NewCacheHandlerRoundtrip(defaultRoundTripper http.RoundTripper, rfcCompliance bool, ...) *CacheHandler
- func NewWithCustomStorageCache(client *http.Client, rfcCompliance bool, ...) (cacheHandler *CacheHandler, err error)
- func NewWithInmemoryCache(client *http.Client, rfcCompliance bool, duration ...time.Duration) (cachedHandler *CacheHandler, err error)
Examples ¶
Constants ¶
View Source
const ( HeaderAuthorization = "Authorization" HeaderCacheControl = "Cache-Control" // To indicate that the response is got from this httpcache library XFromHache = "X-HTTPCache" XHacheOrigin = "X-HTTPCache-Origin" )
Headers
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CacheHandler ¶
type CacheHandler struct { DefaultRoundTripper http.RoundTripper CacheInteractor cache.ICacheInteractor ComplyRFC bool }
CacheHandler custom plugable' struct of implementation of the http.RoundTripper
func NewCacheHandlerRoundtrip ¶
func NewCacheHandlerRoundtrip(defaultRoundTripper http.RoundTripper, rfcCompliance bool, cacheActor cache.ICacheInteractor) *CacheHandler
NewCacheHandlerRoundtrip will create an implementations of cache http roundtripper
func NewWithCustomStorageCache ¶
func NewWithCustomStorageCache(client *http.Client, rfcCompliance bool, cacheInteractor cache.ICacheInteractor) (cacheHandler *CacheHandler, err error)
NewWithCustomStorageCache will initiate the httpcache with your defined cache storage To use your own cache storage handler, you need to implement the cache.Interactor interface And pass it to httpcache.
func NewWithInmemoryCache ¶
func NewWithInmemoryCache(client *http.Client, rfcCompliance bool, duration ...time.Duration) (cachedHandler *CacheHandler, 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
func (*CacheHandler) RFC7234Compliance ¶
func (r *CacheHandler) RFC7234Compliance(val bool) *CacheHandler
RFC7234Compliance used for enable/disable the RFC 7234 compliance
Click to show internal directories.
Click to hide internal directories.