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 } func createCache() { ipCache = NewCache() } func GetCacheContent(key uint) (uint, bool) { if ipCache == nil { createCache() return 0, false } return ipCache.Get(key) } func SetCacheContent(key, value uint, ttl time.Duration) { if ipCache == nil { createCache() } ipCache.Set(key, value, ttl) }