Skip to content

Commit 228d0e2

Browse files
committed
refactor: refactor Redis store configuration to use functional options pattern
- Replace all Redis store convenience methods with a functional options pattern for configuration - Remove legacy methods for enabling and configuring the Redis store - Add new methods and types for Redis configuration via options (e.g., WithRedisAddr, WithRedisAuth, WithRedisCache, WithRedisPool, WithRedisKeyPrefix) - Update documentation and examples to use the functional options API for Redis setup - Refactor initialization logic to use a unified EnableRedisStore method and helper for fallback - Simplify token response generation by making generateTokenResponse return just the response - Update all tests and test helpers to cover the new options-based Redis configuration API - Remove explicit Redis config struct manipulation in examples and tests; use options instead - Translate all related README sections in English, Simplified Chinese, and Traditional Chinese - Improve clarity and maintainability of Redis middleware integration Signed-off-by: appleboy <[email protected]>
1 parent daa117d commit 228d0e2

File tree

9 files changed

+466
-407
lines changed

9 files changed

+466
-407
lines changed

README.md

Lines changed: 40 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,8 @@ Easily add login, token refresh, and authorization to your Gin applications.
3030
- [Redis Store Configuration](#redis-store-configuration)
3131
- [Redis Features](#redis-features)
3232
- [Redis Usage Methods](#redis-usage-methods)
33-
- [Method 1: Enable Redis with Default Configuration](#method-1-enable-redis-with-default-configuration)
34-
- [Method 2: Enable Redis with Custom Address](#method-2-enable-redis-with-custom-address)
35-
- [Method 3: Enable Redis with Full Options](#method-3-enable-redis-with-full-options)
36-
- [Method 4: Enable Redis with Custom Configuration](#method-4-enable-redis-with-custom-configuration)
37-
- [Method 5: Configure Client-side Cache](#method-5-configure-client-side-cache)
38-
- [Method 6: Method Chaining](#method-6-method-chaining)
33+
- [Using Functional Options Pattern (Recommended)](#using-functional-options-pattern-recommended)
34+
- [Available Options](#available-options)
3935
- [Configuration Options](#configuration-options)
4036
- [RedisConfig](#redisconfig)
4137
- [Fallback Behavior](#fallback-behavior)
@@ -381,65 +377,51 @@ This library supports Redis as a backend for refresh token storage, with built-i
381377

382378
### Redis Usage Methods
383379

384-
#### Method 1: Enable Redis with Default Configuration
380+
#### Using Functional Options Pattern (Recommended)
381+
382+
The Redis configuration now uses a functional options pattern for cleaner and more flexible configuration:
385383

386384
```go
385+
// Method 1: Enable Redis with default configuration
387386
middleware := &jwt.GinJWTMiddleware{
388387
// ... other configuration
389-
}
390-
391-
// Enable Redis with default settings (localhost:6379)
392-
middleware.EnableRedisStore()
393-
```
394-
395-
#### Method 2: Enable Redis with Custom Address
396-
397-
```go
398-
// Enable Redis with custom address
399-
middleware.EnableRedisStoreWithAddr("redis.example.com:6379")
400-
```
401-
402-
#### Method 3: Enable Redis with Full Options
403-
404-
```go
405-
// Enable Redis with address, password, and database
406-
middleware.EnableRedisStoreWithOptions("redis.example.com:6379", "password", 0)
407-
```
408-
409-
#### Method 4: Enable Redis with Custom Configuration
410-
411-
```go
412-
import "github.com/appleboy/gin-jwt/v2/store"
413-
414-
config := &store.RedisConfig{
415-
Addr: "redis.example.com:6379",
416-
Password: "your-password",
417-
DB: 0,
418-
CacheSize: 256 * 1024 * 1024, // 256MB cache
419-
CacheTTL: 5 * time.Minute, // 5 minute cache TTL
420-
KeyPrefix: "myapp-jwt:",
421-
}
388+
}.EnableRedisStore()
422389

423-
middleware.EnableRedisStoreWithConfig(config)
424-
```
425-
426-
#### Method 5: Configure Client-side Cache
427-
428-
```go
429-
// Set client-side cache size and TTL
430-
middleware.SetRedisClientSideCache(64*1024*1024, 30*time.Second) // 64MB cache, 30s TTL
431-
```
390+
// Method 2: Enable Redis with custom address
391+
middleware := &jwt.GinJWTMiddleware{
392+
// ... other configuration
393+
}.EnableRedisStore(
394+
jwt.WithRedisAddr("redis.example.com:6379"),
395+
)
432396

433-
#### Method 6: Method Chaining
397+
// Method 3: Enable Redis with authentication
398+
middleware := &jwt.GinJWTMiddleware{
399+
// ... other configuration
400+
}.EnableRedisStore(
401+
jwt.WithRedisAddr("redis.example.com:6379"),
402+
jwt.WithRedisAuth("password", 0),
403+
)
434404

435-
```go
405+
// Method 4: Full configuration with all options
436406
middleware := &jwt.GinJWTMiddleware{
437407
// ... other configuration
438-
}.
439-
EnableRedisStoreWithAddr("redis.example.com:6379").
440-
SetRedisClientSideCache(128*1024*1024, time.Minute)
408+
}.EnableRedisStore(
409+
jwt.WithRedisAddr("redis.example.com:6379"),
410+
jwt.WithRedisAuth("password", 1),
411+
jwt.WithRedisCache(128*1024*1024, time.Minute), // 128MB cache, 1min TTL
412+
jwt.WithRedisPool(20, time.Hour, 2*time.Hour), // Pool config
413+
jwt.WithRedisKeyPrefix("myapp:jwt:"), // Key prefix
414+
)
441415
```
442416

417+
#### Available Options
418+
419+
- `WithRedisAddr(addr string)` - Sets Redis server address
420+
- `WithRedisAuth(password string, db int)` - Sets authentication and database
421+
- `WithRedisCache(size int, ttl time.Duration)` - Configures client-side cache
422+
- `WithRedisPool(poolSize int, maxIdleTime, maxLifetime time.Duration)` - Configures connection pool
423+
- `WithRedisKeyPrefix(prefix string)` - Sets key prefix for Redis keys
424+
443425
### Configuration Options
444426

445427
#### RedisConfig
@@ -514,8 +496,10 @@ func main() {
514496

515497
return nil, jwt.ErrFailedAuthentication
516498
},
517-
}).EnableRedisStoreWithAddr("localhost:6379"). // Enable Redis
518-
SetRedisClientSideCache(64*1024*1024, 30*time.Second) // Configure cache
499+
}).EnableRedisStore( // Enable Redis with options
500+
jwt.WithRedisAddr("localhost:6379"), // Redis server address
501+
jwt.WithRedisCache(64*1024*1024, 30*time.Second), // 64MB cache, 30s TTL
502+
)
519503

520504
if err != nil {
521505
log.Fatal("JWT Error:" + err.Error())

README.zh-CN.md

Lines changed: 40 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,8 @@
3030
- [Redis 存储配置](#redis-存储配置)
3131
- [Redis 功能特色](#redis-功能特色)
3232
- [Redis 使用方法](#redis-使用方法)
33-
- [方法 1:启用默认 Redis 配置](#方法-1启用默认-redis-配置)
34-
- [方法 2:启用自定义地址的 Redis](#方法-2启用自定义地址的-redis)
35-
- [方法 3:使用完整选项启用 Redis](#方法-3使用完整选项启用-redis)
36-
- [方法 4:使用自定义配置启用 Redis](#方法-4使用自定义配置启用-redis)
37-
- [方法 5:配置客户端缓存](#方法-5配置客户端缓存)
38-
- [方法 6:方法链](#方法-6方法链)
33+
- [使用函数选项模式(推荐)](#使用函数选项模式推荐)
34+
- [可用选项](#可用选项)
3935
- [配置选项](#配置选项)
4036
- [RedisConfig](#redisconfig)
4137
- [回退行为](#回退行为)
@@ -216,65 +212,51 @@ fmt.Printf("New Refresh Token: %s\n", newTokenPair.RefreshToken)
216212

217213
### Redis 使用方法
218214

219-
#### 方法 1:启用默认 Redis 配置
215+
#### 使用函数选项模式(推荐)
216+
217+
Redis 配置现在使用函数选项模式,提供更清洁且灵活的配置:
220218

221219
```go
220+
// 方法 1:使用默认配置启用 Redis
222221
middleware := &jwt.GinJWTMiddleware{
223222
// ... 其他配置
224-
}
225-
226-
// 使用默认设置启用 Redis(localhost:6379)
227-
middleware.EnableRedisStore()
228-
```
229-
230-
#### 方法 2:启用自定义地址的 Redis
231-
232-
```go
233-
// 使用自定义地址启用 Redis
234-
middleware.EnableRedisStoreWithAddr("redis.example.com:6379")
235-
```
236-
237-
#### 方法 3:使用完整选项启用 Redis
238-
239-
```go
240-
// 使用地址、密码和数据库启用 Redis
241-
middleware.EnableRedisStoreWithOptions("redis.example.com:6379", "password", 0)
242-
```
243-
244-
#### 方法 4:使用自定义配置启用 Redis
245-
246-
```go
247-
import "github.com/appleboy/gin-jwt/v2/store"
248-
249-
config := &store.RedisConfig{
250-
Addr: "redis.example.com:6379",
251-
Password: "your-password",
252-
DB: 0,
253-
CacheSize: 256 * 1024 * 1024, // 256MB 缓存
254-
CacheTTL: 5 * time.Minute, // 5 分钟缓存 TTL
255-
KeyPrefix: "myapp-jwt:",
256-
}
223+
}.EnableRedisStore()
257224

258-
middleware.EnableRedisStoreWithConfig(config)
259-
```
260-
261-
#### 方法 5:配置客户端缓存
262-
263-
```go
264-
// 设置客户端缓存大小和 TTL
265-
middleware.SetRedisClientSideCache(64*1024*1024, 30*time.Second) // 64MB 缓存,30秒 TTL
266-
```
225+
// 方法 2:使用自定义地址启用 Redis
226+
middleware := &jwt.GinJWTMiddleware{
227+
// ... 其他配置
228+
}.EnableRedisStore(
229+
jwt.WithRedisAddr("redis.example.com:6379"),
230+
)
267231

268-
#### 方法 6:方法链
232+
// 方法 3:使用认证启用 Redis
233+
middleware := &jwt.GinJWTMiddleware{
234+
// ... 其他配置
235+
}.EnableRedisStore(
236+
jwt.WithRedisAddr("redis.example.com:6379"),
237+
jwt.WithRedisAuth("password", 0),
238+
)
269239

270-
```go
240+
// 方法 4:使用所有选项的完整配置
271241
middleware := &jwt.GinJWTMiddleware{
272242
// ... 其他配置
273-
}.
274-
EnableRedisStoreWithAddr("redis.example.com:6379").
275-
SetRedisClientSideCache(128*1024*1024, time.Minute)
243+
}.EnableRedisStore(
244+
jwt.WithRedisAddr("redis.example.com:6379"),
245+
jwt.WithRedisAuth("password", 1),
246+
jwt.WithRedisCache(128*1024*1024, time.Minute), // 128MB 缓存,1分钟 TTL
247+
jwt.WithRedisPool(20, time.Hour, 2*time.Hour), // 连接池配置
248+
jwt.WithRedisKeyPrefix("myapp:jwt:"), // 键前缀
249+
)
276250
```
277251

252+
#### 可用选项
253+
254+
- `WithRedisAddr(addr string)` - 设置 Redis 服务器地址
255+
- `WithRedisAuth(password string, db int)` - 设置认证和数据库
256+
- `WithRedisCache(size int, ttl time.Duration)` - 配置客户端缓存
257+
- `WithRedisPool(poolSize int, maxIdleTime, maxLifetime time.Duration)` - 配置连接池
258+
- `WithRedisKeyPrefix(prefix string)` - 设置 Redis 键的前缀
259+
278260
### 配置选项
279261

280262
#### RedisConfig
@@ -349,8 +331,10 @@ func main() {
349331

350332
return nil, jwt.ErrFailedAuthentication
351333
},
352-
}).EnableRedisStoreWithAddr("localhost:6379"). // 启用 Redis
353-
SetRedisClientSideCache(64*1024*1024, 30*time.Second) // 配置缓存
334+
}).EnableRedisStore( // 使用选项启用 Redis
335+
jwt.WithRedisAddr("localhost:6379"), // Redis 服务器地址
336+
jwt.WithRedisCache(64*1024*1024, 30*time.Second), // 64MB 缓存,30秒 TTL
337+
)
354338

355339
if err != nil {
356340
log.Fatal("JWT Error:" + err.Error())

README.zh-TW.md

Lines changed: 40 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,8 @@
3030
- [Redis 儲存配置](#redis-儲存配置)
3131
- [Redis 功能特色](#redis-功能特色)
3232
- [Redis 使用方法](#redis-使用方法)
33-
- [方法 1:啟用預設 Redis 配置](#方法-1啟用預設-redis-配置)
34-
- [方法 2:啟用自訂位址的 Redis](#方法-2啟用自訂位址的-redis)
35-
- [方法 3:使用完整選項啟用 Redis](#方法-3使用完整選項啟用-redis)
36-
- [方法 4:使用自訂配置啟用 Redis](#方法-4使用自訂配置啟用-redis)
37-
- [方法 5:配置用戶端快取](#方法-5配置用戶端快取)
38-
- [方法 6:方法鏈](#方法-6方法鏈)
33+
- [使用函數選項模式(推薦)](#使用函數選項模式推薦)
34+
- [可用選項](#可用選項)
3935
- [配置選項](#配置選項)
4036
- [RedisConfig](#redisconfig)
4137
- [回退行為](#回退行為)
@@ -216,65 +212,51 @@ fmt.Printf("New Refresh Token: %s\n", newTokenPair.RefreshToken)
216212

217213
### Redis 使用方法
218214

219-
#### 方法 1:啟用預設 Redis 配置
215+
#### 使用函數選項模式(推薦)
216+
217+
Redis 配置現在使用函數選項模式,提供更清潔且靈活的配置:
220218

221219
```go
220+
// 方法 1:使用預設配置啟用 Redis
222221
middleware := &jwt.GinJWTMiddleware{
223222
// ... 其他配置
224-
}
225-
226-
// 使用預設設定啟用 Redis(localhost:6379)
227-
middleware.EnableRedisStore()
228-
```
229-
230-
#### 方法 2:啟用自訂位址的 Redis
231-
232-
```go
233-
// 使用自訂位址啟用 Redis
234-
middleware.EnableRedisStoreWithAddr("redis.example.com:6379")
235-
```
236-
237-
#### 方法 3:使用完整選項啟用 Redis
238-
239-
```go
240-
// 使用位址、密碼和資料庫啟用 Redis
241-
middleware.EnableRedisStoreWithOptions("redis.example.com:6379", "password", 0)
242-
```
243-
244-
#### 方法 4:使用自訂配置啟用 Redis
245-
246-
```go
247-
import "github.com/appleboy/gin-jwt/v2/store"
248-
249-
config := &store.RedisConfig{
250-
Addr: "redis.example.com:6379",
251-
Password: "your-password",
252-
DB: 0,
253-
CacheSize: 256 * 1024 * 1024, // 256MB 快取
254-
CacheTTL: 5 * time.Minute, // 5 分鐘快取 TTL
255-
KeyPrefix: "myapp-jwt:",
256-
}
223+
}.EnableRedisStore()
257224

258-
middleware.EnableRedisStoreWithConfig(config)
259-
```
260-
261-
#### 方法 5:配置用戶端快取
262-
263-
```go
264-
// 設定用戶端快取大小和 TTL
265-
middleware.SetRedisClientSideCache(64*1024*1024, 30*time.Second) // 64MB 快取,30秒 TTL
266-
```
225+
// 方法 2:使用自訂位址啟用 Redis
226+
middleware := &jwt.GinJWTMiddleware{
227+
// ... 其他配置
228+
}.EnableRedisStore(
229+
jwt.WithRedisAddr("redis.example.com:6379"),
230+
)
267231

268-
#### 方法 6:方法鏈
232+
// 方法 3:使用認證啟用 Redis
233+
middleware := &jwt.GinJWTMiddleware{
234+
// ... 其他配置
235+
}.EnableRedisStore(
236+
jwt.WithRedisAddr("redis.example.com:6379"),
237+
jwt.WithRedisAuth("password", 0),
238+
)
269239

270-
```go
240+
// 方法 4:使用所有選項的完整配置
271241
middleware := &jwt.GinJWTMiddleware{
272242
// ... 其他配置
273-
}.
274-
EnableRedisStoreWithAddr("redis.example.com:6379").
275-
SetRedisClientSideCache(128*1024*1024, time.Minute)
243+
}.EnableRedisStore(
244+
jwt.WithRedisAddr("redis.example.com:6379"),
245+
jwt.WithRedisAuth("password", 1),
246+
jwt.WithRedisCache(128*1024*1024, time.Minute), // 128MB 快取,1分鐘 TTL
247+
jwt.WithRedisPool(20, time.Hour, 2*time.Hour), // 連線池配置
248+
jwt.WithRedisKeyPrefix("myapp:jwt:"), // 鍵前綴
249+
)
276250
```
277251

252+
#### 可用選項
253+
254+
- `WithRedisAddr(addr string)` - 設定 Redis 伺服器位址
255+
- `WithRedisAuth(password string, db int)` - 設定認證和資料庫
256+
- `WithRedisCache(size int, ttl time.Duration)` - 配置用戶端快取
257+
- `WithRedisPool(poolSize int, maxIdleTime, maxLifetime time.Duration)` - 配置連線池
258+
- `WithRedisKeyPrefix(prefix string)` - 設定 Redis 鍵的前綴
259+
278260
### 配置選項
279261

280262
#### RedisConfig
@@ -349,8 +331,10 @@ func main() {
349331

350332
return nil, jwt.ErrFailedAuthentication
351333
},
352-
}).EnableRedisStoreWithAddr("localhost:6379"). // 啟用 Redis
353-
SetRedisClientSideCache(64*1024*1024, 30*time.Second) // 配置快取
334+
}).EnableRedisStore( // 使用選項啟用 Redis
335+
jwt.WithRedisAddr("localhost:6379"), // Redis 伺服器位址
336+
jwt.WithRedisCache(64*1024*1024, 30*time.Second), // 64MB 快取,30秒 TTL
337+
)
354338

355339
if err != nil {
356340
log.Fatal("JWT Error:" + err.Error())

0 commit comments

Comments
 (0)