github.com/gofiber/fiber/v2@v2.47.0/docs/api/middleware/cache.md (about)

     1  ---
     2  id: cache
     3  title: Cache
     4  ---
     5  
     6  Cache middleware for [Fiber](https://github.com/gofiber/fiber) designed to intercept responses and cache them. This middleware will cache the `Body`, `Content-Type` and `StatusCode` using the `c.Path()` as unique identifier. Special thanks to [@codemicro](https://github.com/codemicro/fiber-cache) for creating this middleware for Fiber core!
     7  
     8  Request Directives<br />
     9  `Cache-Control: no-cache` will return the up-to-date response but still caches it. You will always get a `miss` cache status.<br />
    10  `Cache-Control: no-store` will refrain from caching. You will always get the up-to-date response.
    11  
    12  ## Signatures
    13  
    14  ```go
    15  func New(config ...Config) fiber.Handler
    16  ```
    17  
    18  ## Examples
    19  
    20  Import the middleware package that is part of the Fiber web framework
    21  
    22  ```go
    23  import (
    24      "github.com/gofiber/fiber/v2"
    25      "github.com/gofiber/fiber/v2/middleware/cache"
    26  )
    27  ```
    28  
    29  After you initiate your Fiber app, you can use the following possibilities:
    30  
    31  ```go
    32  // Initialize default config
    33  app.Use(cache.New())
    34  
    35  // Or extend your config for customization
    36  app.Use(cache.New(cache.Config{
    37      Next: func(c *fiber.Ctx) bool {
    38          return c.Query("refresh") == "true"
    39      },
    40      Expiration: 30 * time.Minute,
    41      CacheControl: true,
    42  }))
    43  ```
    44  
    45  Or you can custom key and expire time like this:
    46  
    47  ```go
    48  app.Use(cache.New(cache.Config{
    49      ExpirationGenerator: func(c *fiber.Ctx, cfg *cache.Config) time.Duration {
    50          newCacheTime, _ := strconv.Atoi(c.GetRespHeader("Cache-Time", "600"))
    51          return time.Second * time.Duration(newCacheTime)
    52      },
    53      KeyGenerator: func(c *fiber.Ctx) string {
    54  		return utils.CopyString(c.Path())
    55      },
    56  }))
    57  
    58  app.Get("/", func(c *fiber.Ctx) error {
    59      c.Response().Header.Add("Cache-Time", "6000")
    60      return c.SendString("hi")
    61  })
    62  ```
    63  
    64  ## Config
    65  
    66  ```go
    67  // Config defines the config for middleware.
    68  type Config struct {
    69      // Next defines a function to skip this middleware when returned true.
    70      //
    71      // Optional. Default: nil
    72      Next func(c *fiber.Ctx) bool
    73  
    74      // Expiration is the time that an cached response will live
    75      //
    76      // Optional. Default: 1 * time.Minute
    77      Expiration time.Duration
    78  
    79  	// CacheHeader header on response header, indicate cache status, with the following possible return value
    80  	//
    81  	// hit, miss, unreachable
    82  	//
    83  	// Optional. Default: X-Cache
    84  	CacheHeader string
    85  
    86      // CacheControl enables client side caching if set to true
    87      //
    88      // Optional. Default: false
    89      CacheControl bool
    90  
    91      // Key allows you to generate custom keys, by default c.Path() is used
    92      //
    93      // Default: func(c *fiber.Ctx) string {
    94      //   return utils.CopyString(c.Path())
    95      // }
    96      KeyGenerator func(*fiber.Ctx) string
    97  
    98      // allows you to generate custom Expiration Key By Key, default is Expiration (Optional)
    99      //
   100      // Default: nil
   101      ExpirationGenerator func(*fiber.Ctx, *Config) time.Duration
   102  
   103      // Store is used to store the state of the middleware
   104      //
   105      // Default: an in memory store for this process only
   106      Storage fiber.Storage
   107  
   108      // allows you to store additional headers generated by next middlewares & handler
   109      //
   110      // Default: false
   111      StoreResponseHeaders bool
   112  
   113      // Max number of bytes of response bodies simultaneously stored in cache. When limit is reached,
   114      // entries with the nearest expiration are deleted to make room for new.
   115      // 0 means no limit
   116      //
   117      // Default: 0
   118      MaxBytes uint
   119  
   120      // You can specify HTTP methods to cache.
   121      // The middleware just caches the routes of its methods in this slice.
   122      //
   123      // Default: []string{fiber.MethodGet, fiber.MethodHead}
   124      Methods []string
   125  }
   126  ```
   127  
   128  ## Default Config
   129  
   130  ```go
   131  var ConfigDefault = Config{
   132      Next:         nil,
   133      Expiration:   1 * time.Minute,
   134  	CacheHeader:  "X-Cache",
   135      CacheControl: false,
   136      KeyGenerator: func(c *fiber.Ctx) string {
   137          return utils.CopyString(c.Path())
   138      },
   139      ExpirationGenerator:  nil,
   140      StoreResponseHeaders: false,
   141      Storage:              nil,
   142      MaxBytes:             0,
   143      Methods: []string{fiber.MethodGet, fiber.MethodHead},
   144  }
   145  ```