ntfy-alertmanager/cache/memory.go
Thorben Günther d35b77fbc9
cache: Move to own interface package
With this interface it will be easy to support multiple cache types.
2023-03-08 16:16:21 +01:00

67 lines
1.3 KiB
Go

package cache
import (
"sync"
"time"
)
// MemoryCache is the in-memory cache.
type MemoryCache struct {
mu sync.Mutex
duration time.Duration
alerts map[string]*cachedAlert
}
type cachedAlert struct {
expires time.Time
status string
}
// NewMemoryCache creates a in-memory cache.
func NewMemoryCache(d time.Duration) Cache {
c := new(MemoryCache)
c.duration = d
c.alerts = make(map[string]*cachedAlert)
return c
}
// Set saves an alert in the cache.
func (c *MemoryCache) Set(fingerprint string, status string) {
c.mu.Lock()
defer c.mu.Unlock()
alert := new(cachedAlert)
alert.expires = time.Now().Add(c.duration)
alert.status = status
c.alerts[fingerprint] = alert
}
// Contains checks if an alert with a given fingerprint is in the cache
// and checks if the status matches.
func (c *MemoryCache) Contains(fingerprint string, status string) bool {
c.mu.Lock()
defer c.mu.Unlock()
alert, ok := c.alerts[fingerprint]
if ok {
return alert.status == status
}
return false
}
func (a *cachedAlert) expired() bool {
return a.expires.Before(time.Now())
}
// Cleanup iterates over all alerts in the cache and removes them, if they
// are expired.
func (c *MemoryCache) Cleanup() {
c.mu.Lock()
defer c.mu.Unlock()
for key, value := range c.alerts {
if value.expired() {
delete(c.alerts, key)
}
}
}