-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
1,162 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,49 @@ | ||
package cache | ||
|
||
import ( | ||
"github.com/TensoRaws/NuxBT-Backend/module/util" | ||
"time" | ||
|
||
"github.com/TensoRaws/NuxBT-Backend/module/cache" | ||
"github.com/TensoRaws/NuxBT-Backend/module/log" | ||
"github.com/TensoRaws/NuxBT-Backend/module/util" | ||
"github.com/TensoRaws/NuxBT-Backend/third_party/gin_cache" | ||
"github.com/TensoRaws/NuxBT-Backend/third_party/gin_cache/persist" | ||
"github.com/gin-gonic/gin" | ||
"time" | ||
) | ||
|
||
// Response 缓存接口响应的中间件 | ||
func Response(redisClient *cache.Client, ttl time.Duration) gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
// 生成缓存键,使用请求的 URL 和方法 | ||
cacheKey := c.Request.Method + ":" + c.Request.URL.String() | ||
|
||
// 尝试从缓存中获取响应 | ||
cachedResponse, err := redisClient.Get(cacheKey).Result() | ||
if err == nil { | ||
// 缓存命中,直接返回缓存的响应 | ||
util.OKWithCache(c, cachedResponse) | ||
log.Logger.Debug("Cache hit: " + cacheKey) | ||
return | ||
} | ||
func Response(redisClient *cache.Client, ttl time.Duration, queryFilter []string) gin.HandlerFunc { | ||
redisStore := persist.NewRedisStore(redisClient.C) | ||
|
||
// 缓存未命中,调用后续的处理函数 | ||
c.Next() | ||
|
||
// 调用结束后,将结果存入缓存 | ||
result, exists := c.Get("cache") | ||
if exists { | ||
redisClient.Set(cacheKey, result, ttl) | ||
} | ||
var strategy gin_cache.Option | ||
if queryFilter != nil { | ||
strategy = gin_cache.WithCacheStrategyByRequest(func(c *gin.Context) (bool, gin_cache.Strategy) { | ||
// 剔除 query 参数 | ||
return true, gin_cache.Strategy{ | ||
CacheKey: util.RemoveQueryParameter(c.Request.RequestURI, queryFilter...), | ||
} | ||
}) | ||
} else { | ||
strategy = gin_cache.WithCacheStrategyByRequest(func(c *gin.Context) (bool, gin_cache.Strategy) { | ||
return true, gin_cache.Strategy{ | ||
CacheKey: c.Request.RequestURI, | ||
} | ||
}) | ||
} | ||
|
||
return gin_cache.CacheByRequestURI( | ||
redisStore, | ||
ttl, | ||
gin_cache.WithOnHitCache( | ||
func(c *gin.Context) { | ||
log.Logger.Info("Cache hit: " + c.Request.RequestURI) | ||
}, | ||
), | ||
gin_cache.WithOnMissCache( | ||
func(c *gin.Context) { | ||
log.Logger.Info("Cache miss, try to cache: " + c.Request.RequestURI) | ||
}, | ||
), | ||
strategy, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package util | ||
|
||
import ( | ||
"github.com/TensoRaws/NuxBT-Backend/module/log" | ||
"net/url" | ||
) | ||
|
||
// RemoveQueryParameter 从 URL 中移除一组 query 参数 | ||
func RemoveQueryParameter(rawurl string, keys ...string) string { | ||
u, err := url.Parse(rawurl) | ||
if err != nil { | ||
// 处理错误 | ||
log.Logger.Error("failed to parse url: " + err.Error()) | ||
return rawurl | ||
} | ||
|
||
// 获取原始的 query 参数 | ||
query := u.Query() | ||
|
||
// 删除 query 参数 | ||
for _, key := range keys { | ||
query.Del(key) | ||
} | ||
|
||
// 重新设置 URL 的 query 参数 | ||
u.RawQuery = query.Encode() | ||
|
||
return u.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package util | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestRemoveQueryParameter(t *testing.T) { | ||
type args struct { | ||
rawurl string | ||
keys []string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want string | ||
}{ | ||
// TODO: Add test cases. | ||
{ | ||
name: "case1", | ||
args: args{rawurl: "http://127.0.0.1:7312/?a=1&b=2&c=3", keys: []string{"a", "b"}}, | ||
want: "http://127.0.0.1:7312/?c=3", | ||
}, | ||
{ | ||
name: "case2", | ||
args: args{rawurl: "https://114514.com/?a=1&b=2&c=3", keys: []string{"a", "b", "c"}}, | ||
want: "https://114514.com/", | ||
}, | ||
{ | ||
name: "mock token", | ||
args: args{rawurl: "https://114514.com/?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.ey&sort=top", | ||
keys: []string{"token"}}, | ||
want: "https://114514.com/?sort=top", | ||
}, | ||
{ | ||
name: "invalid query", | ||
args: args{rawurl: "https://114514.com/?a=1&b=2&c=3", keys: []string{"d"}}, | ||
want: "https://114514.com/?a=1&b=2&c=3", | ||
}, | ||
{ | ||
name: "empty query", | ||
args: args{rawurl: "https://114514.com/?token=eyJhb", keys: []string{}}, | ||
want: "https://114514.com/?token=eyJhb", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
assert.Equalf(t, tt.want, RemoveQueryParameter(tt.args.rawurl, tt.args.keys...), | ||
"RemoveQueryParameter(%v, %v)", tt.args.rawurl, tt.args.keys) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# third_party |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2021 cyhone | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
Oops, something went wrong.