Skip to content

Commit 82bac1a

Browse files
committed
优化静态资源 cache
1 parent 3599ddf commit 82bac1a

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

common/middleware/cache.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,19 @@ package common
33
import (
44
"github.com/gin-gonic/gin"
55
"github.com/we7coreteam/w7-rangine-go/v2/src/http/middleware"
6-
http2 "net/http"
76
"strconv"
87
"strings"
9-
"time"
108
)
119

1210
type CacheMiddleware struct {
1311
middleware.Abstract
1412
}
1513

1614
func (self CacheMiddleware) Process(http *gin.Context) {
17-
if strings.HasPrefix(http.Request.URL.Path, "/dpanel/static") {
18-
defaultMaxAge := 604800
19-
http.Writer.Header().Add("Cache-Control", "public, max-age="+strconv.Itoa(defaultMaxAge))
20-
http.Writer.Header().Add("Expires", time.Now().Add(time.Duration(defaultMaxAge)*time.Second).UTC().Format(http2.TimeFormat))
21-
http.Writer.WriteHeader(304)
15+
url := http.Request.URL.Path
16+
if strings.HasPrefix(url, "/dpanel/static") || strings.HasPrefix(url, "/favicon.ico") {
17+
defaultMaxAge := 3600
18+
http.Header("Cache-Control", "public, max-age="+strconv.Itoa(defaultMaxAge))
2219
}
2320
http.Next()
2421
return

0 commit comments

Comments
 (0)