Add caching to minimize lookup time (closes #12)
This commit is contained in:
parent
177f378715
commit
ec76d2b728
3 changed files with 47 additions and 10 deletions
29
pkg/geoloc/cache.go
Normal file
29
pkg/geoloc/cache.go
Normal file
|
@ -0,0 +1,29 @@
|
|||
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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue