11package redis
22
33import (
4- "context"
5- "fmt"
6- "sync"
7- "time"
4+ "context"
5+ "fmt"
6+ "os"
7+ "sync"
8+ "time"
89
9- "github.com/go-redis/redis/v8"
10+ "github.com/go-redis/redis/v8"
1011)
1112
1213var ctx = context .Background ()
@@ -17,158 +18,157 @@ var redisDB int
1718var clientMutex sync.RWMutex
1819
1920type RedisClient struct {
20- client * redis.Client
21+ client * redis.Client
2122}
2223
2324func InitRedis (addr string , password string , db int ) error {
24- clientMutex .Lock ()
25- defer clientMutex .Unlock ()
26-
27- redisAddr = addr
28- redisPassword = password
29- redisDB = db
30-
31- // Don't use sync.Once with prefork mode - each process needs its own connection
32- redisClient = redis .NewClient (& redis.Options {
33- Addr : addr ,
34- Password : password ,
35- DB : db ,
36- PoolSize : 10 ,
37- MinIdleConns : 5 ,
38- MaxRetries : 3 ,
39- RetryDelay : time .Millisecond * 100 ,
40- PoolTimeout : time .Second * 4 ,
41- IdleTimeout : time .Minute * 5 ,
42- ConnMaxLifetime : time .Hour ,
43- DialTimeout : time .Second * 5 ,
44- ReadTimeout : time .Second * 3 ,
45- WriteTimeout : time .Second * 3 ,
46- PoolFIFO : false ,
47- })
48-
49- ctxTimeout , cancel := context .WithTimeout (ctx , time .Second * 5 )
50- defer cancel ()
51-
52- _ , err := redisClient .Ping (ctxTimeout ).Result ()
53- if err != nil {
54- redisClient .Close ()
55- redisClient = nil
56- return fmt .Errorf ("could not connect to Redis: %v" , err )
57- }
58-
59- fmt .Print ("Redis connected successfully\n " )
60- return nil
25+ clientMutex .Lock ()
26+ defer clientMutex .Unlock ()
27+
28+ redisAddr = addr
29+ redisPassword = password
30+ redisDB = db
31+
32+ // Don't use sync.Once with prefork mode - each process needs its own connection
33+ redisClient = redis .NewClient (& redis.Options {
34+ Addr : addr ,
35+ Password : password ,
36+ DB : db ,
37+ PoolSize : 10 ,
38+ MinIdleConns : 5 ,
39+ MaxRetries : 3 ,
40+ PoolTimeout : time .Second * 4 ,
41+ IdleTimeout : time .Minute * 5 ,
42+ DialTimeout : time .Second * 5 ,
43+ ReadTimeout : time .Second * 3 ,
44+ WriteTimeout : time .Second * 3 ,
45+ PoolFIFO : false ,
46+ })
47+
48+ ctxTimeout , cancel := context .WithTimeout (ctx , time .Second * 5 )
49+ defer cancel ()
50+
51+ _ , err := redisClient .Ping (ctxTimeout ).Result ()
52+ if err != nil {
53+ redisClient .Close ()
54+ redisClient = nil
55+ return fmt .Errorf ("could not connect to Redis: %v" , err )
56+ }
57+
58+ if os .Getenv ("FIBER_PREFORK_CHILD" ) == "" {
59+ fmt .Print ("Redis connected successfully\n " )
60+ }
61+
62+ return nil
6163}
6264
6365func (r * RedisClient ) Set (key string , value interface {}, expirationSeconds int ) error {
64- expiration := time .Duration (expirationSeconds ) * time .Second
65- err := r .client .Set (ctx , key , value , expiration ).Err ()
66- if err != nil {
67- return fmt .Errorf ("could not set value in Redis: %v" , err )
68- }
69- return nil
66+ expiration := time .Duration (expirationSeconds ) * time .Second
67+ err := r .client .Set (ctx , key , value , expiration ).Err ()
68+ if err != nil {
69+ return fmt .Errorf ("could not set value in Redis: %v" , err )
70+ }
71+ return nil
7072}
7173
7274func Get (key string ) (string , error ) {
73- clientMutex .RLock ()
74- client := redisClient
75- clientMutex .RUnlock ()
76-
77- if client == nil {
78- clientMutex .Lock ()
79- if redisClient == nil {
80- if redisAddr != "" {
81- err := InitRedis (redisAddr , redisPassword , redisDB )
82- if err != nil {
83- clientMutex .Unlock ()
84- return "" , fmt .Errorf ("redis client not initialized and re-initialization failed: %v" , err )
85- }
86- } else {
87- clientMutex .Unlock ()
88- return "" , fmt .Errorf ("redis client not initialized. Call InitRedis() first" )
89- }
90- }
91- client = redisClient
92- clientMutex .Unlock ()
93- }
94-
95- val , err := client .Get (ctx , key ).Result ()
96- if err != nil {
97- if err == redis .Nil {
98- return "" , nil
99- }
100- return "" , fmt .Errorf ("could not get value from Redis: %v" , err )
101- }
102- return val , nil
75+ clientMutex .RLock ()
76+ client := redisClient
77+ clientMutex .RUnlock ()
78+
79+ if client == nil {
80+ clientMutex .Lock ()
81+ if redisClient == nil {
82+ if redisAddr != "" {
83+ err := InitRedis (redisAddr , redisPassword , redisDB )
84+ if err != nil {
85+ clientMutex .Unlock ()
86+ return "" , fmt .Errorf ("redis client not initialized and re-initialization failed: %v" , err )
87+ }
88+ } else {
89+ clientMutex .Unlock ()
90+ return "" , fmt .Errorf ("redis client not initialized. Call InitRedis() first" )
91+ }
92+ }
93+ client = redisClient
94+ clientMutex .Unlock ()
95+ }
96+
97+ val , err := client .Get (ctx , key ).Result ()
98+ if err != nil {
99+ if err == redis .Nil {
100+ return "" , nil
101+ }
102+ return "" , fmt .Errorf ("could not get value from Redis: %v" , err )
103+ }
104+ return val , nil
103105}
104106
105107func NewRedisClient (addr string , password string , db int ) * RedisClient {
106- rdb := redis .NewClient (& redis.Options {
107- Addr : addr ,
108- Password : password ,
109- DB : db ,
110- PoolSize : 10 ,
111- MinIdleConns : 5 ,
112- MaxRetries : 3 ,
113- RetryDelay : time .Millisecond * 100 ,
114- PoolTimeout : time .Second * 4 ,
115- IdleTimeout : time .Minute * 5 ,
116- ConnMaxLifetime : time .Hour ,
117- DialTimeout : time .Second * 5 ,
118- ReadTimeout : time .Second * 3 ,
119- WriteTimeout : time .Second * 3 ,
120- PoolFIFO : false ,
121- })
122-
123- ctxTimeout , cancel := context .WithTimeout (ctx , time .Second * 5 )
124- defer cancel ()
125-
126- _ , err := rdb .Ping (ctxTimeout ).Result ()
127- if err != nil {
128- panic (fmt .Errorf ("could not connect to Redis: %v" , err ))
129- }
130-
131- return & RedisClient {client : rdb }
108+ rdb := redis .NewClient (& redis.Options {
109+ Addr : addr ,
110+ Password : password ,
111+ DB : db ,
112+ PoolSize : 10 ,
113+ MinIdleConns : 5 ,
114+ MaxRetries : 3 ,
115+ PoolTimeout : time .Second * 4 ,
116+ IdleTimeout : time .Minute * 5 ,
117+ DialTimeout : time .Second * 5 ,
118+ ReadTimeout : time .Second * 3 ,
119+ WriteTimeout : time .Second * 3 ,
120+ PoolFIFO : false ,
121+ })
122+
123+ ctxTimeout , cancel := context .WithTimeout (ctx , time .Second * 5 )
124+ defer cancel ()
125+
126+ _ , err := rdb .Ping (ctxTimeout ).Result ()
127+ if err != nil {
128+ panic (fmt .Errorf ("could not connect to Redis: %v" , err ))
129+ }
130+
131+ return & RedisClient {client : rdb }
132132}
133133
134134func Set (key string , value interface {}, expirationSeconds int ) error {
135- clientMutex .RLock ()
136- client := redisClient
137- clientMutex .RUnlock ()
138-
139- if client == nil {
140- clientMutex .Lock ()
141- if redisClient == nil {
142- if redisAddr != "" {
143- err := InitRedis (redisAddr , redisPassword , redisDB )
144- if err != nil {
145- clientMutex .Unlock ()
146- return fmt .Errorf ("redis client not initialized and re-initialization failed: %v" , err )
147- }
148- } else {
149- clientMutex .Unlock ()
150- return fmt .Errorf ("redis client not initialized. Call InitRedis() first" )
151- }
152- }
153- client = redisClient
154- clientMutex .Unlock ()
155- }
156-
157- expiration := time .Duration (expirationSeconds ) * time .Second
158- err := client .Set (ctx , key , value , expiration ).Err ()
159- if err != nil {
160- return fmt .Errorf ("could not set value in Redis: %v" , err )
161- }
162- return nil
135+ clientMutex .RLock ()
136+ client := redisClient
137+ clientMutex .RUnlock ()
138+
139+ if client == nil {
140+ clientMutex .Lock ()
141+ if redisClient == nil {
142+ if redisAddr != "" {
143+ err := InitRedis (redisAddr , redisPassword , redisDB )
144+ if err != nil {
145+ clientMutex .Unlock ()
146+ return fmt .Errorf ("redis client not initialized and re-initialization failed: %v" , err )
147+ }
148+ } else {
149+ clientMutex .Unlock ()
150+ return fmt .Errorf ("redis client not initialized. Call InitRedis() first" )
151+ }
152+ }
153+ client = redisClient
154+ clientMutex .Unlock ()
155+ }
156+
157+ expiration := time .Duration (expirationSeconds ) * time .Second
158+ err := client .Set (ctx , key , value , expiration ).Err ()
159+ if err != nil {
160+ return fmt .Errorf ("could not set value in Redis: %v" , err )
161+ }
162+ return nil
163163}
164164
165165func (r * RedisClient ) Get (key string ) (string , error ) {
166- val , err := r .client .Get (ctx , key ).Result ()
167- if err != nil {
168- if err == redis .Nil {
169- return "" , nil
170- }
171- return "" , fmt .Errorf ("could not get value from Redis: %v" , err )
172- }
173- return val , nil
166+ val , err := r .client .Get (ctx , key ).Result ()
167+ if err != nil {
168+ if err == redis .Nil {
169+ return "" , nil
170+ }
171+ return "" , fmt .Errorf ("could not get value from Redis: %v" , err )
172+ }
173+ return val , nil
174174}
0 commit comments