66 lines
1.0 KiB
Go
66 lines
1.0 KiB
Go
|
|
package pokecache
|
||
|
|
|
||
|
|
import (
|
||
|
|
"time"
|
||
|
|
"sync"
|
||
|
|
)
|
||
|
|
|
||
|
|
type cacheEntry struct {
|
||
|
|
createdAt time.Time
|
||
|
|
val []byte
|
||
|
|
}
|
||
|
|
|
||
|
|
type Cache struct {
|
||
|
|
PokeCache map[string]cacheEntry
|
||
|
|
Interval time.Duration
|
||
|
|
Mu sync.Mutex
|
||
|
|
}
|
||
|
|
|
||
|
|
func (c *Cache) Add(key string, val []byte) {
|
||
|
|
newEntry := cacheEntry{
|
||
|
|
createdAt: time.Now(),
|
||
|
|
val: val,
|
||
|
|
}
|
||
|
|
c.Mu.Lock()
|
||
|
|
defer c.Mu.Unlock()
|
||
|
|
c.PokeCache[key] = newEntry
|
||
|
|
}
|
||
|
|
|
||
|
|
func (c *Cache) Get(key string) ([]byte, bool) {
|
||
|
|
c.Mu.Lock()
|
||
|
|
|
||
|
|
defer c.Mu.Unlock()
|
||
|
|
cache, exists := c.PokeCache[key]
|
||
|
|
if !exists {
|
||
|
|
return nil, false
|
||
|
|
}
|
||
|
|
return cache.val, true
|
||
|
|
}
|
||
|
|
|
||
|
|
func (c *Cache) reapLoop() {
|
||
|
|
ticker := time.NewTicker(c.Interval)
|
||
|
|
defer ticker.Stop()
|
||
|
|
|
||
|
|
for range ticker.C {
|
||
|
|
c.Mu.Lock()
|
||
|
|
for k, v := range c.PokeCache {
|
||
|
|
if time.Since(v.createdAt) > c.Interval {
|
||
|
|
delete(c.PokeCache, k)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
c.Mu.Unlock()
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewCache(interval time.Duration) *Cache {
|
||
|
|
newCache := Cache{
|
||
|
|
PokeCache: map[string]cacheEntry{},
|
||
|
|
Interval: interval,
|
||
|
|
Mu: sync.Mutex{},
|
||
|
|
}
|
||
|
|
go newCache.reapLoop()
|
||
|
|
return &newCache
|
||
|
|
}
|
||
|
|
|
||
|
|
|