-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
213 lines (195 loc) · 4.96 KB
/
config.go
File metadata and controls
213 lines (195 loc) · 4.96 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package cachex
import (
"context"
"errors"
"time"
"github.com/redis/go-redis/v9"
)
// Config holds Redis store configuration
type RedisConfig struct {
// Redis connection settings
Addr string
Password string
DB int
Username string
// Connection pool settings
PoolSize int
MinIdleConns int
MaxRetries int
// Timeout settings
DialTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
// Performance settings
EnablePipelining bool
EnableMetrics bool
// Health check settings
HealthCheckInterval time.Duration
HealthCheckTimeout time.Duration
}
// NewRedisConfigWithParams creates a new RedisConfig with all parameters as input
func NewRedisConfig(
addr string,
password string,
db int,
username string,
poolSize int,
minIdleConns int,
maxRetries int,
dialTimeout time.Duration,
readTimeout time.Duration,
writeTimeout time.Duration,
enablePipelining bool,
enableMetrics bool,
healthCheckInterval time.Duration,
healthCheckTimeout time.Duration,
) *RedisConfig {
return &RedisConfig{
Addr: addr,
Password: password,
DB: db,
Username: username,
PoolSize: poolSize,
MinIdleConns: minIdleConns,
MaxRetries: maxRetries,
DialTimeout: dialTimeout,
ReadTimeout: readTimeout,
WriteTimeout: writeTimeout,
EnablePipelining: enablePipelining,
EnableMetrics: enableMetrics,
HealthCheckInterval: healthCheckInterval,
HealthCheckTimeout: healthCheckTimeout,
}
}
// Validate validates the RedisConfig
func (r *RedisConfig) Validate() error {
if r.Addr == "" {
return errors.New("address is required")
}
if r.DB < 0 {
return errors.New("database number cannot be negative")
}
if r.DB > 15 {
return errors.New("database number cannot exceed 15")
}
if r.PoolSize < 0 {
return errors.New("pool size cannot be negative")
}
if r.PoolSize > 1000 {
return errors.New("pool size cannot exceed 1000")
}
if r.MinIdleConns < 0 {
return errors.New("min idle connections cannot be negative")
}
if r.MinIdleConns > r.PoolSize {
return errors.New("min idle connections cannot exceed pool size")
}
if r.MaxRetries < 0 {
return errors.New("max retries cannot be negative")
}
if r.MaxRetries > 10 {
return errors.New("max retries cannot exceed 10")
}
if r.DialTimeout < 0 {
return errors.New("dial timeout cannot be negative")
}
if r.ReadTimeout <= 0 {
return errors.New("read timeout must be positive")
}
if r.WriteTimeout <= 0 {
return errors.New("write timeout must be positive")
}
if r.HealthCheckInterval <= 0 {
return errors.New("health check interval must be positive")
}
if r.HealthCheckTimeout <= 0 {
return errors.New("health check timeout must be positive")
}
return nil
}
// CreateRedisClient creates a Redis client from the RedisConfig
func (r *RedisConfig) CreateRedisClient() *redis.Client {
options := &redis.Options{
Addr: r.Addr,
Password: r.Password,
DB: r.DB,
Username: r.Username,
PoolSize: r.PoolSize,
MinIdleConns: r.MinIdleConns,
MaxRetries: r.MaxRetries,
DialTimeout: r.DialTimeout,
ReadTimeout: r.ReadTimeout,
WriteTimeout: r.WriteTimeout,
}
return redis.NewClient(options)
}
// ConnectRedisClient creates a Redis client and tests the connection
func (r *RedisConfig) ConnectRedisClient(ctx context.Context) (*redis.Client, error) {
// Validate configuration first
if err := r.Validate(); err != nil {
return nil, err
}
// Create client
client := r.CreateRedisClient()
// Test connection
_, err := client.Ping(ctx).Result()
if err != nil {
client.Close()
return nil, err
}
return client, nil
}
// CreateRedisCacheWithParams creates a Redis cache instance with all parameters as input
// This function takes all configuration parameters, creates a Redis client, and returns a cache instance
func CreateRedisCache(
// Redis connection settings
addr string,
password string,
db int,
// Connection pool settings
username string,
poolSize int,
minIdleConns int,
maxRetries int,
// Timeout settings
dialTimeout time.Duration,
readTimeout time.Duration,
writeTimeout time.Duration,
// Performance settings
enablePipelining bool,
enableMetrics bool,
// Health check settings
healthCheckInterval time.Duration,
healthCheckTimeout time.Duration,
// Optional components (can be nil for defaults)
codec Codec,
keyBuilder KeyBuilder,
keyHasher KeyHasher,
) (Cache, error) {
// Create Redis configuration
config := NewRedisConfig(
addr,
password,
db,
username,
poolSize,
minIdleConns,
maxRetries,
dialTimeout,
readTimeout,
writeTimeout,
enablePipelining,
enableMetrics,
healthCheckInterval,
healthCheckTimeout,
)
// Create and test Redis client connection
ctx := context.Background()
client, err := config.ConnectRedisClient(ctx)
if err != nil {
return nil, err
}
// Create cache instance
cache := NewRedisCache(client, codec, keyBuilder, keyHasher)
return cache, nil
}