8000 Add NewFromConnection method for Redis by sky93 · Pull Request #1680 · gofiber/storage · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add NewFromConnection method for Redis #1680

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/test-redis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ jobs:
- 1.19.x
- 1.20.x
- 1.21.x
- 1.22.x
- 1.23.x
- 1.24.x
redis:
- '6.x'
- '7.x'
Expand Down
38 changes: 38 additions & 0 deletions redis/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ A Redis storage driver using [go-redis/redis](https://github.com/go-redis/redis)
### Signatures
```go
func New(config ...Config) Storage
func NewFromConnection(conn redis.UniversalClient) *Storage
func (s *Storage) Get(key string) ([]byte, error)
func (s *Storage) Set(key string, val []byte, exp time.Duration) error
func (s *Storage) Delete(key string) error
Expand Down Expand Up @@ -199,6 +200,43 @@ var ConfigDefault = Config{
}
```

### Using an Existing Redis Connection
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.

```go
import (
"github.com/gofiber/storage/redis"
redigo "github.com/redis/go-redis/v9"
"fmt"
"context"
)

func main() {
// Create or reuse a Redis universal client (e.g., redis.NewClient, redis.NewClusterClient, etc.)
client := redigo.NewUniversalClient(&redigo.UniversalOptions{
Addrs: []string{"127.0.0.1:6379"},
})

// Create a new Storage instance from the existing Redis client
store := redis.NewFromConnection(client)

// Set a value
if err := store.Set("john", []byte("doe"), 0); err != nil {
panic(err)
}

// Get the value
val, err := store.Get("john")
if err != nil {
panic(err)
}
fmt.Println("Stored value:", string(val))

// Clean up
store.Close()
}
```

### Example: Using DragonflyDB
> **Note:** You can use [DragonflyDB](https://dragonflydb.io/) in the same way as Redis.
> Simply start a DragonflyDB server and configure it just like Redis. Then, call `New()` and use it exactly as you would with Redis.
7 changes: 7 additions & 0 deletions redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ type Storage struct {
db redis.UniversalClient
}

// NewFromConnection creates a new instance of Storage using the provided Redis universal client.
func NewFromConnection(conn redis.UniversalClient) *Storage {
return &Storage{
db: conn,
}
}

// New creates a new Redis storage instance.
func New(config ...Config) *Storage {
// Set default config
Expand Down
24 changes: 24 additions & 0 deletions redis/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,3 +539,27 @@ func Benchmark_Redis_SetAndDelete(b *testing.B) {

require.NoError(b, err)
}

func Test_Redis_NewFromConnection(t *testing.T) {
t.Parallel()

connection := New(Config{
Reset: true,
})

testStore := NewFromConnection(connection.Conn())

err := testStore.Set("foo", []byte("bar"), 0)
require.NoError(t, err, "failed to set key in Redis storage")

val, err := testStore.Get("foo")
require.NoError(t, err, "failed to get key from Redis storage")
require.Equal(t, []byte("bar"), val, "value mismatch in Redis storage")

err = testStore.Delete("foo")
require.NoError(t, err, "failed to delete key in Redis storage")

val, err = testStore.Get("foo")

require.Nil(t, val, "expected value to be nil after deletion")
}
0