feat:cache 增加是否存在检测

This commit is contained in:
samwaf
2023-08-24 13:42:56 +08:00
parent b1c2017973
commit cf1dc95127
2 changed files with 22 additions and 0 deletions

13
cache/waf_cache.go vendored
View File

@@ -52,6 +52,19 @@ func (wafCache *WafCache) GetString(key string) (string, error) {
} }
return "", errors.New("数据不存在") return "", errors.New("数据不存在")
} }
func (wafCache *WafCache) IsKeyExist(key string) bool {
wafCache.mu.Lock()
defer wafCache.mu.Unlock()
item, found := wafCache.cache[key]
if !found {
return false
}
if time.Since(item.createTime) <= item.ttl {
return true
}
delete(wafCache.cache, key)
return false
}
func (wafCache *WafCache) Get(key string) interface{} { func (wafCache *WafCache) Get(key string) interface{} {
wafCache.mu.Lock() wafCache.mu.Lock()
defer wafCache.mu.Unlock() defer wafCache.mu.Unlock()

View File

@@ -33,3 +33,12 @@ func TestWafCache_GetString(t *testing.T) {
println(key1Value) println(key1Value)
} }
} }
func TestWafCache_IsKeyExist(t *testing.T) {
wafcache := InitWafCache()
bExist := wafcache.IsKeyExist("KEY1")
if bExist {
println("存在")
} else {
println("不存在")
}
}