Skip to content

Commit f911e17

Browse files
committed
Mandatory context on NewRedisStore
1 parent 449e2aa commit f911e17

File tree

3 files changed

+8
-23
lines changed

3 files changed

+8
-23
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ It uses [`go-redis v8`](https://github.com/go-redis/redis) as client to connect
1010
package main
1111

1212
import (
13+
"context"
1314
"github.com/go-redis/redis/v8"
1415
"github.com/gorilla/sessions"
1516
"github.com/rbcervilla/redisstore/v8"
@@ -25,7 +26,7 @@ func main() {
2526
})
2627

2728
// New default RedisStore
28-
store, err := redisstore.NewRedisStore(client)
29+
store, err := redisstore.NewRedisStore(context.Background(), client)
2930
if err != nil {
3031
log.Fatal("failed to create redis store: ", err)
3132
}

redisstore.go

+1-18
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,7 @@ type RedisStore struct {
3333
type KeyGenFunc func() (string, error)
3434

3535
// NewRedisStore returns a new RedisStore with default configuration
36-
func NewRedisStore(client redis.UniversalClient) (*RedisStore, error) {
37-
38-
rs := &RedisStore{
39-
options: sessions.Options{
40-
Path: "/",
41-
MaxAge: 86400 * 30,
42-
},
43-
client: client,
44-
keyPrefix: "session:",
45-
keyGen: generateRandomKey,
46-
serializer: GobSerializer{},
47-
}
48-
49-
return rs, rs.client.Ping(context.TODO()).Err()
50-
}
51-
52-
// NewRedisStoreWithContext returns a new RedisStore with default configuration
53-
func NewRedisStoreWithContext(ctx context.Context, client redis.UniversalClient) (*RedisStore, error) {
36+
func NewRedisStore(ctx context.Context, client redis.UniversalClient) (*RedisStore, error) {
5437

5538
rs := &RedisStore{
5639
options: sessions.Options{

redisstore_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package redisstore
22

33
import (
4+
"context"
45
"github.com/go-redis/redis/v8"
56
"github.com/gorilla/sessions"
67
"net/http"
@@ -18,7 +19,7 @@ func TestNew(t *testing.T) {
1819
Addr: redisAddr,
1920
})
2021

21-
store, err := NewRedisStore(client)
22+
store, err := NewRedisStore(context.Background(), client)
2223
if err != nil {
2324
t.Fatal("failed to create redis store", err)
2425
}
@@ -43,7 +44,7 @@ func TestOptions(t *testing.T) {
4344
Addr: redisAddr,
4445
})
4546

46-
store, err := NewRedisStore(client)
47+
store, err := NewRedisStore(context.Background(), client)
4748
if err != nil {
4849
t.Fatal("failed to create redis store", err)
4950
}
@@ -71,7 +72,7 @@ func TestSave(t *testing.T) {
7172
Addr: redisAddr,
7273
})
7374

74-
store, err := NewRedisStore(client)
75+
store, err := NewRedisStore(context.Background(), client)
7576
if err != nil {
7677
t.Fatal("failed to create redis store", err)
7778
}
@@ -100,7 +101,7 @@ func TestDelete(t *testing.T) {
100101
Addr: redisAddr,
101102
})
102103

103-
store, err := NewRedisStore(client)
104+
store, err := NewRedisStore(context.Background(), client)
104105
if err != nil {
105106
t.Fatal("failed to create redis store", err)
106107
}

0 commit comments

Comments
 (0)