This repository has been archived on 2023-12-05. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
protect_trans_info/adapters/inmemorycache/cache.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
}