Skip to content

Commit 53b374f

Browse files
liu-xuewenliuxuewen
andauthored
feat: add redis debug mode (#59)
Co-authored-by: liuxuewen <[email protected]>
1 parent fef5615 commit 53b374f

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.DS_Store
22

3+
.idea

redistore.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import (
1111
"encoding/json"
1212
"errors"
1313
"fmt"
14+
"log"
1415
"net/http"
16+
"os"
1517
"strings"
1618
"time"
1719

@@ -251,6 +253,26 @@ func NewRediStore(size int, network, address, password string, keyPairs ...[]byt
251253
}, keyPairs...)
252254
}
253255

256+
// NewRediStoreWithDebug returns a new RediStore with debug.
257+
// size: maximum number of idle connections.
258+
func NewRediStoreWithDebug(size int, network, address, password string, keyPairs ...[]byte) (*RediStore, error) {
259+
return NewRediStoreWithPool(&redis.Pool{
260+
MaxIdle: size,
261+
IdleTimeout: 240 * time.Second,
262+
TestOnBorrow: func(c redis.Conn, t time.Time) error {
263+
_, err := c.Do("PING")
264+
return err
265+
},
266+
Dial: func() (redis.Conn, error) {
267+
c, err := dial(network, address, password)
268+
if err != nil {
269+
return nil, err
270+
}
271+
return redis.NewLoggingConn(c, log.New(os.Stdout, "", log.LstdFlags), "redis"), nil
272+
},
273+
}, keyPairs...)
274+
}
275+
254276
func dialWithDB(network, address, password, DB string) (redis.Conn, error) {
255277
c, err := dial(network, address, password)
256278
if err != nil {

0 commit comments

Comments
 (0)