@@ -12,6 +12,16 @@ local redis_connector = require "resty.redis.connector"
1212local rc = redis_connector .new ()
1313
1414local redis ,
err = rc :
connect {
url = " redis://[email protected] :6379/2" }
15+
16+ -- or...
17+
18+ local redis , err = rc :connect {
19+ host = " 127.0.0.1" ,
20+ port = 6379 ,
21+ db = 2 ,
22+ password = " PASSWORD" ,
23+ }
24+
1525if not redis then
1626 ngx .log (ngx .ERR , err )
1727end
@@ -34,9 +44,9 @@ When connecting via Redis Sentinel, the format is as follows:
3444
3545Again, ` PASSWORD ` and ` DB ` are optional. ` ROLE ` must be any of ` m ` , ` s ` or ` a ` , meaning:
3646
37- ` m ` : master
38- ` s ` : slave
39- ` a ` : any (first tries the master, but will failover to a slave if required)
47+ * ` m ` : master
48+ * ` s ` : slave
49+ * ` a ` : any (first tries the master, but will failover to a slave if required)
4050
4151
4252## Parameters
@@ -55,15 +65,15 @@ The defaults are as follows:
5565 password = " " ,
5666 db = 0 ,
5767 master_name = " mymaster" ,
58- role = " master" ,
68+ role = " master" , -- master | slave | any
5969 sentinels = nil ,
6070}
6171```
6272
6373Note that if ` sentinel:// ` is supplied as the ` url ` parameter, a table of ` sentinels ` must also
6474be supplied. e.g.
6575
66- ``lua
76+ ``` lua
6777local redis , err = rc :connect {
6878 url = " sentinel://mymaster:a/2" ,
6979 sentinels = {
@@ -80,6 +90,13 @@ local redis, err = rc:connect{
8090* [ set_read_timeout] ( #set_read_timeout )
8191* [ set_connection_options] ( #set_connection_options )
8292* [ connect] ( #connect )
93+ * [ Utilities] ( #utilities )
94+ * [ connect_via_sentinel] ( #connect_via_sentinel )
95+ * [ try_hosts] ( #try_hosts )
96+ * [ connect_to_host] ( #connect_to_host )
97+ * [ Sentinel Utilities] ( #sentinel-utilities )
98+ * [ sentinel.get_master] ( #sentinelget_master )
99+ * [ sentinel.get_slaves] ( #sentinelget_slaves )
83100
84101
85102### new
@@ -119,6 +136,46 @@ method.
119136Attempts to create a connection, according to the [ params] ( #parameters ) supplied.
120137
121138
139+ ## Utilities
140+
141+ ### connect_via_sentinel
142+
143+ ` syntax: redis, err = rc:connect_via_sentinel(sentinels, master_name, role) `
144+
145+ Returns a Redis connection by first accessing a sentinel as supplied by the ` sentinels ` table,
146+ and querying this with the ` master_name ` and ` role ` .
147+
148+
149+ ### try_hosts
150+
151+ ` syntax: redis, err = rc:try_hosts(hosts) `
152+
153+ Tries the hosts supplied in order and returns the first successful connection.
154+
155+
156+ ### connect_to_host
157+
158+ ` syntax: redis, err = rc:connect_to_host(host) `
159+
160+ Attempts to connect to the supplied ` host ` .
161+
162+
163+ ## Sentinel Utilities
164+
165+ ### sentinel.get_master
166+
167+ ` syntax: master, err = sentinel.get_master(sentinel, master_name) `
168+
169+ Given a connected Sentinel instance and a master name, will return the current master Redis instance.
170+
171+
172+ ### sentinel.get_slaves
173+
174+ ` syntax: slaves, err = sentinel.get_slaves(sentinel, master_name) `
175+
176+ Given a connected Sentinel instance and a master name, will return a list of registered slave Redis instances.
177+
178+
122179## TODO
123180
124181* Redis Cluster support.
133190
134191This module is licensed under the 2-clause BSD license.
135192
136- Copyright (c) 2013 , James Hurst <[email protected] > 193+ Copyright (c)
2015 , James Hurst
< [email protected] > 137194
138195All rights reserved.
139196
0 commit comments