1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-09 13:05:56 +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

31
cache/entry/entry.go vendored
View File

@@ -13,6 +13,11 @@ type Entry struct {
// ExpiresAt is the time which this cache will not be available
expiresAt time.Time
// when `Reset` this value is reseting to time.Now(),
// it's used to send the "Last-Modified" header,
// some clients may need it.
LastModified time.Time
// Response the response should be served to the client
response *Response
// but we need the key to invalidate manually...xmm
@@ -78,10 +83,23 @@ func (e *Entry) ChangeLifetime(fdur LifeChanger) {
}
}
// CopyHeaders clones headers "src" to "dst" .
func CopyHeaders(dst map[string][]string, src map[string][]string) {
if dst == nil || src == nil {
return
}
for k, vv := range src {
v := make([]string, len(vv))
copy(v, vv)
dst[k] = v
}
}
// Reset called each time the entry is expired
// and the handler calls this after the original handler executed
// to re-set the response with the new handler's content result
func (e *Entry) Reset(statusCode int, contentType string,
func (e *Entry) Reset(statusCode int, headers map[string][]string,
body []byte, lifeChanger LifeChanger) {
if e.response == nil {
@@ -91,8 +109,10 @@ func (e *Entry) Reset(statusCode int, contentType string,
e.response.statusCode = statusCode
}
if contentType != "" {
e.response.contentType = contentType
if len(headers) > 0 {
newHeaders := make(map[string][]string, len(headers))
CopyHeaders(newHeaders, headers)
e.response.headers = newHeaders
}
e.response.body = body
@@ -101,5 +121,8 @@ func (e *Entry) Reset(statusCode int, contentType string,
if lifeChanger != nil {
e.ChangeLifetime(lifeChanger)
}
e.expiresAt = time.Now().Add(e.life)
now := time.Now()
e.expiresAt = now.Add(e.life)
e.LastModified = now
}

View File

@@ -1,19 +1,21 @@
package entry
import "net/http"
// Response is the cached response will be send to the clients
// its fields setted at runtime on each of the non-cached executions
// non-cached executions = first execution, and each time after
// cache expiration datetime passed
// cache expiration datetime passed.
type Response struct {
// statusCode for the response cache handler
// statusCode for the response cache handler.
statusCode int
// contentType for the response cache handler
contentType string
// body is the contents will be served by the cache handler
// body is the contents will be served by the cache handler.
body []byte
// the total headers of the response, including content type.
headers http.Header
}
// StatusCode returns a valid status code
// StatusCode returns a valid status code.
func (r *Response) StatusCode() int {
if r.statusCode <= 0 {
r.statusCode = 200
@@ -22,14 +24,19 @@ func (r *Response) StatusCode() int {
}
// ContentType returns a valid content type
func (r *Response) ContentType() string {
if r.contentType == "" {
r.contentType = "text/html; charset=utf-8"
}
return r.contentType
// func (r *Response) ContentType() string {
// if r.headers == "" {
// r.contentType = "text/html; charset=utf-8"
// }
// return r.contentType
// }
// Headers returns the total headers of the cached response.
func (r *Response) Headers() http.Header {
return r.headers
}
// Body returns contents will be served by the cache handler
// Body returns contents will be served by the cache handler.
func (r *Response) Body() []byte {
return r.body
}