Skip to content
This repository was archived by the owner on Aug 2, 2022. It is now read-only.

Commit 70136f5

Browse files
committed
add:添加静态资源缓存,优化展示速度
1 parent 59191a0 commit 70136f5

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

middleware/cache.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* @Description:
3+
* @Author: gphper
4+
* @Date: 2021-11-20 18:42:49
5+
*/
6+
package middleware
7+
8+
import (
9+
"strings"
10+
11+
"github.com/gin-gonic/gin"
12+
)
13+
14+
/**
15+
* 静态资源缓存中间件
16+
*/
17+
func StaticCache() gin.HandlerFunc {
18+
return func(c *gin.Context) {
19+
path := c.FullPath()
20+
21+
if strings.Contains(path, "/statics") {
22+
c.Writer.Header().Set("Cache-Control", "max-age=7200")
23+
}
24+
25+
c.Next()
26+
}
27+
}

router/default.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func Init() *gin.Engine {
2020
router := gin.Default()
2121

2222
store := cookie.NewStore([]byte("goredismanagerphper"))
23-
router.Use(gzip.Gzip(gzip.DefaultCompression), sessions.Sessions("goredismanager", store))
23+
router.Use(middleware.StaticCache(), gzip.Gzip(gzip.DefaultCompression), sessions.Sessions("goredismanager", store))
2424

2525
router.GET("/login", controllers.LoginC.ShowLogin)
2626
router.POST("/login", controllers.LoginC.Login)

web/views/layout/layout.html

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
<!--
2-
* @Description:
3-
* @Author: gphper
4-
* @Date: 2021-09-19 21:00:43
5-
-->
61
<!doctype html>
72
<html lang="zh-CN">
83
<head>
94
<!-- 必须的 meta 标签 -->
105
<meta charset="utf-8">
6+
<meta http-equiv="Cache-Control" content="max-age=7200" />
117
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
128
<link rel="icon" href="/statics/images/favicon.ico">
139
<link rel="shortcut icon" href="/statics/images/favicon.ico" type="image/x-icon">

0 commit comments

Comments
 (0)