🍃 去除文件缓存

This commit is contained in:
微凉
2020-12-31 15:03:25 +08:00
parent ba7c33a2bb
commit d5e3527bfb
12 changed files with 84 additions and 46 deletions

View File

@ -1,32 +1,31 @@
package controllers
import (
"fmt"
"github.com/Xhofe/alist/alidrive"
"github.com/Xhofe/alist/conf"
"github.com/gin-gonic/gin"
"github.com/patrickmn/go-cache"
log "github.com/sirupsen/logrus"
"strings"
)
// 因为下载地址有时效,所以去掉了文件请求和直链的缓存
func Get(c *gin.Context) {
var get alidrive.GetReq
if err := c.ShouldBindJSON(&get); err != nil {
c.JSON(200,metaResponse(400,"Bad Request"))
return
}
log.Debugf("get:%v",get)
log.Debugf("get:%+v",get)
// cache
cacheKey:=fmt.Sprintf("%s-%s","g",get.FileId)
if conf.Conf.Cache.Enable {
file,exist:=conf.Cache.Get(cacheKey)
if exist {
log.Debugf("使用了缓存:%s",cacheKey)
c.JSON(200,dataResponse(file))
return
}
}
//cacheKey:=fmt.Sprintf("%s-%s","g",get.FileId)
//if conf.Conf.Cache.Enable {
// file,exist:=conf.Cache.Get(cacheKey)
// if exist {
// log.Debugf("使用了缓存:%s",cacheKey)
// c.JSON(200,dataResponse(file))
// return
// }
//}
file,err:=alidrive.GetFile(get.FileId)
if err !=nil {
c.JSON(200,metaResponse(500,err.Error()))
@ -38,9 +37,9 @@ func Get(c *gin.Context) {
return
}
file.Paths=*paths
if conf.Conf.Cache.Enable {
conf.Cache.Set(cacheKey,file,cache.DefaultExpiration)
}
//if conf.Conf.Cache.Enable {
// conf.Cache.Set(cacheKey,file,cache.DefaultExpiration)
//}
c.JSON(200,dataResponse(file))
}
@ -48,23 +47,23 @@ func Down(c *gin.Context) {
fileIdParam:=c.Param("file_id")
log.Debugf("down:%s",fileIdParam)
fileId:=strings.Split(fileIdParam,"/")[1]
cacheKey:=fmt.Sprintf("%s-%s","d",fileId)
if conf.Conf.Cache.Enable {
downloadUrl,exist:=conf.Cache.Get(cacheKey)
if exist {
log.Debugf("使用了缓存:%s",cacheKey)
c.Redirect(301,downloadUrl.(string))
return
}
}
//cacheKey:=fmt.Sprintf("%s-%s","d",fileId)
//if conf.Conf.Cache.Enable {
// downloadUrl,exist:=conf.Cache.Get(cacheKey)
// if exist {
// log.Debugf("使用了缓存:%s",cacheKey)
// c.Redirect(301,downloadUrl.(string))
// return
// }
//}
file,err:=alidrive.GetFile(fileId)
if err != nil {
c.JSON(200, metaResponse(500,err.Error()))
return
}
if conf.Conf.Cache.Enable {
conf.Cache.Set(cacheKey,file.DownloadUrl,cache.DefaultExpiration)
}
//if conf.Conf.Cache.Enable {
// conf.Cache.Set(cacheKey,file.DownloadUrl,cache.DefaultExpiration)
//}
c.Redirect(301,file.DownloadUrl)
return
}