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

Commit

Permalink
chore: add sample to use custom storage (#20)
Browse files Browse the repository at this point in the history
* chore: add sample to use custom storage

* chore: remove unused code
  • Loading branch information
bxcodec authored Jun 21, 2020
1 parent 0868d67 commit 427f8ec
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 3 deletions.
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ With only less than 10 line of code, you can got a complete implementations of H
[![Build Status](https://travis-ci.com/bxcodec/httpcache.svg?token=Y64SjWyDK7wXJiFFqV6M&branch=master)](https://travis-ci.com/bxcodec/httpcache)
[![License](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/bxcodec/httpcache/blob/master/LICENSE)
[![GoDoc](https://godoc.org/github.com/bxcodec/httpcache?status.svg)](https://godoc.org/github.com/bxcodec/httpcache)
[![Go.Dev](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white)](https://pkg.go.dev/github.com/bxcodec/httpcache?tab=doc)

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

Expand All @@ -23,7 +24,7 @@ This package is used for caching your http request results from the server. Exam
## Support

You can file an [Issue](https://github.com/bxcodec/httpcache/issues/new).
See documentation in [Godoc](https://godoc.org/github.com/bxcodec/httpcache)
See documentation in [Godoc](https://godoc.org/github.com/bxcodec/httpcache) or in [go.dev](https://pkg.go.dev/github.com/bxcodec/httpcache?tab=doc)


## Getting Started
Expand All @@ -33,11 +34,11 @@ See documentation in [Godoc](https://godoc.org/github.com/bxcodec/httpcache)
```shell
go get -u github.com/bxcodec/httpcache
```
# Example
# Example with Inmemory Storage

---

Example how to use more details can be seen in the sample folder: [/sample](/sample)
Example how to use more details can be seen in the sample folder: [/sample/inmem](/sample/inmem)

Short example:

Expand Down Expand Up @@ -71,6 +72,20 @@ for i:=0; i< 10; i++ {
// See the response time, it will different on each request and will go smaller.
```

# Example with Custom Storage

You also can use your own custom storage, what you need to do is implement the `cache.ICacheInteractor` interface.
Example how to use more details can be seen in the sample folder: [/sample/customstorage](/sample/customstorage)
Example:

```go
client := &http.Client{}
_, err := httpcache.NewWithCustomStorageCache(client, mystorage.NewCustomInMemStorage())
if err != nil {
log.Fatal(err)
}
```

### TODOs
- See the [issues](https://github.com/bxcodec/httpcache/issues)

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ go 1.13

require (
github.com/bxcodec/gotcha v1.0.0-beta.2
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/stretchr/testify v1.4.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/bxcodec/gotcha v1.0.0-beta.2 h1:0jY/Mx6O5jzM2fkcz84zzyy67hLu/bKGJEFTt
github.com/bxcodec/gotcha v1.0.0-beta.2/go.mod h1:MEL9PRYL9Squu1zxreMIzJU6xtMouPmQybWEtXrL1nk=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
Expand Down
52 changes: 52 additions & 0 deletions sample/customstorage/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package main

import (
"fmt"
"io/ioutil"
"log"
"net/http"
"time"

"github.com/bxcodec/httpcache"
"github.com/bxcodec/httpcache/sample/customstorage/mystorage"
)

func main() {
client := &http.Client{}
handler, err := httpcache.NewWithCustomStorageCache(client, mystorage.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)
fmt.Println("Header", res.Header)
// printBody(res)
time.Sleep(time.Second * 1)
fmt.Println("Sequence >>> ", i)
if i%5 == 0 {
err := handler.CacheInteractor.Flush()
if err != nil {
log.Fatal(err)
}
}
}
}

func printBody(resp *http.Response) {
jbyt, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
fmt.Printf("ResponseBody: \t%s\n", string(jbyt))
}
49 changes: 49 additions & 0 deletions sample/customstorage/mystorage/custom.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package mystorage

import (
"time"

"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"
}

0 comments on commit 427f8ec

Please sign in to comment.