generated from mrz1836/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
redis.go
55 lines (48 loc) · 1.19 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
package cachestore
import (
"context"
"github.com/gomodule/redigo/redis"
"github.com/mrz1836/go-cache"
"github.com/newrelic/go-agent/v3/newrelic"
)
// loadRedisClient will load the cache client (redis)
func loadRedisClient(
ctx context.Context,
config *RedisConfig,
newRelicEnabled bool,
) (*cache.Client, error) {
// Check for a config
if config == nil || config.URL == "" {
return nil, ErrInvalidRedisConfig
}
// If NewRelic is enabled
if newRelicEnabled {
if txn := newrelic.FromContext(ctx); txn != nil {
segment := txn.StartSegment("load_redis_client")
segment.AddAttribute("url", config.URL)
defer segment.End()
}
}
// Attempt to create the client
client, err := cache.Connect(
ctx,
config.URL,
config.MaxActiveConnections,
config.MaxIdleConnections,
config.MaxConnectionLifetime,
config.MaxIdleTimeout,
config.DependencyMode,
newRelicEnabled,
redis.DialUseTLS(config.UseTLS),
)
if err != nil {
return nil, err
}
// Test the connection if DependencyMode mode is off (no connection tested)
if !config.DependencyMode { // Fire a ping to make sure it works!
if err = cache.Ping(ctx, client); err != nil {
return nil, err
}
}
return client, nil
}