implement caching (#1)
Co-authored-by: mr0x50 <24775431+mroxso@users.noreply.github.com> Reviewed-on: highperfocused/lumina-relay#1
This commit is contained in:
64
relay/cache/cache.go
vendored
Normal file
64
relay/cache/cache.go
vendored
Normal file
@@ -0,0 +1,64 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type item struct {
|
||||
value interface{}
|
||||
expiration int64
|
||||
}
|
||||
|
||||
type Cache struct {
|
||||
items map[string]item
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
func New() *Cache {
|
||||
cache := &Cache{
|
||||
items: make(map[string]item),
|
||||
}
|
||||
go cache.startCleanup()
|
||||
return cache
|
||||
}
|
||||
|
||||
func (c *Cache) Set(key string, value interface{}, duration time.Duration) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
c.items[key] = item{
|
||||
value: value,
|
||||
expiration: time.Now().Add(duration).UnixNano(),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Cache) Get(key string) (interface{}, bool) {
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
|
||||
item, exists := c.items[key]
|
||||
if !exists {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
if time.Now().UnixNano() > item.expiration {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
return item.value, true
|
||||
}
|
||||
|
||||
func (c *Cache) startCleanup() {
|
||||
ticker := time.NewTicker(time.Minute)
|
||||
for range ticker.C {
|
||||
c.mu.Lock()
|
||||
now := time.Now().UnixNano()
|
||||
for k, v := range c.items {
|
||||
if now > v.expiration {
|
||||
delete(c.items, k)
|
||||
}
|
||||
}
|
||||
c.mu.Unlock()
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user