gohelpers/pkg/cache/cache.go
2022-04-07 15:20:21 +08:00

30 lines
515 B
Go

package cache
// 简单缓存 适合小数据量
import (
"sync"
"sync/atomic"
"time"
)
var globalMap sync.Map
var cacheLen int64
func SetCache(key string, data interface{}, timeout int) {
globalMap.Store(key, data)
atomic.AddInt64(&cacheLen, 1)
time.AfterFunc(time.Second*time.Duration(timeout), func() {
atomic.AddInt64(&cacheLen, -1)
globalMap.Delete(key)
})
}
func GetCache(key string) (interface{}, bool) {
return globalMap.Load(key)
}
func DeleteCache(key string) {
globalMap.Delete(key)
}