静态文件缓存
This commit is contained in:
parent
907d5be596
commit
f31f7a4cc0
|
@ -28,6 +28,8 @@ ssl:
|
|||
key: ""
|
||||
|
||||
cacheTime:
|
||||
# 静态资源缓存时间Cache-Control
|
||||
cacheControl: 5d
|
||||
# 最近文章缓存时间
|
||||
recentPostCacheTime: 5m
|
||||
# 分类缓存时间
|
||||
|
|
|
@ -1,15 +1,26 @@
|
|||
package middleware
|
||||
|
||||
import (
|
||||
"github.com/fthvgb1/wp-go/helper/slice"
|
||||
"fmt"
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/config"
|
||||
"github.com/gin-gonic/gin"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var path = map[string]struct{}{
|
||||
"includes": {},
|
||||
"wp-content": {},
|
||||
"favicon.ico": {},
|
||||
}
|
||||
|
||||
func SetStaticFileCache(c *gin.Context) {
|
||||
f := strings.Split(strings.TrimLeft(c.FullPath(), "/"), "/")
|
||||
if len(f) > 0 && slice.IsContained(f[0], []string{"wp-includes", "wp-content", "favicon.ico"}) {
|
||||
c.Header("Cache-Control", "private, max-age=86400")
|
||||
if _, ok := path[f[0]]; ok {
|
||||
t := config.GetConfig().CacheTime.CacheControl
|
||||
if t > 0 {
|
||||
c.Header("Cache-Control", fmt.Sprintf("private, max-age=%d", int(t.Seconds())))
|
||||
}
|
||||
}
|
||||
|
||||
c.Next()
|
||||
}
|
||||
|
|
|
@ -34,6 +34,7 @@ type Config struct {
|
|||
}
|
||||
|
||||
type CacheTime struct {
|
||||
CacheControl time.Duration `yaml:"cacheControl"`
|
||||
RecentPostCacheTime time.Duration `yaml:"recentPostCacheTime"`
|
||||
CategoryCacheTime time.Duration `yaml:"categoryCacheTime"`
|
||||
ArchiveCacheTime time.Duration `yaml:"archiveCacheTime"`
|
||||
|
|
Loading…
Reference in New Issue
Block a user