country-geo-locations/pkg/geoloc/cache.go

29 lines
417 B
Go

package geoloc
import (
"sync"
"time"
)
var IPCache *Cache
type Cache struct {
store sync.Map
}
func NewCache() *Cache {
return &Cache{}
}
func (c *Cache) Set(key, value uint, ttl time.Duration) {
c.store.Store(key, value)
time.AfterFunc(ttl, func() {
c.store.Delete(key)
})
}
func (c *Cache) Get(key uint) (uint, bool) {
output, _ := c.store.Load(key)
value, ok := output.(uint)
return value, ok
}