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

Commit 9944a46

Browse files
authored
feat(cache-control): add cache control handler (#13)
* 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
1 parent 4fd5a09 commit 9944a46

13 files changed

Lines changed: 1316 additions & 36 deletions

File tree

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,13 @@ for i:=0; i< 10; i++ {
7979
//TODO(bxcodec)
8080

8181

82-
## Contribution
8382

83+
## Inspirations and Thanks
84+
- [pquerna/cachecontrol](github.com/pquerna/cachecontrol) for the Cache-Header Extraction
85+
86+
87+
## Contribution
8488
---
8589

86-
To contrib to this project, you can open a PR or an issue.
90+
To contrib to this project, you can open a PR or an issue.
91+

cache/cache.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,17 @@ var (
1616

1717
// Interactor ...
1818
type Interactor interface {
19-
Set(key string, value CachedResponse) error
19+
Set(key string, value CachedResponse, duration time.Duration) error
2020
Get(key string) (CachedResponse, error)
2121
Delete(key string) error
2222
}
2323

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

3432
// Validate will validate the cached response
@@ -48,6 +46,5 @@ func (c *CachedResponse) Validate() (err error) {
4846
if c.CachedTime.IsZero() {
4947
return ErrInvalidCachedResponse
5048
}
51-
5249
return
5350
}

cache/inmem/inmem.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package inmem
22

33
import (
4+
"time"
5+
46
memcache "github.com/bxcodec/gotcha/cache"
57
"github.com/bxcodec/hache/cache"
68
)
@@ -16,7 +18,8 @@ func NewCache(c memcache.Cache) cache.Interactor {
1618
}
1719
}
1820

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

control/cache-control.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package control
2+
3+
import (
4+
// "github.com/pquerna/cachecontrol/cacheobject"
5+
cacheobject "github.com/bxcodec/hache/control/cacheheader"
6+
"net/http"
7+
"time"
8+
)
9+
10+
type Options struct {
11+
// Set to True for a prviate cache, which is not shared amoung users (eg, in a browser)
12+
// Set to False for a "shared" cache, which is more common in a server context.
13+
PrivateCache bool
14+
}
15+
16+
// Given an HTTP Request, the future Status Code, and an ResponseWriter,
17+
// determine the possible reasons a response SHOULD NOT be cached.
18+
func CachableResponseWriter(req *http.Request,
19+
statusCode int,
20+
resp http.ResponseWriter,
21+
opts Options) ([]cacheobject.Reason, time.Time, error) {
22+
return cacheobject.UsingRequestResponse(req, statusCode, resp.Header(), opts.PrivateCache)
23+
}
24+
25+
// Given an HTTP Request and Response, determine the possible reasons a response SHOULD NOT
26+
// be cached.
27+
func CachableResponse(req *http.Request,
28+
resp *http.Response,
29+
opts Options) ([]cacheobject.Reason, time.Time, error) {
30+
return cacheobject.UsingRequestResponse(req, resp.StatusCode, resp.Header, opts.PrivateCache)
31+
}

0 commit comments

Comments
 (0)