mirror of
https://github.com/kataras/iris.git
synced 2026-03-04 15:36:03 +00:00
add Cache304 as an alternative to the server-side kataras/iris/cache middleware - it can perform better with less server overheat but it comes with a cost of 304 instead of 200 so custom clients must make that check
Former-commit-id: b0ba68c528c870fe060e2825c35689771a1d3680
This commit is contained in:
@@ -382,6 +382,7 @@ The `httptest` package is your way for end-to-end HTTP testing, it uses the http
|
||||
iris cache library lives on its own [package](https://github.com/kataras/iris/tree/master/cache).
|
||||
|
||||
- [Simple](cache/simple/main.go)
|
||||
- [Client-Side (304)](cache/client-side/main.go) - part of the iris context core
|
||||
|
||||
> You're free to use your own favourite caching package if you'd like so.
|
||||
|
||||
|
||||
28
_examples/cache/client-side/main.go
vendored
28
_examples/cache/client-side/main.go
vendored
@@ -6,22 +6,34 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
)
|
||||
|
||||
var modtime = time.Now()
|
||||
|
||||
func greet(ctx iris.Context) {
|
||||
ctx.Header("X-Custom", "my custom header")
|
||||
response := fmt.Sprintf("Hello World! %s", time.Now())
|
||||
ctx.WriteWithExpiration([]byte(response), modtime)
|
||||
}
|
||||
const refreshEvery = 10 * time.Second
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
app.Use(iris.Cache304(refreshEvery))
|
||||
// same as:
|
||||
// app.Use(func(ctx iris.Context) {
|
||||
// now := time.Now()
|
||||
// if modified, err := ctx.CheckIfModifiedSince(now.Add(-refresh)); !modified && err == nil {
|
||||
// ctx.WriteNotModified()
|
||||
// return
|
||||
// }
|
||||
|
||||
// ctx.SetLastModified(now)
|
||||
|
||||
// ctx.Next()
|
||||
// })
|
||||
|
||||
app.Get("/", greet)
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
|
||||
func greet(ctx iris.Context) {
|
||||
ctx.Header("X-Custom", "my custom header")
|
||||
ctx.Writef("Hello World! %s", time.Now())
|
||||
}
|
||||
|
||||
5
_examples/cache/simple/main.go
vendored
5
_examples/cache/simple/main.go
vendored
@@ -73,3 +73,8 @@ func writeMarkdown(ctx iris.Context) {
|
||||
|
||||
ctx.Markdown(markdownContents)
|
||||
}
|
||||
|
||||
/* Note that `StaticWeb` does use the browser's disk caching by-default
|
||||
therefore, register the cache handler AFTER any StaticWeb calls,
|
||||
for a faster solution that server doesn't need to keep track of the response
|
||||
navigate to https://github.com/kataras/iris/blob/master/_examples/cache/client-side/main.go */
|
||||
|
||||
Reference in New Issue
Block a user