cache.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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.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/cache/client"
  24. "github.com/kataras/iris/context"
  25. )
  26. // Cache accepts the cache expiration duration.
  27. // If the "expiration" input argument is invalid, <=2 seconds,
  28. // then expiration is taken by the "cache-control's maxage" header.
  29. // Returns a Handler structure which you can use to customize cache further.
  30. //
  31. // All types of response can be cached, templates, json, text, anything.
  32. //
  33. // Use it for server-side caching, see the `iris#Cache304` for an alternative approach that
  34. // may be more suited to your needs.
  35. //
  36. // You can add validators with this function.
  37. func Cache(expiration time.Duration) *client.Handler {
  38. return client.NewHandler(expiration)
  39. }
  40. // Handler like `Cache` but returns an Iris Handler to be used as a middleware.
  41. // For more options use the `Cache`.
  42. //
  43. // Examples can be found at: https://github.com/kataras/iris/tree/master/_examples/response-writer/cache
  44. func Handler(expiration time.Duration) context.Handler {
  45. h := Cache(expiration).ServeHTTP
  46. return h
  47. }