cache.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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"
  8. "github.com/kataras/iris/cache"
  9. )
  10. func main(){
  11. app := iris.Default()
  12. middleware := cache.Handler(2 *time.Minute)
  13. app.Get("/hello", middleware, h)
  14. app.Run(iris.Addr(":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/cache/client"
  24. "github.com/kataras/iris/context"
  25. )
  26. // Cache accepts the cache expiration duration
  27. // if the expiration <=2 seconds then expiration is taken by the "cache-control's maxage" header
  28. // returns context.Handler, which you can use as your default router or per-route handler
  29. //
  30. // All types of response can be cached, templates, json, text, anything.
  31. //
  32. // Use it for server-side caching, see the `iris#Cache304` for an alternative approach that
  33. // may fit your needs most.
  34. //
  35. // You can add validators with this function.
  36. func Cache(expiration time.Duration) *client.Handler {
  37. return client.NewHandler(expiration)
  38. }
  39. // Handler accepts one single parameter:
  40. // the cache expiration duration
  41. // if the expiration <=2 seconds then expiration is taken by the "cache-control's maxage" header
  42. // returns context.Handler.
  43. //
  44. // All types of response can be cached, templates, json, text, anything.
  45. //
  46. // Use it for server-side caching, see the `iris#Cache304` for an alternative approach that
  47. // may fit your needs most.
  48. //
  49. // it returns a context.Handler which can be used as a middleware, for more options use the `Cache`.
  50. //
  51. // Examples can be found at: https://github.com/kataras/iris/tree/master/_examples/#caching
  52. func Handler(expiration time.Duration) context.Handler {
  53. h := Cache(expiration).ServeHTTP
  54. return h
  55. }