1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-20 03:17:04 +00:00

some major improvements to the (server-side) cache middleware and an example of a client-side responsibility cache

Former-commit-id: 93d3a7a6f163c6d49f315f86d10e63f7b1b1d93a
This commit is contained in:
Gerasimos (Makis) Maropoulos
2018-01-25 15:09:24 +02:00
parent 981322cfd2
commit befb1f0c08
7 changed files with 90 additions and 151 deletions

View File

@@ -4,7 +4,6 @@ import (
"sync"
"time"
"github.com/kataras/iris/cache/cfg"
"github.com/kataras/iris/cache/client/rule"
"github.com/kataras/iris/cache/entry"
"github.com/kataras/iris/context"
@@ -66,6 +65,12 @@ var emptyHandler = func(ctx context.Context) {
ctx.StopExecution()
}
func parseLifeChanger(ctx context.Context) entry.LifeChanger {
return func() time.Duration {
return time.Duration(ctx.MaxAge()) * time.Second
}
}
///TODO: debug this and re-run the parallel tests on larger scale,
// because I think we have a bug here when `core/router#StaticWeb` is used after this middleware.
func (h *Handler) ServeHTTP(ctx context.Context) {
@@ -135,14 +140,19 @@ func (h *Handler) ServeHTTP(ctx context.Context) {
// no need to copy the body, its already done inside
body := recorder.Body()
if len(body) == 0 {
// if no body then just exit
// if no body then just exit.
return
}
// check for an expiration time if the
// given expiration was not valid then check for GetMaxAge &
// update the response & release the recorder
e.Reset(recorder.StatusCode(), recorder.Header().Get(cfg.ContentTypeHeader), body, GetMaxAge(ctx.Request()))
e.Reset(
recorder.StatusCode(),
recorder.Header(),
body,
parseLifeChanger(ctx),
)
// fmt.Printf("reset cache entry\n")
// fmt.Printf("key: %s\n", key)
@@ -152,12 +162,13 @@ func (h *Handler) ServeHTTP(ctx context.Context) {
}
// if it's valid then just write the cached results
ctx.ContentType(response.ContentType())
entry.CopyHeaders(ctx.ResponseWriter().Header(), response.Headers())
context.SetLastModified(ctx, e.LastModified)
ctx.StatusCode(response.StatusCode())
ctx.Write(response.Body())
// fmt.Printf("key: %s\n", key)
// fmt.Printf("write content type: %s\n", response.ContentType())
// fmt.Printf("write content type: %s\n", response.Headers()["ContentType"])
// fmt.Printf("write body len: %d\n", len(response.Body()))
}