Skip to content

Commit 0e3517c

Browse files
committed
add cluster support
1 parent e898262 commit 0e3517c

File tree

3 files changed

+98
-10
lines changed

3 files changed

+98
-10
lines changed

README.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
# redis
2-
3-
> A redis-based session store
1+
# Redis store for [Session](https://github.com/go-session/session)
42

53
[![Build][Build-Status-Image]][Build-Status-Url] [![Coverage][Coverage-Image]][Coverage-Url] [![ReportCard][reportcard-image]][reportcard-url] [![GoDoc][godoc-image]][godoc-url] [![License][license-image]][license-url]
64

options.go

+60
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,63 @@ func (o *Options) redisOptions() *redis.Options {
8888
TLSConfig: o.TLSConfig,
8989
}
9090
}
91+
92+
// ClusterOptions are used to configure a cluster client and should be
93+
// passed to NewClusterClient.
94+
type ClusterOptions struct {
95+
// A seed list of host:port addresses of cluster nodes.
96+
Addrs []string
97+
98+
// The maximum number of retries before giving up. Command is retried
99+
// on network errors and MOVED/ASK redirects.
100+
// Default is 8.
101+
MaxRedirects int
102+
103+
// Enables read-only commands on slave nodes.
104+
ReadOnly bool
105+
// Allows routing read-only commands to the closest master or slave node.
106+
RouteByLatency bool
107+
// Allows routing read-only commands to the random master or slave node.
108+
RouteRandomly bool
109+
110+
// Following options are copied from Options struct.
111+
112+
OnConnect func(*redis.Conn) error
113+
114+
MaxRetries int
115+
MinRetryBackoff time.Duration
116+
MaxRetryBackoff time.Duration
117+
Password string
118+
119+
DialTimeout time.Duration
120+
ReadTimeout time.Duration
121+
WriteTimeout time.Duration
122+
123+
// PoolSize applies per cluster node and not for the whole cluster.
124+
PoolSize int
125+
PoolTimeout time.Duration
126+
IdleTimeout time.Duration
127+
IdleCheckFrequency time.Duration
128+
}
129+
130+
func (o *ClusterOptions) redisClusterOptions() *redis.ClusterOptions {
131+
return &redis.ClusterOptions{
132+
Addrs: o.Addrs,
133+
MaxRedirects: o.MaxRedirects,
134+
ReadOnly: o.ReadOnly,
135+
RouteByLatency: o.RouteByLatency,
136+
RouteRandomly: o.RouteRandomly,
137+
OnConnect: o.OnConnect,
138+
MaxRetries: o.MaxRetries,
139+
MinRetryBackoff: o.MinRetryBackoff,
140+
MaxRetryBackoff: o.MaxRetryBackoff,
141+
Password: o.Password,
142+
DialTimeout: o.DialTimeout,
143+
ReadTimeout: o.ReadTimeout,
144+
WriteTimeout: o.WriteTimeout,
145+
PoolSize: o.PoolSize,
146+
PoolTimeout: o.PoolTimeout,
147+
IdleTimeout: o.IdleTimeout,
148+
IdleCheckFrequency: o.IdleCheckFrequency,
149+
}
150+
}

redis.go

+37-7
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ var (
1818
)
1919

2020
// NewRedisStore create an instance of a redis store
21-
func NewRedisStore(opt *Options) session.ManagerStore {
22-
if opt == nil {
23-
panic("option cannot be nil")
21+
func NewRedisStore(opts *Options) session.ManagerStore {
22+
if opts == nil {
23+
panic("options cannot be nil")
2424
}
25-
return &managerStore{cli: redis.NewClient(opt.redisOptions())}
25+
return &managerStore{cli: redis.NewClient(opts.redisOptions())}
2626
}
2727

2828
// NewRedisStoreWithCli create an instance of a redis store
@@ -37,8 +37,38 @@ func NewRedisStoreWithCli(cli *redis.Client) session.ManagerStore {
3737
}
3838
}
3939

40+
// NewRedisClusterStore create an instance of a redis cluster store
41+
func NewRedisClusterStore(opts *ClusterOptions) session.ManagerStore {
42+
if opts == nil {
43+
panic("options cannot be nil")
44+
}
45+
return &managerStore{cli: redis.NewClusterClient(opts.redisClusterOptions())}
46+
}
47+
48+
// NewRedisClusterStoreWithCli create an instance of a redis cluster store
49+
func NewRedisClusterStoreWithCli(cli *redis.ClusterClient) session.ManagerStore {
50+
return &managerStore{
51+
cli: cli,
52+
pool: sync.Pool{
53+
New: func() interface{} {
54+
return newDefaultStore(cli)
55+
},
56+
},
57+
}
58+
}
59+
60+
type clienter interface {
61+
Get(key string) *redis.StringCmd
62+
Set(key string, value interface{}, expiration time.Duration) *redis.StatusCmd
63+
Expire(key string, expiration time.Duration) *redis.BoolCmd
64+
Exists(keys ...string) *redis.IntCmd
65+
TxPipeline() redis.Pipeliner
66+
Del(keys ...string) *redis.IntCmd
67+
Close() error
68+
}
69+
4070
type managerStore struct {
41-
cli *redis.Client
71+
cli clienter
4272
pool sync.Pool
4373
}
4474

@@ -147,7 +177,7 @@ func (s *managerStore) Close() error {
147177
return s.cli.Close()
148178
}
149179

150-
func newDefaultStore(cli *redis.Client) *store {
180+
func newDefaultStore(cli clienter) *store {
151181
return &store{
152182
cli: cli,
153183
}
@@ -159,7 +189,7 @@ type store struct {
159189
sid string
160190
expired int64
161191
values map[string]interface{}
162-
cli *redis.Client
192+
cli clienter
163193
}
164194

165195
func (s *store) reset(ctx context.Context, sid string, expired int64, values map[string]interface{}) {

0 commit comments

Comments
 (0)