cache.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* Package cache provides server-side caching capabilities with rich support of options and rules.
  2. Use it for server-side caching, see the `iris#Cache304` for an alternative approach that
  3. may fit your needs most.
  4. Example code:
  5. import (
  6. "time"
  7. "github.com/kataras/iris/v12"
  8. "github.com/kataras/iris/v12/cache"
  9. )
  10. func main(){
  11. app := iris.Default()
  12. middleware := cache.Handler(2 *time.Minute)
  13. app.Get("/hello", middleware, h)
  14. app.Listen(":8080")
  15. }
  16. func h(ctx iris.Context) {
  17. ctx.HTML("<h1> Hello, this should be cached. Every 2 minutes it will be refreshed, check your browser's inspector</h1>")
  18. }
  19. */
  20. package cache
  21. import (
  22. "time"
  23. "github.com/kataras/iris/v12/cache/client"
  24. "github.com/kataras/iris/v12/context"
  25. )
  26. // WithKey sets a custom entry key for cached pages.
  27. // Should be prepended to the cache handler.
  28. //
  29. // Usage:
  30. // app.Get("/", cache.WithKey("custom-key"), cache.Handler(time.Minute), mainHandler)
  31. func WithKey(key string) context.Handler {
  32. return func(ctx *context.Context) {
  33. client.SetKey(ctx, key)
  34. ctx.Next()
  35. }
  36. }
  37. // DefaultMaxAge is a function which returns the
  38. // `context#MaxAge` as time.Duration.
  39. // It's the default expiration function for the cache handler.
  40. var DefaultMaxAge = func(ctx *context.Context) time.Duration {
  41. return time.Duration(ctx.MaxAge()) * time.Second
  42. }
  43. // MaxAge is a shortcut to set a simple duration as a MaxAgeFunc.
  44. //
  45. // Usage:
  46. // app.Get("/", cache.Cache(cache.MaxAge(1*time.Minute), mainHandler)
  47. func MaxAge(dur time.Duration) client.MaxAgeFunc {
  48. return func(*context.Context) time.Duration {
  49. return dur
  50. }
  51. }
  52. // Cache accepts the cache expiration duration.
  53. // If the "maxAgeFunc" input argument is nil,
  54. // then expiration is taken by the "cache-control's maxage" header.
  55. // Returns a Handler structure which you can use to customize cache further.
  56. //
  57. // All types of response can be cached, templates, json, text, anything.
  58. //
  59. // Use it for server-side caching, see the `iris#Cache304` for an alternative approach that
  60. // may be more suited to your needs.
  61. //
  62. // You can add validators with this function.
  63. func Cache(maxAgeFunc client.MaxAgeFunc) *client.Handler {
  64. if maxAgeFunc == nil {
  65. maxAgeFunc = DefaultMaxAge
  66. }
  67. return client.NewHandler(maxAgeFunc)
  68. }
  69. // Handler like `Cache` but returns an Iris Handler to be used as a middleware.
  70. // For more options use the `Cache`.
  71. //
  72. // Examples can be found at: https://github.com/kataras/iris/tree/main/_examples/response-writer/cache
  73. func Handler(expiration time.Duration) context.Handler {
  74. maxAgeFunc := func(*context.Context) time.Duration {
  75. return expiration
  76. }
  77. h := Cache(maxAgeFunc).ServeHTTP
  78. return h
  79. }