mirror of
https://gitee.com/samwaf/SamWaf.git
synced 2026-08-31 01:41:39 +08:00
Vendored
+22
-6
@@ -3,6 +3,7 @@ package cache
|
||||
import (
|
||||
"SamWaf/common/zlog"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -15,7 +16,7 @@ type WafCache struct {
|
||||
type WafCacheItem struct {
|
||||
value interface{}
|
||||
createTime time.Time
|
||||
lastTime time.Time
|
||||
expireTime time.Time
|
||||
ttl time.Duration
|
||||
}
|
||||
|
||||
@@ -28,10 +29,11 @@ func InitWafCache() *WafCache {
|
||||
return wafcache
|
||||
}
|
||||
func (wafCache *WafCache) Set(key string, value interface{}) {
|
||||
wafCache.SetWithTTl(key, value, -1)
|
||||
wafCache.SetWithTTl(key, value, 100*365*24*time.Hour)
|
||||
}
|
||||
|
||||
func (wafCache *WafCache) SetWithTTl(key string, value interface{}, ttl time.Duration) {
|
||||
fmt.Println(ttl)
|
||||
wafCache.mu.Lock()
|
||||
defer wafCache.mu.Unlock()
|
||||
createTime := time.Now()
|
||||
@@ -43,7 +45,21 @@ func (wafCache *WafCache) SetWithTTl(key string, value interface{}, ttl time.Dur
|
||||
wafCache.cache[key] = WafCacheItem{
|
||||
value: value,
|
||||
createTime: createTime,
|
||||
lastTime: time.Now(),
|
||||
expireTime: createTime.Add(ttl),
|
||||
ttl: ttl,
|
||||
}
|
||||
}
|
||||
|
||||
// SetWithTTlRenewTime 并重置时间
|
||||
func (wafCache *WafCache) SetWithTTlRenewTime(key string, value interface{}, ttl time.Duration) {
|
||||
wafCache.mu.Lock()
|
||||
defer wafCache.mu.Unlock()
|
||||
createTime := time.Now()
|
||||
|
||||
wafCache.cache[key] = WafCacheItem{
|
||||
value: value,
|
||||
createTime: createTime,
|
||||
expireTime: createTime.Add(ttl), // 计算过期时间
|
||||
ttl: ttl,
|
||||
}
|
||||
}
|
||||
@@ -98,7 +114,7 @@ func (wafCache *WafCache) Remove(key string) interface{} {
|
||||
delete(wafCache.cache, key)
|
||||
return nil
|
||||
}
|
||||
func (wafCache *WafCache) GetLastTime(key string) (time.Time, error) {
|
||||
func (wafCache *WafCache) GetExpireTime(key string) (time.Time, error) {
|
||||
wafCache.mu.Lock()
|
||||
defer wafCache.mu.Unlock()
|
||||
item, found := wafCache.cache[key]
|
||||
@@ -106,9 +122,9 @@ func (wafCache *WafCache) GetLastTime(key string) (time.Time, error) {
|
||||
return time.Time{}, errors.New("数据不存在")
|
||||
}
|
||||
if time.Since(item.createTime) <= item.ttl {
|
||||
return item.lastTime, nil
|
||||
return item.expireTime, nil
|
||||
}
|
||||
zlog.Debug("GetLastTime CLEAR CACHE EXPIRE :" + key)
|
||||
zlog.Debug("GetExpireTime CLEAR CACHE EXPIRE :" + key)
|
||||
delete(wafCache.cache, key)
|
||||
return time.Time{}, errors.New("数据已过期")
|
||||
}
|
||||
|
||||
Vendored
+11
-3
@@ -16,15 +16,23 @@ func TestWafCache_SetWithTTl(t *testing.T) {
|
||||
time.Sleep(65 * time.Second)
|
||||
|
||||
}
|
||||
func TestWafCache_GetLastTime(t *testing.T) {
|
||||
func TestWafCache_GetExpireTime(t *testing.T) {
|
||||
wafcache := InitWafCache()
|
||||
wafcache.SetWithTTl("KEY1", "我是key1的值", 5*time.Second)
|
||||
key1Value, err := wafcache.GetLastTime("KEY1")
|
||||
wafcache.SetWithTTl("KEY1", "我是key1的值", 5*time.Minute)
|
||||
key1Value, err := wafcache.GetExpireTime("KEY1")
|
||||
if err == nil {
|
||||
println(key1Value.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestWafCache_GetExpireTimeForever(t *testing.T) {
|
||||
wafcache := InitWafCache()
|
||||
wafcache.Set("KEY1", "我是key1的值")
|
||||
key1Value, err := wafcache.GetExpireTime("KEY1")
|
||||
if err == nil {
|
||||
println(key1Value.String())
|
||||
}
|
||||
}
|
||||
func TestWafCache_GetString(t *testing.T) {
|
||||
wafcache := InitWafCache()
|
||||
wafcache.SetWithTTl("KEY1", "我是key1的值字符串", 5*time.Second)
|
||||
|
||||
@@ -19,6 +19,7 @@ var (
|
||||
|
||||
// Auth 鉴权中间件
|
||||
func Auth() gin.HandlerFunc {
|
||||
innerName := "Auth"
|
||||
return func(c *gin.Context) {
|
||||
// 获取请求头中 token,实际是一个完整被签名过的 token;a complete, signed token
|
||||
tokenStr := ""
|
||||
@@ -39,7 +40,7 @@ func Auth() gin.HandlerFunc {
|
||||
//检查是否存在
|
||||
isTokenExist := global.GCACHE_WAFCACHE.IsKeyExist(enums.CACHE_TOKEN + tokenStr)
|
||||
if !isTokenExist {
|
||||
response.AuthFailWithMessage("非法口令", c)
|
||||
response.AuthFailWithMessage("令牌过期", c)
|
||||
c.Abort()
|
||||
return
|
||||
} else {
|
||||
@@ -52,7 +53,23 @@ func Auth() gin.HandlerFunc {
|
||||
return
|
||||
} else {
|
||||
//刷新token时间
|
||||
global.GCACHE_WAFCACHE.SetWithTTl(enums.CACHE_TOKEN+tokenStr, tokenInfo, time.Duration(global.GCONFIG_RECORD_TOKEN_EXPIRE_MINTUTES)*time.Minute)
|
||||
if global.GWAF_RELEASE == "false" {
|
||||
tokenList := global.GCACHE_WAFCACHE.ListAvailableKeysWithPrefix(enums.CACHE_TOKEN)
|
||||
|
||||
for _, duration := range tokenList {
|
||||
remainTime := fmt.Sprintf("%02d时%02d分", int(duration.Hours()), int(duration.Minutes())%60)
|
||||
zlog.Debug(fmt.Sprintf("%v 当前token有效缓存剩余时间 %v", innerName, remainTime))
|
||||
}
|
||||
}
|
||||
expireTime, err := global.GCACHE_WAFCACHE.GetExpireTime(enums.CACHE_TOKEN + tokenStr)
|
||||
if err == nil {
|
||||
remainingTime := time.Until(expireTime) // 计算剩余有效时间
|
||||
if remainingTime > 0 && remainingTime < 2*time.Minute {
|
||||
zlog.Debug(fmt.Sprintf("%v 当前token有效缓存剩余时间 %v 小于2分钟进行缓存可用时间延期处理", innerName, expireTime))
|
||||
global.GCACHE_WAFCACHE.SetWithTTlRenewTime(enums.CACHE_TOKEN+tokenStr, tokenInfo, time.Duration(global.GCONFIG_RECORD_TOKEN_EXPIRE_MINTUTES)*time.Minute)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user