config.go 3.2 KB

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