@@ -11,6 +11,7 @@ import (
11
11
redis_store "github.com/eko/gocache/store/redis/v4"
12
12
gocache "github.com/patrickmn/go-cache"
13
13
"github.com/redis/go-redis/v9"
14
+ log "github.com/sirupsen/logrus"
14
15
)
15
16
16
17
// RedisStoreEnvVar is the environment variable that determines if a redis store should be used.
@@ -19,16 +20,16 @@ const RedisStoreEnvVar = "NB_IDP_CACHE_REDIS_ADDRESS"
19
20
20
21
// NewStore creates a new cache store with the given max timeout and cleanup interval. It checks for the environment Variable RedisStoreEnvVar
21
22
// to determine if a redis store should be used. If the environment variable is set, it will attempt to connect to the redis store.
22
- func NewStore (maxTimeout , cleanupInterval time.Duration ) (store.StoreInterface , error ) {
23
+ func NewStore (ctx context. Context , maxTimeout , cleanupInterval time.Duration ) (store.StoreInterface , error ) {
23
24
redisAddr := os .Getenv (RedisStoreEnvVar )
24
25
if redisAddr != "" {
25
- return getRedisStore (redisAddr )
26
+ return getRedisStore (ctx , redisAddr )
26
27
}
27
28
goc := gocache .New (maxTimeout , cleanupInterval )
28
29
return gocache_store .NewGoCache (goc ), nil
29
30
}
30
31
31
- func getRedisStore (redisEnvAddr string ) (store.StoreInterface , error ) {
32
+ func getRedisStore (ctx context. Context , redisEnvAddr string ) (store.StoreInterface , error ) {
32
33
options , err := redis .ParseURL (redisEnvAddr )
33
34
if err != nil {
34
35
return nil , fmt .Errorf ("parsing redis cache url: %s" , err )
@@ -38,13 +39,15 @@ func getRedisStore(redisEnvAddr string) (store.StoreInterface, error) {
38
39
options .MinIdleConns = 3
39
40
options .MaxActiveConns = 100
40
41
redisClient := redis .NewClient (options )
41
- ctx , cancel := context .WithTimeout (context . Background () , 2 * time .Second )
42
+ subCtx , cancel := context .WithTimeout (ctx , 2 * time .Second )
42
43
defer cancel ()
43
44
44
- _ , err = redisClient .Ping (ctx ).Result ()
45
+ _ , err = redisClient .Ping (subCtx ).Result ()
45
46
if err != nil {
46
47
return nil , err
47
48
}
48
49
50
+ log .WithContext (subCtx ).Infof ("using redis cache at %s" , redisEnvAddr )
51
+
49
52
return redis_store .NewRedis (redisClient ), nil
50
53
}
0 commit comments