2023-03-08 16:16:21 +01:00
|
|
|
// Package cache includes a memory cache for ntfy-alertmanager.
|
|
|
|
package cache
|
|
|
|
|
2023-07-12 19:00:25 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"git.xenrox.net/~xenrox/ntfy-alertmanager/config"
|
|
|
|
)
|
|
|
|
|
2023-03-08 16:16:21 +01:00
|
|
|
// Cache is the interface that describes a cache for ntfy-alertmanager.
|
|
|
|
type Cache interface {
|
2023-03-08 22:05:15 +01:00
|
|
|
Set(fingerprint string, status string) error
|
|
|
|
Contains(fingerprint string, status string) (bool, error)
|
2023-03-08 16:16:21 +01:00
|
|
|
Cleanup()
|
|
|
|
}
|
2023-07-12 19:00:25 +02:00
|
|
|
|
|
|
|
// NewCache reads the cache configuration cfg and creates the cache.
|
|
|
|
func NewCache(cfg config.CacheConfig) (Cache, error) {
|
|
|
|
switch strings.ToLower(cfg.Type) {
|
|
|
|
case "memory":
|
|
|
|
return NewMemoryCache(cfg.Duration), nil
|
|
|
|
case "redis":
|
|
|
|
return NewRedisCache(cfg.RedisURL, cfg.Duration)
|
2023-07-12 19:13:09 +02:00
|
|
|
case "disabled":
|
|
|
|
return NewDisabledCache()
|
2023-07-12 19:00:25 +02:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("illegal type %q", cfg.Type)
|
|
|
|
}
|
|
|
|
}
|