mirror of
https://github.com/gofiber/fiber.git
synced 2025-05-31 11:52:41 +00:00
🧹 update: add methods configuration for cache middleware (#2081)
* 🧹 update: add methods configuration for cache middleware * 🧹 update: add methods configuration for cache middleware
This commit is contained in:
parent
f482b303b5
commit
8ec62a64cc
7
middleware/cache/README.md
vendored
7
middleware/cache/README.md
vendored
@ -125,6 +125,12 @@ type Config struct {
|
|||||||
//
|
//
|
||||||
// Default: 0
|
// Default: 0
|
||||||
MaxBytes uint
|
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
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -144,5 +150,6 @@ var ConfigDefault = Config{
|
|||||||
StoreResponseHeaders: false,
|
StoreResponseHeaders: false,
|
||||||
Storage: nil,
|
Storage: nil,
|
||||||
MaxBytes: 0,
|
MaxBytes: 0,
|
||||||
|
Methods: []string{fiber.MethodGet, fiber.MethodHead},
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
11
middleware/cache/cache.go
vendored
11
middleware/cache/cache.go
vendored
@ -83,8 +83,15 @@ func New(config ...Config) fiber.Handler {
|
|||||||
|
|
||||||
// Return new handler
|
// Return new handler
|
||||||
return func(c *fiber.Ctx) error {
|
return func(c *fiber.Ctx) error {
|
||||||
// Only cache GET and HEAD methods
|
// Only cache selected methods
|
||||||
if c.Method() != fiber.MethodGet && c.Method() != fiber.MethodHead {
|
var isExists bool
|
||||||
|
for _, method := range cfg.Methods {
|
||||||
|
if c.Method() == method {
|
||||||
|
isExists = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !isExists {
|
||||||
c.Set(cfg.CacheHeader, cacheUnreachable)
|
c.Set(cfg.CacheHeader, cacheUnreachable)
|
||||||
return c.Next()
|
return c.Next()
|
||||||
}
|
}
|
||||||
|
46
middleware/cache/cache_test.go
vendored
46
middleware/cache/cache_test.go
vendored
@ -173,7 +173,7 @@ func Test_Cache_Invalid_Expiration(t *testing.T) {
|
|||||||
utils.AssertEqual(t, cachedBody, body)
|
utils.AssertEqual(t, cachedBody, body)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_Cache_Invalid_Method(t *testing.T) {
|
func Test_Cache_Get(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
app := fiber.New()
|
app := fiber.New()
|
||||||
@ -213,6 +213,48 @@ func Test_Cache_Invalid_Method(t *testing.T) {
|
|||||||
utils.AssertEqual(t, "123", string(body))
|
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) {
|
func Test_Cache_NothingToCache(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
@ -428,10 +470,12 @@ func Test_Cache_WithHead(t *testing.T) {
|
|||||||
|
|
||||||
req := httptest.NewRequest("HEAD", "/", nil)
|
req := httptest.NewRequest("HEAD", "/", nil)
|
||||||
resp, err := app.Test(req)
|
resp, err := app.Test(req)
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
utils.AssertEqual(t, cacheMiss, resp.Header.Get("X-Cache"))
|
utils.AssertEqual(t, cacheMiss, resp.Header.Get("X-Cache"))
|
||||||
|
|
||||||
cachedReq := httptest.NewRequest("HEAD", "/", nil)
|
cachedReq := httptest.NewRequest("HEAD", "/", nil)
|
||||||
cachedResp, err := app.Test(cachedReq)
|
cachedResp, err := app.Test(cachedReq)
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
utils.AssertEqual(t, cacheHit, cachedResp.Header.Get("X-Cache"))
|
utils.AssertEqual(t, cacheHit, cachedResp.Header.Get("X-Cache"))
|
||||||
|
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
|
10
middleware/cache/config.go
vendored
10
middleware/cache/config.go
vendored
@ -66,6 +66,12 @@ type Config struct {
|
|||||||
//
|
//
|
||||||
// Default: 0
|
// Default: 0
|
||||||
MaxBytes uint
|
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
|
// ConfigDefault is the default config
|
||||||
@ -81,6 +87,7 @@ var ConfigDefault = Config{
|
|||||||
StoreResponseHeaders: false,
|
StoreResponseHeaders: false,
|
||||||
Storage: nil,
|
Storage: nil,
|
||||||
MaxBytes: 0,
|
MaxBytes: 0,
|
||||||
|
Methods: []string{fiber.MethodGet, fiber.MethodHead},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper function to set default values
|
// Helper function to set default values
|
||||||
@ -114,5 +121,8 @@ func configDefault(config ...Config) Config {
|
|||||||
if cfg.KeyGenerator == nil {
|
if cfg.KeyGenerator == nil {
|
||||||
cfg.KeyGenerator = ConfigDefault.KeyGenerator
|
cfg.KeyGenerator = ConfigDefault.KeyGenerator
|
||||||
}
|
}
|
||||||
|
if len(cfg.Methods) == 0 {
|
||||||
|
cfg.Methods = ConfigDefault.Methods
|
||||||
|
}
|
||||||
return cfg
|
return cfg
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user