github.com/gofiber/fiber/v2@v2.47.0/middleware/cache/config.go (about)

     1  package cache
     2  
     3  import (
     4  	"log"
     5  	"time"
     6  
     7  	"github.com/gofiber/fiber/v2"
     8  	"github.com/gofiber/fiber/v2/utils"
     9  )
    10  
    11  // Config defines the config for middleware.
    12  type Config struct {
    13  	// Next defines a function to skip this middleware when returned true.
    14  	//
    15  	// Optional. Default: nil
    16  	Next func(c *fiber.Ctx) bool
    17  
    18  	// Expiration is the time that an cached response will live
    19  	//
    20  	// Optional. Default: 1 * time.Minute
    21  	Expiration time.Duration
    22  
    23  	// CacheHeader header on response header, indicate cache status, with the following possible return value
    24  	//
    25  	// hit, miss, unreachable
    26  	//
    27  	// Optional. Default: X-Cache
    28  	CacheHeader string
    29  
    30  	// CacheControl enables client side caching if set to true
    31  	//
    32  	// Optional. Default: false
    33  	CacheControl bool
    34  
    35  	// Key allows you to generate custom keys, by default c.Path() is used
    36  	//
    37  	// Default: func(c *fiber.Ctx) string {
    38  	//   return utils.CopyString(c.Path())
    39  	// }
    40  	KeyGenerator func(*fiber.Ctx) string
    41  
    42  	// allows you to generate custom Expiration Key By Key, default is Expiration (Optional)
    43  	//
    44  	// Default: nil
    45  	ExpirationGenerator func(*fiber.Ctx, *Config) time.Duration
    46  
    47  	// Store is used to store the state of the middleware
    48  	//
    49  	// Default: an in memory store for this process only
    50  	Storage fiber.Storage
    51  
    52  	// Deprecated: Use Storage instead
    53  	Store fiber.Storage
    54  
    55  	// Deprecated: Use KeyGenerator instead
    56  	Key func(*fiber.Ctx) string
    57  
    58  	// allows you to store additional headers generated by next middlewares & handler
    59  	//
    60  	// Default: false
    61  	StoreResponseHeaders bool
    62  
    63  	// Max number of bytes of response bodies simultaneously stored in cache. When limit is reached,
    64  	// entries with the nearest expiration are deleted to make room for new.
    65  	// 0 means no limit
    66  	//
    67  	// Default: 0
    68  	MaxBytes uint
    69  
    70  	// You can specify HTTP methods to cache.
    71  	// The middleware just caches the routes of its methods in this slice.
    72  	//
    73  	// Default: []string{fiber.MethodGet, fiber.MethodHead}
    74  	Methods []string
    75  }
    76  
    77  // ConfigDefault is the default config
    78  var ConfigDefault = Config{
    79  	Next:         nil,
    80  	Expiration:   1 * time.Minute,
    81  	CacheHeader:  "X-Cache",
    82  	CacheControl: false,
    83  	KeyGenerator: func(c *fiber.Ctx) string {
    84  		return utils.CopyString(c.Path())
    85  	},
    86  	ExpirationGenerator:  nil,
    87  	StoreResponseHeaders: false,
    88  	Storage:              nil,
    89  	MaxBytes:             0,
    90  	Methods:              []string{fiber.MethodGet, fiber.MethodHead},
    91  }
    92  
    93  // Helper function to set default values
    94  func configDefault(config ...Config) Config {
    95  	// Return default config if nothing provided
    96  	if len(config) < 1 {
    97  		return ConfigDefault
    98  	}
    99  
   100  	// Override default config
   101  	cfg := config[0]
   102  
   103  	// Set default values
   104  	if cfg.Store != nil {
   105  		log.Printf("[Warning] - [CACHE] Store is deprecated, please use Storage\n")
   106  		cfg.Storage = cfg.Store
   107  	}
   108  	if cfg.Key != nil {
   109  		log.Printf("[Warning] - [CACHE] Key is deprecated, please use KeyGenerator\n")
   110  		cfg.KeyGenerator = cfg.Key
   111  	}
   112  	if cfg.Next == nil {
   113  		cfg.Next = ConfigDefault.Next
   114  	}
   115  	if int(cfg.Expiration.Seconds()) == 0 {
   116  		cfg.Expiration = ConfigDefault.Expiration
   117  	}
   118  	if cfg.CacheHeader == "" {
   119  		cfg.CacheHeader = ConfigDefault.CacheHeader
   120  	}
   121  	if cfg.KeyGenerator == nil {
   122  		cfg.KeyGenerator = ConfigDefault.KeyGenerator
   123  	}
   124  	if len(cfg.Methods) == 0 {
   125  		cfg.Methods = ConfigDefault.Methods
   126  	}
   127  	return cfg
   128  }