mirror of
https://github.com/gofiber/fiber.git
synced 2025-02-23 14:44:03 +00:00
📦 Add Key option to Cache config (#983)
This commit is contained in:
parent
eea842dc39
commit
1ad625704d
19
middleware/cache/README.md
vendored
19
middleware/cache/README.md
vendored
@ -48,13 +48,25 @@ type Config struct {
|
|||||||
|
|
||||||
// Expiration is the time that an cached response will live
|
// Expiration is the time that an cached response will live
|
||||||
//
|
//
|
||||||
// Optional. Default: 5 * time.Minute
|
// Optional. Default: 1 * time.Minute
|
||||||
Expiration time.Duration
|
Expiration time.Duration
|
||||||
|
|
||||||
// CacheControl enables client side caching if set to true
|
// CacheControl enables client side caching if set to true
|
||||||
//
|
//
|
||||||
// Optional. Default: false
|
// Optional. Default: false
|
||||||
CacheControl bool
|
CacheControl bool
|
||||||
|
|
||||||
|
// Key allows you to generate custom keys, by default c.Path() is used
|
||||||
|
//
|
||||||
|
// Default: func(c *fiber.Ctx) string {
|
||||||
|
// return c.Path()
|
||||||
|
// }
|
||||||
|
Key func(*fiber.Ctx) string
|
||||||
|
|
||||||
|
// Store is used to store the state of the middleware
|
||||||
|
//
|
||||||
|
// Default: an in memory store for this process only
|
||||||
|
Store fiber.Storage
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -63,7 +75,10 @@ type Config struct {
|
|||||||
// ConfigDefault is the default config
|
// ConfigDefault is the default config
|
||||||
var ConfigDefault = Config{
|
var ConfigDefault = Config{
|
||||||
Next: nil,
|
Next: nil,
|
||||||
Expiration: 5 * time.Minute,
|
Expiration: 1 * time.Minute,
|
||||||
CacheControl: false,
|
CacheControl: false,
|
||||||
|
Key: func(c *fiber.Ctx) string {
|
||||||
|
return c.Path()
|
||||||
|
},
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
15
middleware/cache/cache.go
vendored
15
middleware/cache/cache.go
vendored
@ -29,6 +29,13 @@ type Config struct {
|
|||||||
// Optional. Default: false
|
// Optional. Default: false
|
||||||
CacheControl bool
|
CacheControl bool
|
||||||
|
|
||||||
|
// Key allows you to generate custom keys, by default c.Path() is used
|
||||||
|
//
|
||||||
|
// Default: func(c *fiber.Ctx) string {
|
||||||
|
// return c.Path()
|
||||||
|
// }
|
||||||
|
Key func(*fiber.Ctx) string
|
||||||
|
|
||||||
// Store is used to store the state of the middleware
|
// Store is used to store the state of the middleware
|
||||||
//
|
//
|
||||||
// Default: an in memory store for this process only
|
// Default: an in memory store for this process only
|
||||||
@ -44,6 +51,9 @@ var ConfigDefault = Config{
|
|||||||
Next: nil,
|
Next: nil,
|
||||||
Expiration: 1 * time.Minute,
|
Expiration: 1 * time.Minute,
|
||||||
CacheControl: false,
|
CacheControl: false,
|
||||||
|
Key: func(c *fiber.Ctx) string {
|
||||||
|
return c.Path()
|
||||||
|
},
|
||||||
defaultStore: true,
|
defaultStore: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,6 +73,9 @@ func New(config ...Config) fiber.Handler {
|
|||||||
if int(cfg.Expiration.Seconds()) == 0 {
|
if int(cfg.Expiration.Seconds()) == 0 {
|
||||||
cfg.Expiration = ConfigDefault.Expiration
|
cfg.Expiration = ConfigDefault.Expiration
|
||||||
}
|
}
|
||||||
|
if cfg.Key == nil {
|
||||||
|
cfg.Key = ConfigDefault.Key
|
||||||
|
}
|
||||||
if cfg.Store == nil {
|
if cfg.Store == nil {
|
||||||
cfg.defaultStore = true
|
cfg.defaultStore = true
|
||||||
}
|
}
|
||||||
@ -123,7 +136,7 @@ func New(config ...Config) fiber.Handler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get key from request
|
// Get key from request
|
||||||
key := c.Path()
|
key := cfg.Key(c)
|
||||||
|
|
||||||
// Create new entry
|
// Create new entry
|
||||||
var entry entry
|
var entry entry
|
||||||
|
19
middleware/cache/cache_test.go
vendored
19
middleware/cache/cache_test.go
vendored
@ -232,6 +232,25 @@ func Test_Cache_NothingToCache(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_CustomKey(t *testing.T) {
|
||||||
|
app := fiber.New()
|
||||||
|
var called bool
|
||||||
|
app.Use(New(Config{Key: func(c *fiber.Ctx) string {
|
||||||
|
called = true
|
||||||
|
return c.Path()
|
||||||
|
}}))
|
||||||
|
|
||||||
|
app.Get("/", func(c *fiber.Ctx) error {
|
||||||
|
return c.SendString("hi")
|
||||||
|
})
|
||||||
|
|
||||||
|
req := httptest.NewRequest("GET", "/", nil)
|
||||||
|
_, err := app.Test(req)
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
utils.AssertEqual(t, true, called)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// go test -v -run=^$ -bench=Benchmark_Cache -benchmem -count=4
|
// go test -v -run=^$ -bench=Benchmark_Cache -benchmem -count=4
|
||||||
func Benchmark_Cache(b *testing.B) {
|
func Benchmark_Cache(b *testing.B) {
|
||||||
app := fiber.New()
|
app := fiber.New()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user