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