forked from thanos-io/thanos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis.go
69 lines (56 loc) · 1.93 KB
/
redis.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
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.
package cache
import (
"context"
"time"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/thanos-io/thanos/pkg/cacheutil"
)
// RedisCache is a redis cache.
type RedisCache struct {
logger log.Logger
redisClient *cacheutil.RedisClient
name string
// Metrics.
requests prometheus.Counter
hits prometheus.Counter
}
// NewRedisCache makes a new RedisCache.
func NewRedisCache(name string, logger log.Logger, redisClient *cacheutil.RedisClient, reg prometheus.Registerer) *RedisCache {
c := &RedisCache{
logger: logger,
redisClient: redisClient,
name: name,
}
c.requests = promauto.With(reg).NewCounter(prometheus.CounterOpts{
Name: "thanos_cache_redis_requests_total",
Help: "Total number of items requests to redis.",
ConstLabels: prometheus.Labels{"name": name},
})
c.hits = promauto.With(reg).NewCounter(prometheus.CounterOpts{
Name: "thanos_cache_redis_hits_total",
Help: "Total number of items requests to the cache that were a hit.",
ConstLabels: prometheus.Labels{"name": name},
})
level.Info(logger).Log("msg", "created redis cache")
return c
}
// Store data identified by keys.
func (c *RedisCache) Store(ctx context.Context, data map[string][]byte, ttl time.Duration) {
c.redisClient.SetMulti(ctx, data, ttl)
}
// Fetch fetches multiple keys and returns a map containing cache hits, along with a list of missing keys.
// In case of error, it logs and return an empty cache hits map.
func (c *RedisCache) Fetch(ctx context.Context, keys []string) map[string][]byte {
c.requests.Add(float64(len(keys)))
results := c.redisClient.GetMulti(ctx, keys)
c.hits.Add(float64(len(results)))
return results
}
func (c *RedisCache) Name() string {
return c.name
}