Skip to content

Commit 9fbd5b6

Browse files
committed
API缓存
1 parent e965c6d commit 9fbd5b6

File tree

6 files changed

+145
-262
lines changed

6 files changed

+145
-262
lines changed

change.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@
3939
- [x] 私有项目,增加访问密码(项目成员可以免密码访问)
4040
- [x] 小分辨率电脑,内容阅读时,不悬浮显示内容菜单,避免影响阅读体验
4141
- [x] 配置管理,增加图片链接由`http://`自动替换为`https://`开关,因为 --> [Chrome 将不再允许 https:// 页面加载 HTTP 资源](https://www.oschina.net/news/110345/chrome-to-block-http-in-https)
42+
- [x] 使用`Cache-Control`实现API缓存设置
4243
- [ ] 项目成员PC端无法访问和编辑项目的问题处理
44+
- [ ] 管理后台,增加一个环境监测,用于检测网站各项依赖是否正常
4345

4446

4547
## V2.1 升级日志

controllers/api/BaseController.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@ func (this *BaseController) Response(httpStatus int, message string, data ...int
166166
if err != nil {
167167
beego.Error(err)
168168
}
169+
170+
if this.Ctx.Request.Method == http.MethodGet && models.GetHTTPCache() > 0 {
171+
this.Ctx.ResponseWriter.Header().Set("Cache-Control", fmt.Sprintf("max-age=%v", models.GetHTTPCache()))
172+
}
173+
169174
this.Ctx.ResponseWriter.Header().Set("Content-Type", "application/json; charset=utf-8")
170175
if strings.Contains(strings.ToLower(this.Ctx.Request.Header.Get("Accept-Encoding")), "gzip") { //gzip压缩
171176
this.Ctx.ResponseWriter.Header().Set("Content-Encoding", "gzip")

favicon.ico

0 Bytes
Binary file not shown.

models/base.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,18 @@ import (
1616
"github.com/astaxie/beego/orm"
1717
)
1818

19+
var httpCache = 0
20+
1921
func Init() {
2022
initAPI()
23+
go func() {
24+
httpCache, _ = strconv.Atoi(GetOptionValue("HTTP_CACHE", "0"))
25+
time.Sleep(60 * time.Second)
26+
}()
27+
}
28+
29+
func GetHTTPCache() int {
30+
return httpCache
2131
}
2232

2333
//设置增减

0 commit comments

Comments
 (0)