ntfy-alertmanager/cache/cache.go
2024-11-09 14:13:33 +01:00

31 lines
854 B
Go

// Package cache includes a memory cache for ntfy-alertmanager.
package cache
import (
"context"
"fmt"
"strings"
"git.xenrox.net/~xenrox/ntfy-alertmanager/config"
)
// Cache is the interface that describes a cache for ntfy-alertmanager.
type Cache interface {
Set(ctx context.Context, fingerprint string, status string) error
Contains(ctx context.Context, fingerprint string, status string) (bool, error)
Cleanup()
}
// NewCache reads the cache configuration cfg and creates the cache.
func NewCache(ctx context.Context, cfg config.CacheConfig) (Cache, error) {
switch strings.ToLower(cfg.Type) {
case "memory":
return NewMemoryCache(cfg.Duration), nil
case "redis":
return NewRedisCache(ctx, cfg.RedisURL, cfg.Duration)
case "disabled":
return NewDisabledCache()
default:
return nil, fmt.Errorf("illegal type %q", cfg.Type)
}
}