Skip to content

Commit a8327f0

Browse files
authored
Merge pull request #1680 from sky93/main
Add NewFromConnection method for Redis
2 parents 195845e + d43750b commit a8327f0

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

.github/workflows/test-redis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ jobs:
1818
- 1.19.x
1919
- 1.20.x
2020
- 1.21.x
21+
- 1.22.x
22+
- 1.23.x
23+
- 1.24.x
2124
redis:
2225
- '6.x'
2326
- '7.x'

redis/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ A Redis storage driver using [go-redis/redis](https://github.com/go-redis/redis)
2121
### Signatures
2222
```go
2323
func New(config ...Config) Storage
24+
func NewFromConnection(conn redis.UniversalClient) *Storage
2425
func (s *Storage) Get(key string) ([]byte, error)
2526
func (s *Storage) Set(key string, val []byte, exp time.Duration) error
2627
func (s *Storage) Delete(key string) error
@@ -199,6 +200,43 @@ var ConfigDefault = Config{
199200
}
200201
```
201202

203+
### Using an Existing Redis Connection
204+
If you already have a Redis client configured in your application, you can create a Storage instance directly from that client. This is useful when you want to share an existing connection throughout your application instead of creating a new one.
205+
206+
```go
207+
import (
208+
"github.com/gofiber/storage/redis"
209+
redigo "github.com/redis/go-redis/v9"
210+
"fmt"
211+
"context"
212+
)
213+
214+
func main() {
215+
// Create or reuse a Redis universal client (e.g., redis.NewClient, redis.NewClusterClient, etc.)
216+
client := redigo.NewUniversalClient(&redigo.UniversalOptions{
217+
Addrs: []string{"127.0.0.1:6379"},
218+
})
219+
220+
// Create a new Storage instance from the existing Redis client
221+
store := redis.NewFromConnection(client)
222+
223+
// Set a value
224+
if err := store.Set("john", []byte("doe"), 0); err != nil {
225+
panic(err)
226+
}
227+
228+
// Get the value
229+
val, err := store.Get("john")
230+
if err != nil {
231+
panic(err)
232+
}
233+
fmt.Println("Stored value:", string(val))
234+
235+
// Clean up
236+
store.Close()
237+
}
238+
```
239+
202240
### Example: Using DragonflyDB
203241
> **Note:** You can use [DragonflyDB](https://dragonflydb.io/) in the same way as Redis.
204242
> Simply start a DragonflyDB server and configure it just like Redis. Then, call `New()` and use it exactly as you would with Redis.

redis/redis.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ type Storage struct {
1313
db redis.UniversalClient
1414
}
1515

16+
// NewFromConnection creates a new instance of Storage using the provided Redis universal client.
17+
func NewFromConnection(conn redis.UniversalClient) *Storage {
18+
return &Storage{
19+
db: conn,
20+
}
21+
}
22+
1623
// New creates a new Redis storage instance.
1724
func New(config ...Config) *Storage {
1825
// Set default config

redis/redis_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,3 +539,27 @@ func Benchmark_Redis_SetAndDelete(b *testing.B) {
539539

540540
require.NoError(b, err)
541541
}
542+
543+
func Test_Redis_NewFromConnection(t *testing.T) {
544+
t.Parallel()
545+
546+
connection := New(Config{
547+
Reset: true,
548+
})
549+
550+
testStore := NewFromConnection(connection.Conn())
551+
552+
err := testStore.Set("foo", []byte("bar"), 0)
553+
require.NoError(t, err, "failed to set key in Redis storage")
554+
555+
val, err := testStore.Get("foo")
556+
require.NoError(t, err, "failed to get key from Redis storage")
557+
require.Equal(t, []byte("bar"), val, "value mismatch in Redis storage")
558+
559+
err = testStore.Delete("foo")
560+
require.NoError(t, err, "failed to delete key in Redis storage")
561+
562+
val, err = testStore.Get("foo")
563+
564+
require.Nil(t, val, "expected value to be nil after deletion")
565+
}

0 commit comments

Comments
 (0)