Skip to content

Commit

Permalink
🧹 update: add methods configuration for cache middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
efectn committed Sep 9, 2022
1 parent bb10225 commit 236934d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
11 changes: 9 additions & 2 deletions middleware/cache/cache.go
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
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
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
}

1 comment on commit 236934d

@ReneWerner87
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 236934d Previous: 501f769 Ratio
Benchmark_StatusMessage/default 8.792 ns/op 0 B/op 0 allocs/op 3.684 ns/op 0 B/op 0 allocs/op 2.39

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.