Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🧹 update: add methods configuration for cache middleware #2081

Merged
merged 2 commits into from
Sep 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions middleware/cache/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ type Config struct {
//
// Default: 0
MaxBytes uint

// You can specify HTTP methods to cache.
// The middleware just caches the routes of its methods in this slice.
//
// Default: []string{fiber.MethodGet, fiber.MethodHead}
Methods []string
}
```

Expand All @@ -144,5 +150,6 @@ var ConfigDefault = Config{
StoreResponseHeaders: false,
Storage: nil,
MaxBytes: 0,
Methods: []string{fiber.MethodGet, fiber.MethodHead},
}
```
11 changes: 9 additions & 2 deletions middleware/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,15 @@ func New(config ...Config) fiber.Handler {

// Return new handler
return func(c *fiber.Ctx) error {
// Only cache GET and HEAD methods
if c.Method() != fiber.MethodGet && c.Method() != fiber.MethodHead {
// Only cache selected methods
var isExists bool
for _, method := range cfg.Methods {
if c.Method() == method {
isExists = true
}
}

if !isExists {
c.Set(cfg.CacheHeader, cacheUnreachable)
return c.Next()
}
Expand Down
46 changes: 45 additions & 1 deletion middleware/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func Test_Cache_Invalid_Expiration(t *testing.T) {
utils.AssertEqual(t, cachedBody, body)
}

func Test_Cache_Invalid_Method(t *testing.T) {
func Test_Cache_Get(t *testing.T) {
t.Parallel()

app := fiber.New()
Expand Down Expand Up @@ -213,6 +213,48 @@ func Test_Cache_Invalid_Method(t *testing.T) {
utils.AssertEqual(t, "123", string(body))
}

func Test_Cache_Post(t *testing.T) {
t.Parallel()

app := fiber.New()

app.Use(New(Config{
Methods: []string{fiber.MethodPost},
}))

app.Post("/", func(c *fiber.Ctx) error {
return c.SendString(c.Query("cache"))
})

app.Get("/get", func(c *fiber.Ctx) error {
return c.SendString(c.Query("cache"))
})

resp, err := app.Test(httptest.NewRequest("POST", "/?cache=123", nil))
utils.AssertEqual(t, nil, err)
body, err := ioutil.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, "123", string(body))

resp, err = app.Test(httptest.NewRequest("POST", "/?cache=12345", nil))
utils.AssertEqual(t, nil, err)
body, err = ioutil.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, "123", string(body))

resp, err = app.Test(httptest.NewRequest("GET", "/get?cache=123", nil))
utils.AssertEqual(t, nil, err)
body, err = ioutil.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, "123", string(body))

resp, err = app.Test(httptest.NewRequest("GET", "/get?cache=12345", nil))
utils.AssertEqual(t, nil, err)
body, err = ioutil.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, "12345", string(body))
}

func Test_Cache_NothingToCache(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -428,10 +470,12 @@ func Test_Cache_WithHead(t *testing.T) {

req := httptest.NewRequest("HEAD", "/", nil)
resp, err := app.Test(req)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, cacheMiss, resp.Header.Get("X-Cache"))

cachedReq := httptest.NewRequest("HEAD", "/", nil)
cachedResp, err := app.Test(cachedReq)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, cacheHit, cachedResp.Header.Get("X-Cache"))

body, err := ioutil.ReadAll(resp.Body)
Expand Down
10 changes: 10 additions & 0 deletions middleware/cache/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ type Config struct {
//
// Default: 0
MaxBytes uint

// You can specify HTTP methods to cache.
// The middleware just caches the routes of its methods in this slice.
//
// Default: []string{fiber.MethodGet, fiber.MethodHead}
Methods []string
}

// ConfigDefault is the default config
Expand All @@ -81,6 +87,7 @@ var ConfigDefault = Config{
StoreResponseHeaders: false,
Storage: nil,
MaxBytes: 0,
Methods: []string{fiber.MethodGet, fiber.MethodHead},
}

// Helper function to set default values
Expand Down Expand Up @@ -114,5 +121,8 @@ func configDefault(config ...Config) Config {
if cfg.KeyGenerator == nil {
cfg.KeyGenerator = ConfigDefault.KeyGenerator
}
if len(cfg.Methods) == 0 {
cfg.Methods = ConfigDefault.Methods
}
return cfg
}