cache: Move creation logic into cache package

This commit is contained in:
Thorben Günther 2023-07-12 19:00:25 +02:00
parent c70b82e9ab
commit b2a1ab61c9
No known key found for this signature in database
GPG key ID: 415CD778D8C5AFED
4 changed files with 31 additions and 37 deletions

19
cache/cache.go vendored
View file

@ -1,9 +1,28 @@
// Package cache includes a memory cache for ntfy-alertmanager.
package cache
import (
"fmt"
"strings"
"git.xenrox.net/~xenrox/ntfy-alertmanager/config"
)
// Cache is the interface that describes a cache for ntfy-alertmanager.
type Cache interface {
Set(fingerprint string, status string) error
Contains(fingerprint string, status string) (bool, error)
Cleanup()
}
// 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)
default:
return nil, fmt.Errorf("illegal type %q", cfg.Type)
}
}

View file

@ -19,15 +19,6 @@ const (
Multi
)
// CacheType is the type of cache that well be used.
type CacheType int
// The different types of caches.
const (
Memory CacheType = iota
Redis
)
// Config is the configuration of the bridge.
type Config struct {
BaseURL string
@ -38,7 +29,7 @@ type Config struct {
Password string
Ntfy ntfyConfig
Labels labels
Cache cacheConfig
Cache CacheConfig
Am alertmanagerConfig
Resolved resolvedConfig
}
@ -65,9 +56,10 @@ type labelConfig struct {
Call string
}
type cacheConfig struct {
// CacheConfig is the configuration of the cache.
type CacheConfig struct {
// shared settings
Type CacheType
Type string
Duration time.Duration
// memory settings
CleanupInterval time.Duration
@ -100,7 +92,7 @@ func ReadConfig(path string) (*Config, error) {
config.LogLevel = "info"
config.AlertMode = Single
config.Cache.Type = Memory
config.Cache.Type = "memory"
config.Cache.Duration = time.Hour * 24
// memory
config.Cache.CleanupInterval = time.Hour
@ -295,19 +287,9 @@ func ReadConfig(path string) (*Config, error) {
if cacheDir != nil {
d = cacheDir.Children.Get("type")
if d != nil {
var cacheType string
if err := d.ParseParams(&cacheType); err != nil {
if err := d.ParseParams(&config.Cache.Type); err != nil {
return nil, err
}
switch strings.ToLower(cacheType) {
case "memory":
config.Cache.Type = Memory
case "redis":
config.Cache.Type = Redis
default:
return nil, fmt.Errorf("cache: illegal type %q", cacheType)
}
}
var durationString string

View file

@ -83,8 +83,8 @@ cache {
"instance:example.com": {Tags: []string{"computer", "example"}},
},
},
Cache: cacheConfig{
Type: Redis,
Cache: CacheConfig{
Type: "redis",
Duration: 48 * time.Hour,
CleanupInterval: time.Hour,
RedisURL: "redis://user:password@localhost:6789/3",

15
main.go
View file

@ -444,16 +444,9 @@ func main() {
client := &httpClient{&http.Client{Timeout: time.Second * 3}}
var c cache.Cache
switch cfg.Cache.Type {
case config.Memory:
c = cache.NewMemoryCache(cfg.Cache.Duration)
case config.Redis:
var err error
c, err = cache.NewRedisCache(cfg.Cache.RedisURL, cfg.Cache.Duration)
if err != nil {
logger.Fatalf("Failed to create redis cache: %v", err)
}
c, err := cache.NewCache(cfg.Cache)
if err != nil {
logger.Fatalf("Failed to create cache: %v", err)
}
bridge := &bridge{cfg: cfg, logger: logger, cache: c, client: client}
@ -468,7 +461,7 @@ func main() {
http.HandleFunc("/silences", bridge.handleSilences)
}
if cfg.Cache.Type == config.Memory {
if _, ok := c.(*cache.MemoryCache); ok {
go bridge.runCleanup()
}
logger.Fatal(http.ListenAndServe(cfg.HTTPAddress, nil))