43 lines
580 B
Go
43 lines
580 B
Go
package inmemorycache
|
|
|
|
import (
|
|
"sync"
|
|
)
|
|
|
|
func NewCache() *Cache {
|
|
return &Cache{cache: map[uint64][]string{}}
|
|
}
|
|
|
|
type Cache struct {
|
|
cache map[uint64][]string
|
|
mu sync.RWMutex
|
|
}
|
|
|
|
func (c *Cache) Get(id uint64) []string {
|
|
c.mu.RLock()
|
|
defer c.mu.RUnlock()
|
|
|
|
return c.cache[id]
|
|
}
|
|
|
|
func (c *Cache) Append(id uint64, ip string) {
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
|
|
if ips := c.cache[id]; len(ips) == 0 {
|
|
c.cache[id] = append(c.cache[id], ip)
|
|
|
|
return
|
|
}
|
|
|
|
for _, s := range c.cache[id] {
|
|
if s == ip {
|
|
return
|
|
}
|
|
}
|
|
|
|
c.cache[id] = append(c.cache[id], ip)
|
|
|
|
return
|
|
}
|