You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
1.3 KiB
Go
67 lines
1.3 KiB
Go
4 years ago
|
package redis
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
|
||
|
"zero/core/logx"
|
||
|
|
||
|
red "github.com/go-redis/redis"
|
||
|
)
|
||
|
|
||
|
type ClosableNode interface {
|
||
|
RedisNode
|
||
|
Close()
|
||
|
}
|
||
|
|
||
|
func CreateBlockingNode(r *Redis) (ClosableNode, error) {
|
||
|
timeout := readWriteTimeout + blockingQueryTimeout
|
||
|
|
||
|
switch r.Type {
|
||
|
case NodeType:
|
||
|
client := red.NewClient(&red.Options{
|
||
|
Addr: r.Addr,
|
||
|
Password: r.Pass,
|
||
|
DB: defaultDatabase,
|
||
|
MaxRetries: maxRetries,
|
||
|
PoolSize: 1,
|
||
|
MinIdleConns: 1,
|
||
|
ReadTimeout: timeout,
|
||
|
})
|
||
|
return &clientBridge{client}, nil
|
||
|
case ClusterType:
|
||
|
client := red.NewClusterClient(&red.ClusterOptions{
|
||
|
Addrs: []string{r.Addr},
|
||
|
Password: r.Pass,
|
||
|
MaxRetries: maxRetries,
|
||
|
PoolSize: 1,
|
||
|
MinIdleConns: 1,
|
||
|
ReadTimeout: timeout,
|
||
|
})
|
||
|
return &clusterBridge{client}, nil
|
||
|
default:
|
||
|
return nil, fmt.Errorf("unknown redis type: %s", r.Type)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
type (
|
||
|
clientBridge struct {
|
||
|
*red.Client
|
||
|
}
|
||
|
|
||
|
clusterBridge struct {
|
||
|
*red.ClusterClient
|
||
|
}
|
||
|
)
|
||
|
|
||
|
func (bridge *clientBridge) Close() {
|
||
|
if err := bridge.Client.Close(); err != nil {
|
||
|
logx.Errorf("Error occurred on close redis client: %s", err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (bridge *clusterBridge) Close() {
|
||
|
if err := bridge.ClusterClient.Close(); err != nil {
|
||
|
logx.Errorf("Error occurred on close redis cluster: %s", err)
|
||
|
}
|
||
|
}
|