From 652d46d9726ad9ceb64b6f11f01ab8e0febda67e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorben=20G=C3=BCnther?= Date: Wed, 8 Mar 2023 16:30:58 +0100 Subject: [PATCH] config: Add cache type --- README.md | 2 ++ config.go | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/README.md b/README.md index 776d0a0..c62f525 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,8 @@ alertmanager { # When the alert-mode is set to single, ntfy-alertmanager will cache each single alert # to avoid sending recurrences. cache { + # The type of cache that will be used (default is memory). + type memory # How long messages stay in the cache for duration 24h # Interval in which the cache is cleaned up diff --git a/config.go b/config.go index cde14b6..056a8a5 100644 --- a/config.go +++ b/config.go @@ -16,6 +16,12 @@ const ( multi ) +type cacheType int + +const ( + memory cacheType = iota +) + type config struct { BaseURL string HTTPAddress string @@ -49,6 +55,7 @@ type labelConfig struct { } type cacheConfig struct { + Type cacheType CleanupInterval time.Duration Duration time.Duration } @@ -77,6 +84,7 @@ func readConfig(path string) (*config, error) { config.LogLevel = "info" config.alertMode = single + config.cache.Type = memory config.cache.CleanupInterval = time.Hour config.cache.Duration = time.Hour * 24 @@ -238,6 +246,21 @@ func readConfig(path string) (*config, error) { cacheDir := cfg.Get("cache") if cacheDir != nil { + d = cacheDir.Children.Get("type") + if d != nil { + var cacheType string + if err := d.ParseParams(&cacheType); err != nil { + return nil, err + } + + switch strings.ToLower(cacheType) { + case "memory": + config.cache.Type = memory + default: + return nil, fmt.Errorf("cache: illegal type %q", cacheType) + } + } + var durationString string d = cacheDir.Children.Get("duration") if d != nil {