iris服务端缓存

目录结构

主目录clientSide

  1. —— main.go

代码示例

main.go

  1. package main
  2. import (
  3. "time"
  4. "github.com/kataras/iris"
  5. "github.com/kataras/iris/cache"
  6. )
  7. var markdownContents = []byte(`## Hello Markdown
  8. This is a sample of Markdown contents
  9. Features
  10. --------
  11. All features of Sundown are supported, including:
  12. * **Compatibility**. The Markdown v1.0.3 test suite passes with
  13. the --tidy option. Without --tidy, the differences are
  14. mostly in whitespace and entity escaping, where blackfriday is
  15. more consistent and cleaner.
  16. * **Common extensions**, including table support, fenced code
  17. blocks, autolinks, strikethroughs, non-strict emphasis, etc.
  18. * **Safety**. Blackfriday is paranoid when parsing, making it safe
  19. to feed untrusted user input without fear of bad things
  20. happening. The test suite stress tests this and there are no
  21. known inputs that make it crash. If you find one, please let me
  22. know and send me the input that does it.
  23. NOTE: "safety" in this context means *runtime safety only*. In order to
  24. protect yourself against JavaScript injection in untrusted content, see
  25. [this example](https://github.com/russross/blackfriday#sanitize-untrusted-content).
  26. * **Fast processing**. It is fast enough to render on-demand in
  27. most web applications without having to cache the output.
  28. * **Routine safety**. You can run multiple parsers in different
  29. goroutines without ill effect. There is no dependence on global
  30. shared state.
  31. * **Minimal dependencies**. Blackfriday only depends on standard
  32. library packages in Go. The source code is pretty
  33. self-contained, so it is easy to add to any project, including
  34. Google App Engine projects.
  35. * **Standards compliant**. Output successfully validates using the
  36. W3C validation tool for HTML 4.01 and XHTML 1.0 Transitional.
  37. [this is a link](https://github.com/kataras/iris) `)
  38. //不应在包含动态数据的处理程序上使用缓存。
  39. //缓存是静态内容的一个好的和必须的功能,即“关于页面”或整个博客网站,静态网站非常适合。
  40. func main() {
  41. app := iris.New()
  42. app.Logger().SetLevel("debug")
  43. app.Get("/", cache.Handler(10*time.Second), writeMarkdown)
  44. // 将其内容保存在第一个请求中并提供服务而不是重新加载内容。
  45. // 10秒后,它将被清除并重置。
  46. app.Run(iris.Addr(":8080"))
  47. }
  48. func writeMarkdown(ctx iris.Context) {
  49. //点击浏览器的刷新按钮多次,你会每10秒钟只看一次这个println
  50. println("Handler executed. Content refreshed.")
  51. ctx.Markdown(markdownContents)
  52. }
  53. /* 请注意,`StaticWeb`默认使用浏览器的磁盘缓存
  54. 因此,在任何StaticWeb调用之后注册缓存处理程序
  55. 为了更快的解决方案,服务器不需要跟踪响应
  56. */

提示

  • 第一次访问,服务器会返回所有信息,当在缓存时间之内请求服务器,将得到最缓存的信息。过期以后将从新在服务生成。
  • 适合于静态页面做缓存