mirror of
https://github.com/jlelse/GoBlog
synced 2024-06-14 11:27:09 +00:00
Improve Cache
This commit is contained in:
parent
0fba6ddfeb
commit
f1d3fd6ad9
14
cache.go
14
cache.go
|
@ -56,7 +56,7 @@ func cacheMiddleware(next http.Handler) http.Handler {
|
|||
}
|
||||
if ifModifiedSinceHeader := r.Header.Get("If-Modified-Since"); ifModifiedSinceHeader != "" {
|
||||
t, err := dateparse.ParseAny(ifModifiedSinceHeader)
|
||||
if err == nil && t.Unix() >= cache.creationTime {
|
||||
if err == nil && t.After(cache.creationTime) {
|
||||
// send 304
|
||||
w.WriteHeader(http.StatusNotModified)
|
||||
return
|
||||
|
@ -80,12 +80,10 @@ func cacheKey(r *http.Request) string {
|
|||
func setCacheHeaders(w http.ResponseWriter, cache *cacheItem) {
|
||||
w.Header().Del(cacheInternalExpirationHeader)
|
||||
w.Header().Set("ETag", cache.hash)
|
||||
w.Header().Set("Last-Modified", time.Unix(cache.creationTime, 0).UTC().Format(http.TimeFormat))
|
||||
w.Header().Set("Last-Modified", cache.creationTime.UTC().Format(http.TimeFormat))
|
||||
if w.Header().Get("Cache-Control") == "" {
|
||||
if cache.expiration != 0 {
|
||||
expiresIn := cache.creationTime + int64(cache.expiration) - time.Now().Unix()
|
||||
// Set expires time
|
||||
w.Header().Set("Cache-Control", fmt.Sprintf("public,max-age=%d,stale-while-revalidate=%d", expiresIn, cache.expiration))
|
||||
w.Header().Set("Cache-Control", fmt.Sprintf("public,max-age=%d,stale-while-revalidate=%d", cache.expiration, cache.expiration))
|
||||
} else {
|
||||
w.Header().Set("Cache-Control", fmt.Sprintf("public,max-age=%d,s-max-age=%d,stale-while-revalidate=%d", appConfig.Cache.Expiration, appConfig.Cache.Expiration/3, appConfig.Cache.Expiration))
|
||||
}
|
||||
|
@ -94,7 +92,7 @@ func setCacheHeaders(w http.ResponseWriter, cache *cacheItem) {
|
|||
|
||||
type cacheItem struct {
|
||||
expiration int
|
||||
creationTime int64
|
||||
creationTime time.Time
|
||||
hash string
|
||||
code int
|
||||
header http.Header
|
||||
|
@ -103,7 +101,7 @@ type cacheItem struct {
|
|||
|
||||
func (c *cacheItem) expired() bool {
|
||||
if c.expiration != 0 {
|
||||
return c.creationTime < time.Now().Unix()-int64(c.expiration)
|
||||
return time.Now().After(c.creationTime.Add(time.Duration(c.expiration) * time.Second))
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
@ -127,7 +125,7 @@ func getCache(key string, next http.Handler, r *http.Request) (item *cacheItem)
|
|||
exp, _ := strconv.Atoi(result.Header.Get(cacheInternalExpirationHeader))
|
||||
item = &cacheItem{
|
||||
expiration: exp,
|
||||
creationTime: time.Now().Unix(),
|
||||
creationTime: time.Now(),
|
||||
hash: hash,
|
||||
code: result.StatusCode,
|
||||
header: result.Header,
|
||||
|
|
2
go.mod
2
go.mod
|
@ -58,7 +58,7 @@ require (
|
|||
golang.org/x/mod v0.4.0 // indirect
|
||||
golang.org/x/net v0.0.0-20201224014010-6772e930b67b // indirect
|
||||
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a
|
||||
golang.org/x/sys v0.0.0-20210108172913-0df2131ae363 // indirect
|
||||
golang.org/x/sys v0.0.0-20210110051926-789bb1bd4061 // indirect
|
||||
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf // indirect
|
||||
golang.org/x/text v0.3.5 // indirect
|
||||
golang.org/x/tools v0.0.0-20210108195828-e2f9c7f1fc8e // indirect
|
||||
|
|
4
go.sum
4
go.sum
|
@ -454,8 +454,8 @@ golang.org/x/sys v0.0.0-20200724161237-0e2f3a69832c h1:UIcGWL6/wpCfyGuJnRFJRurA+
|
|||
golang.org/x/sys v0.0.0-20200724161237-0e2f3a69832c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210108172913-0df2131ae363 h1:wHn06sgWHMO1VsQ8F+KzDJx/JzqfsNLnc+oEi07qD7s=
|
||||
golang.org/x/sys v0.0.0-20210108172913-0df2131ae363/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210110051926-789bb1bd4061 h1:DQmQoKxQWtyybCtX/3dIuDBcAhFszqq8YiNeS6sNu1c=
|
||||
golang.org/x/sys v0.0.0-20210110051926-789bb1bd4061/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221 h1:/ZHdbVpdR/jk3g30/d4yUL0JU9kksj8+F/bnQUVLGDM=
|
||||
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
|
|
Loading…
Reference in New Issue
Block a user