mirror of
https://gitee.com/samwaf/SamWaf.git
synced 2025-12-06 14:59:18 +08:00
feat:cache 增加是否存在检测
This commit is contained in:
13
cache/waf_cache.go
vendored
13
cache/waf_cache.go
vendored
@@ -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()
|
||||||
|
|||||||
9
cache/waf_cache_test.go
vendored
9
cache/waf_cache_test.go
vendored
@@ -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("不存在")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user