ntfy-alertmanager/cache/cache.go

32 lines
854 B
Go
Raw Normal View History

// Package cache includes a memory cache for ntfy-alertmanager.
package cache
import (
2024-11-09 14:13:33 +01:00
"context"
"fmt"
"strings"
"git.xenrox.net/~xenrox/ntfy-alertmanager/config"
)
// Cache is the interface that describes a cache for ntfy-alertmanager.
type Cache interface {
2024-11-09 14:13:33 +01:00
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.
2024-11-09 14:13:33 +01:00
func NewCache(ctx context.Context, cfg config.CacheConfig) (Cache, error) {
switch strings.ToLower(cfg.Type) {
case "memory":
return NewMemoryCache(cfg.Duration), nil
case "redis":
2024-11-09 14:13:33 +01:00
return NewRedisCache(ctx, cfg.RedisURL, cfg.Duration)
2023-07-12 19:13:09 +02:00
case "disabled":
return NewDisabledCache()
default:
return nil, fmt.Errorf("illegal type %q", cfg.Type)
}
}