-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
cache.go
57 lines (51 loc) · 1.54 KB
/
cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package setup
import (
"fmt"
"github.com/miekg/dns"
"github.com/qdm12/dns/v2/internal/config"
promcommon "github.com/qdm12/dns/v2/pkg/metrics/prometheus"
"github.com/qdm12/dns/v2/pkg/middlewares/cache/lru"
noopmetrics "github.com/qdm12/dns/v2/pkg/middlewares/cache/metrics/noop"
prommetrics "github.com/qdm12/dns/v2/pkg/middlewares/cache/metrics/prometheus"
"github.com/qdm12/dns/v2/pkg/middlewares/cache/noop"
)
type Cache interface {
Add(request, response *dns.Msg)
Get(request *dns.Msg) (response *dns.Msg)
}
func BuildCache(userSettings config.Cache, //nolint:ireturn
metrics CacheMetrics) (cache Cache, err error) {
switch userSettings.Type {
case noop.CacheType:
return noop.New(noop.Settings{Metrics: metrics}), nil
case lru.CacheType:
return lru.New(lru.Settings{
MaxEntries: userSettings.LRU.MaxEntries,
Metrics: metrics,
})
default:
panic(fmt.Sprintf("unknown cache type: %s", userSettings.Type))
}
}
func BuildCacheMetrics(userSettings config.Metrics, //nolint:ireturn
registry PrometheusRegistry) (
metrics CacheMetrics, err error) {
switch userSettings.Type {
case noopString:
return noopmetrics.New(), nil
case prometheusString:
settings := prommetrics.Settings{
Prometheus: promcommon.Settings{
Registry: registry,
Prefix: *userSettings.Prometheus.Subsystem,
},
}
metrics, err = prommetrics.New(settings)
if err != nil {
return nil, fmt.Errorf("setting up Prometheus metrics: %w", err)
}
default:
panic(fmt.Sprintf("unknown metrics type: %s", userSettings.Type))
}
return metrics, nil
}