Skip to content

Commit

Permalink
add cacheable methods configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Springer committed Jul 21, 2019
1 parent a2b6ad9 commit 37048c8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ http-cache memory adapter takes way less GC pause time, that means smaller GC ov

## Roadmap
- Make it compliant with RFC7234
- Add middleware configuration (cacheable status codes, request methods etc)
- Add middleware configuration (cacheable status codes, paths etc)
- Develop gRPC middleware
- Develop Badger adapter
- Develop DynamoDB adapter
Expand Down
24 changes: 23 additions & 1 deletion cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type Client struct {
adapter Adapter
ttl time.Duration
refreshKey string
methods []string
}

// ClientOption is used to set Client settings.
Expand All @@ -85,7 +86,7 @@ type Adapter interface {
// Middleware is the HTTP cache middleware handler.
func (c *Client) Middleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" || r.Method == "" {
if c.cacheableMethod(r.Method) {
sortURLParams(r.URL)
key := generateKey(r.URL.String())

Expand Down Expand Up @@ -147,6 +148,15 @@ func (c *Client) Middleware(next http.Handler) http.Handler {
})
}

func (c *Client) cacheableMethod(method string) bool {
for _, m := range c.methods {
if method == m {
return true
}
}
return false
}

// BytesToResponse converts bytes array into Response data structure.
func BytesToResponse(b []byte) Response {
var r Response
Expand Down Expand Up @@ -204,6 +214,9 @@ func NewClient(opts ...ClientOption) (*Client, error) {
if int64(c.ttl) < 1 {
return nil, errors.New("cache client ttl is not set")
}
if c.methods == nil {
c.methods = []string{http.MethodGet}
}

return c, nil
}
Expand Down Expand Up @@ -238,3 +251,12 @@ func ClientWithRefreshKey(refreshKey string) ClientOption {
return nil
}
}

// ClientWithMethods sets the acceptable HTTP methods to be cached.
// Optional setting. If not set, default is "GET".
func ClientWithMethods(methods []string) ClientOption {
return func(c *Client) error {
c.methods = methods
return nil
}
}
3 changes: 3 additions & 0 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,13 @@ func TestNewClient(t *testing.T) {
[]ClientOption{
ClientWithAdapter(adapter),
ClientWithTTL(1 * time.Millisecond),
ClientWithMethods([]string{http.MethodGet}),
},
&Client{
adapter: adapter,
ttl: 1 * time.Millisecond,
refreshKey: "",
methods: []string{http.MethodGet},
},
false,
},
Expand All @@ -322,6 +324,7 @@ func TestNewClient(t *testing.T) {
adapter: adapter,
ttl: 1 * time.Millisecond,
refreshKey: "rk",
methods: []string{http.MethodGet},
},
false,
},
Expand Down

0 comments on commit 37048c8

Please sign in to comment.