Skip to content

Commit 1079bbb

Browse files
committed
feat: unify store initialization with flexible option-based API
- Replace legacy initialization functions with a unified NewStore() using the Option Pattern - Add extensive configuration options for connection, authentication, Redis, and session settings - Implement strict validation for configuration and connection options at initialization - Remove code duplication and improve separation of concerns in store setup - Update documentation and usage examples for the new API - Add a comprehensive migration guide for upgrading from v1 to v2 - Update module path to github.com/boj/redistore/v2 for semantic versioning - Improve error messages and fail-fast behavior for invalid configurations - Add a new test suite with thorough coverage for option validation and error handling - Deprecate v1 API and document migration steps - Enhance developer experience with clearer, more flexible, and self-documenting options Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
1 parent 5c47f51 commit 1079bbb

7 files changed

Lines changed: 1781 additions & 199 deletions

File tree

CHANGELOG.md

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
8+
## [2.0.0] - 2026-01-13
9+
10+
### Breaking Changes
11+
12+
This is a major version update with breaking API changes. Please see [MIGRATION.md](MIGRATION.md) for detailed migration instructions.
13+
14+
#### Removed
15+
16+
- **`NewRediStore(size, network, address, username, password, keyPairs)`** - Replaced by `NewStore()` with `WithAddress()` option
17+
- **`NewRediStoreWithDB(size, network, address, username, password, db, keyPairs)`** - Replaced by `NewStore()` with `WithAddress()` and `WithDB()` options
18+
- **`NewRediStoreWithPool(pool, keyPairs)`** - Replaced by `NewStore()` with `WithPool()` option
19+
- **`NewRediStoreWithURL(size, url, keyPairs)`** - Replaced by `NewStore()` with `WithURL()` option
20+
21+
### Added
22+
23+
#### New API
24+
25+
- **`NewStore(keyPairs, opts...)`** - New unified initialization function using Option Pattern
26+
27+
```go
28+
store, err := NewStore(
29+
[]byte("secret-key"),
30+
WithAddress("tcp", ":6379"),
31+
WithDB("1"),
32+
WithMaxLength(8192),
33+
)
34+
```
35+
36+
#### Connection Options
37+
38+
- **`WithPool(pool)`** - Use custom Redis connection pool
39+
- **`WithAddress(network, address)`** - Connect using network protocol and address
40+
- **`WithURL(url)`** - Connect using Redis URL
41+
42+
#### Authentication Options
43+
44+
- **`WithAuth(username, password)`** - Set username and password for Redis authentication
45+
- **`WithPassword(password)`** - Set password only (convenience function)
46+
47+
#### Redis Configuration Options
48+
49+
- **`WithDB(db)`** - Set Redis database index as string ("0"-"15")
50+
- **`WithDBNum(dbNum)`** - Set Redis database index as integer (0-15)
51+
- **`WithPoolSize(size)`** - Set connection pool size (default: 10)
52+
- **`WithIdleTimeout(timeout)`** - Set connection idle timeout (default: 240s)
53+
54+
#### Store Configuration Options
55+
56+
- **`WithMaxLength(length)`** - Set maximum session data size (default: 4096 bytes)
57+
- **`WithKeyPrefix(prefix)`** - Set Redis key prefix (default: "session\_")
58+
- **`WithDefaultMaxAge(age)`** - Set default TTL in seconds (default: 1200)
59+
- **`WithSerializer(serializer)`** - Set session serializer (default: GobSerializer)
60+
- **`WithSessionOptions(opts)`** - Set full session options
61+
- **`WithPath(path)`** - Set cookie path (default: "/")
62+
- **`WithMaxAge(age)`** - Set cookie MaxAge (default: 30 days)
63+
64+
#### Testing
65+
66+
- **`redistore_options_test.go`** - Comprehensive test suite for option validation
67+
- Added 23+ new tests covering:
68+
- Configuration validation
69+
- Error handling
70+
- Option combinations
71+
- Default values
72+
73+
### Changed
74+
75+
- Module path updated to `github.com/boj/redistore/v2` for v2 versioning
76+
- Improved error messages with detailed context
77+
- Configuration validation moved to initialization time (fail fast)
78+
79+
### Improved
80+
81+
- **Code Quality**
82+
83+
- Eliminated code duplication across initialization functions
84+
- Better separation of concerns with internal configuration structures
85+
- More maintainable and extensible codebase
86+
87+
- **API Design**
88+
89+
- Self-documenting option names
90+
- Compile-time validation of configuration
91+
- Flexible option combinations
92+
- Better default values
93+
94+
- **Developer Experience**
95+
- Single entry point reduces learning curve
96+
- Options can be applied in any order
97+
- Clear error messages for invalid configurations
98+
- Comprehensive documentation
99+
100+
### Fixed
101+
102+
- Connection validation now happens during initialization (not on first use)
103+
- Better handling of nil values in configuration
104+
- Improved database number validation (0-15 range)
105+
106+
### Documentation
107+
108+
- **Added `MIGRATION.md`** - Comprehensive migration guide from v1 to v2
109+
- **Added `CHANGELOG.md`** - Version history and change tracking
110+
- Updated all code examples to use new API
111+
- Improved inline documentation with examples
112+
113+
### Technical Details
114+
115+
#### Internal Changes
116+
117+
- Added `Option` function type for configuration
118+
- Added `storeConfig` struct for internal configuration management
119+
- Added `addressConfig` struct for network address configuration
120+
- Implemented `defaultConfig()` for sensible defaults
121+
- Implemented `validate()` for configuration validation
122+
- Implemented `buildPool()` for pool creation from configuration
123+
124+
#### Compatibility
125+
126+
- **Go Version**: Requires Go 1.23+
127+
- **Dependencies**: No changes to external dependencies
128+
- `github.com/gomodule/redigo v1.9.3`
129+
- `github.com/gorilla/securecookie v1.1.2`
130+
- `github.com/gorilla/sessions v1.4.0`
131+
132+
## [1.0.0] - Previous Releases
133+
134+
For changes prior to v2.0.0, please refer to the git history.
135+
136+
### v1 API (Deprecated)
137+
138+
The v1 API included four initialization functions:
139+
140+
- `NewRediStore()` - Basic initialization
141+
- `NewRediStoreWithDB()` - With database selection
142+
- `NewRediStoreWithPool()` - With custom pool
143+
- `NewRediStoreWithURL()` - With Redis URL
144+
145+
These have been replaced by the unified `NewStore()` function with options in v2.
146+
147+
---
148+
149+
## Migration Guide
150+
151+
See [MIGRATION.md](MIGRATION.md) for step-by-step instructions on upgrading from v1 to v2.
152+
153+
## Support
154+
155+
- **Issues**: https://github.com/boj/redistore/issues
156+
- **Discussions**: https://github.com/boj/redistore/discussions
157+
- **Documentation**: https://pkg.go.dev/github.com/boj/redistore/v2

0 commit comments

Comments
 (0)