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

Commit

Permalink
feat(cache-control): add cache control handler (#13)
Browse files Browse the repository at this point in the history
* add cache control

* feat(cache-control): add cache control handler

* chore(cache-control): remove unused function

* fix(expiry): add condition for expired response

* chore(example): add example how to use
  • Loading branch information
bxcodec authored Jan 8, 2020
1 parent 4fd5a09 commit 9944a46
Show file tree
Hide file tree
Showing 13 changed files with 1,316 additions and 36 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,13 @@ for i:=0; i< 10; i++ {
//TODO(bxcodec)


## Contribution

## Inspirations and Thanks
- [pquerna/cachecontrol](github.com/pquerna/cachecontrol) for the Cache-Header Extraction


## Contribution
---

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

13 changes: 5 additions & 8 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,17 @@ var (

// Interactor ...
type Interactor interface {
Set(key string, value CachedResponse) error
Set(key string, value CachedResponse, duration time.Duration) error
Get(key string) (CachedResponse, error)
Delete(key string) error
}

// CachedResponse represent the cacher struct item
type CachedResponse struct {
// StatusCode int `json:"statusCode"`
DumpedResponse []byte `json:"response"`
// DumpedBody []byte `json:"body"`
RequestURI string `json:"requestUri"`
RequestMethod string `json:"requestMethod"`
CachedTime time.Time `json:"cachedTime"`
DumpedResponse []byte `json:"response"` // The dumped response body
RequestURI string `json:"requestUri"` // The requestURI of the response
RequestMethod string `json:"requestMethod"` // The HTTP Method that call the request for this response
CachedTime time.Time `json:"cachedTime"` // The timestamp when this response is Cached
}

// Validate will validate the cached response
Expand All @@ -48,6 +46,5 @@ func (c *CachedResponse) Validate() (err error) {
if c.CachedTime.IsZero() {
return ErrInvalidCachedResponse
}

return
}
5 changes: 4 additions & 1 deletion cache/inmem/inmem.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package inmem

import (
"time"

memcache "github.com/bxcodec/gotcha/cache"
"github.com/bxcodec/hache/cache"
)
Expand All @@ -16,7 +18,8 @@ func NewCache(c memcache.Cache) cache.Interactor {
}
}

func (i *inmemCache) Set(key string, value cache.CachedResponse) (err error) {
func (i *inmemCache) Set(key string, value cache.CachedResponse, duration time.Duration) (err error) {
// TODO(bxcodec): add custom duration here based on user response result on the fly
return i.cache.Set(key, value)
}

Expand Down
31 changes: 31 additions & 0 deletions control/cache-control.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package control

import (
// "github.com/pquerna/cachecontrol/cacheobject"
cacheobject "github.com/bxcodec/hache/control/cacheheader"
"net/http"
"time"
)

type Options struct {
// Set to True for a prviate cache, which is not shared amoung users (eg, in a browser)
// Set to False for a "shared" cache, which is more common in a server context.
PrivateCache bool
}

// Given an HTTP Request, the future Status Code, and an ResponseWriter,
// determine the possible reasons a response SHOULD NOT be cached.
func CachableResponseWriter(req *http.Request,
statusCode int,
resp http.ResponseWriter,
opts Options) ([]cacheobject.Reason, time.Time, error) {
return cacheobject.UsingRequestResponse(req, statusCode, resp.Header(), opts.PrivateCache)
}

// Given an HTTP Request and Response, determine the possible reasons a response SHOULD NOT
// be cached.
func CachableResponse(req *http.Request,
resp *http.Response,
opts Options) ([]cacheobject.Reason, time.Time, error) {
return cacheobject.UsingRequestResponse(req, resp.StatusCode, resp.Header, opts.PrivateCache)
}
Loading

0 comments on commit 9944a46

Please sign in to comment.