You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2264 lines
72 KiB

8 years ago
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <meta name="theme-color" content="#375EAB">
  7. <title>redis - The Go Programming Language</title>
  8. <link type="text/css" rel="stylesheet" href="../../../../../lib/godoc/style.css">
  9. <link rel="stylesheet" href="../../../../../lib/godoc/jquery.treeview.css">
  10. <script type="text/javascript">window.initFuncs = [];</script>
  11. </head>
  12. <body>
  13. <div id='lowframe' style="position: fixed; bottom: 0; left: 0; height: 0; width: 100%; border-top: thin solid grey; background-color: white; overflow: auto;">
  14. ...
  15. </div><!-- #lowframe -->
  16. <div id="topbar" class="wide"><div class="container">
  17. <div class="top-heading" id="heading-wide"><a href="http://localhost:6060/">The Go Programming Language</a></div>
  18. <div class="top-heading" id="heading-narrow"><a href="http://localhost:6060/">Go</a></div>
  19. <a href="index.html#" id="menu-button"><span id="menu-button-arrow">&#9661;</span></a>
  20. <form method="GET" action="http://localhost:6060/search">
  21. <div id="menu">
  22. <a href="http://localhost:6060/doc/">Documents</a>
  23. <a href="http://localhost:6060/pkg/">Packages</a>
  24. <a href="http://localhost:6060/project/">The Project</a>
  25. <a href="http://localhost:6060/help/">Help</a>
  26. <a href="http://localhost:6060/blog/">Blog</a>
  27. <input type="text" id="search" name="q" class="inactive" value="Search" placeholder="Search">
  28. </div>
  29. </form>
  30. </div></div>
  31. <div id="page" class="wide">
  32. <div class="container">
  33. <h1>Package redis</h1>
  34. <div id="nav"></div>
  35. <!--
  36. Copyright 2009 The Go Authors. All rights reserved.
  37. Use of this source code is governed by a BSD-style
  38. license that can be found in the LICENSE file.
  39. -->
  40. <!--
  41. Note: Static (i.e., not template-generated) href and id
  42. attributes start with "pkg-" to make it impossible for
  43. them to conflict with generated attributes (some of which
  44. correspond to Go identifiers).
  45. -->
  46. <script type='text/javascript'>
  47. document.ANALYSIS_DATA = null;
  48. document.CALLGRAPH = null;
  49. </script>
  50. <div id="short-nav">
  51. <dl>
  52. <dd><code>import "github.com/garyburd/redigo/redis"</code></dd>
  53. </dl>
  54. <dl>
  55. <dd><a href="index.html#pkg-overview" class="overviewLink">Overview</a></dd>
  56. <dd><a href="index.html#pkg-index" class="indexLink">Index</a></dd>
  57. <dd><a href="index.html#pkg-examples" class="examplesLink">Examples</a></dd>
  58. </dl>
  59. </div>
  60. <!-- The package's Name is printed as title by the top-level template -->
  61. <div id="pkg-overview" class="toggleVisible">
  62. <div class="collapsed">
  63. <h2 class="toggleButton" title="Click to show Overview section">Overview ▹</h2>
  64. </div>
  65. <div class="expanded">
  66. <h2 class="toggleButton" title="Click to hide Overview section">Overview ▾</h2>
  67. <p>
  68. Package redis is a client for the Redis database.
  69. </p>
  70. <p>
  71. The Redigo FAQ (<a href="https://github.com/garyburd/redigo/wiki/FAQ">https://github.com/garyburd/redigo/wiki/FAQ</a>) contains more
  72. documentation about this package.
  73. </p>
  74. <h3 id="hdr-Connections">Connections</h3>
  75. <p>
  76. The Conn interface is the primary interface for working with Redis.
  77. Applications create connections by calling the Dial, DialWithTimeout or
  78. NewConn functions. In the future, functions will be added for creating
  79. sharded and other types of connections.
  80. </p>
  81. <p>
  82. The application must call the connection Close method when the application
  83. is done with the connection.
  84. </p>
  85. <h3 id="hdr-Executing_Commands">Executing Commands</h3>
  86. <p>
  87. The Conn interface has a generic method for executing Redis commands:
  88. </p>
  89. <pre>Do(commandName string, args ...interface{}) (reply interface{}, err error)
  90. </pre>
  91. <p>
  92. The Redis command reference (<a href="http://redis.io/commands">http://redis.io/commands</a>) lists the available
  93. commands. An example of using the Redis APPEND command is:
  94. </p>
  95. <pre>n, err := conn.Do(&#34;APPEND&#34;, &#34;key&#34;, &#34;value&#34;)
  96. </pre>
  97. <p>
  98. The Do method converts command arguments to binary strings for transmission
  99. to the server as follows:
  100. </p>
  101. <pre>Go Type Conversion
  102. []byte Sent as is
  103. string Sent as is
  104. int, int64 strconv.FormatInt(v)
  105. float64 strconv.FormatFloat(v, &#39;g&#39;, -1, 64)
  106. bool true -&gt; &#34;1&#34;, false -&gt; &#34;0&#34;
  107. nil &#34;&#34;
  108. all other types fmt.Print(v)
  109. </pre>
  110. <p>
  111. Redis command reply types are represented using the following Go types:
  112. </p>
  113. <pre>Redis type Go type
  114. error redis.Error
  115. integer int64
  116. simple string string
  117. bulk string []byte or nil if value not present.
  118. array []interface{} or nil if value not present.
  119. </pre>
  120. <p>
  121. Use type assertions or the reply helper functions to convert from
  122. interface{} to the specific Go type for the command result.
  123. </p>
  124. <h3 id="hdr-Pipelining">Pipelining</h3>
  125. <p>
  126. Connections support pipelining using the Send, Flush and Receive methods.
  127. </p>
  128. <pre>Send(commandName string, args ...interface{}) error
  129. Flush() error
  130. Receive() (reply interface{}, err error)
  131. </pre>
  132. <p>
  133. Send writes the command to the connection&#39;s output buffer. Flush flushes the
  134. connection&#39;s output buffer to the server. Receive reads a single reply from
  135. the server. The following example shows a simple pipeline.
  136. </p>
  137. <pre>c.Send(&#34;SET&#34;, &#34;foo&#34;, &#34;bar&#34;)
  138. c.Send(&#34;GET&#34;, &#34;foo&#34;)
  139. c.Flush()
  140. c.Receive() // reply from SET
  141. v, err = c.Receive() // reply from GET
  142. </pre>
  143. <p>
  144. The Do method combines the functionality of the Send, Flush and Receive
  145. methods. The Do method starts by writing the command and flushing the output
  146. buffer. Next, the Do method receives all pending replies including the reply
  147. for the command just sent by Do. If any of the received replies is an error,
  148. then Do returns the error. If there are no errors, then Do returns the last
  149. reply. If the command argument to the Do method is &#34;&#34;, then the Do method
  150. will flush the output buffer and receive pending replies without sending a
  151. command.
  152. </p>
  153. <p>
  154. Use the Send and Do methods to implement pipelined transactions.
  155. </p>
  156. <pre>c.Send(&#34;MULTI&#34;)
  157. c.Send(&#34;INCR&#34;, &#34;foo&#34;)
  158. c.Send(&#34;INCR&#34;, &#34;bar&#34;)
  159. r, err := c.Do(&#34;EXEC&#34;)
  160. fmt.Println(r) // prints [1, 1]
  161. </pre>
  162. <h3 id="hdr-Concurrency">Concurrency</h3>
  163. <p>
  164. Connections support one concurrent caller to the Receive method and one
  165. concurrent caller to the Send and Flush methods. No other concurrency is
  166. supported including concurrent calls to the Do method.
  167. </p>
  168. <p>
  169. For full concurrent access to Redis, use the thread-safe Pool to get, use
  170. and release a connection from within a goroutine. Connections returned from
  171. a Pool have the concurrency restrictions described in the previous
  172. paragraph.
  173. </p>
  174. <h3 id="hdr-Publish_and_Subscribe">Publish and Subscribe</h3>
  175. <p>
  176. Use the Send, Flush and Receive methods to implement Pub/Sub subscribers.
  177. </p>
  178. <pre>c.Send(&#34;SUBSCRIBE&#34;, &#34;example&#34;)
  179. c.Flush()
  180. for {
  181. reply, err := c.Receive()
  182. if err != nil {
  183. return err
  184. }
  185. // process pushed message
  186. }
  187. </pre>
  188. <p>
  189. The PubSubConn type wraps a Conn with convenience methods for implementing
  190. subscribers. The Subscribe, PSubscribe, Unsubscribe and PUnsubscribe methods
  191. send and flush a subscription management command. The receive method
  192. converts a pushed message to convenient types for use in a type switch.
  193. </p>
  194. <pre>psc := redis.PubSubConn{c}
  195. psc.Subscribe(&#34;example&#34;)
  196. for {
  197. switch v := psc.Receive().(type) {
  198. case redis.Message:
  199. fmt.Printf(&#34;%s: message: %s\n&#34;, v.Channel, v.Data)
  200. case redis.Subscription:
  201. fmt.Printf(&#34;%s: %s %d\n&#34;, v.Channel, v.Kind, v.Count)
  202. case error:
  203. return v
  204. }
  205. }
  206. </pre>
  207. <h3 id="hdr-Reply_Helpers">Reply Helpers</h3>
  208. <p>
  209. The Bool, Int, Bytes, String, Strings and Values functions convert a reply
  210. to a value of a specific type. To allow convenient wrapping of calls to the
  211. connection Do and Receive methods, the functions take a second argument of
  212. type error. If the error is non-nil, then the helper function returns the
  213. error. If the error is nil, the function converts the reply to the specified
  214. type:
  215. </p>
  216. <pre>exists, err := redis.Bool(c.Do(&#34;EXISTS&#34;, &#34;foo&#34;))
  217. if err != nil {
  218. // handle error return from c.Do or type conversion error.
  219. }
  220. </pre>
  221. <p>
  222. The Scan function converts elements of a array reply to Go types:
  223. </p>
  224. <pre>var value1 int
  225. var value2 string
  226. reply, err := redis.Values(c.Do(&#34;MGET&#34;, &#34;key1&#34;, &#34;key2&#34;))
  227. if err != nil {
  228. // handle error
  229. }
  230. if _, err := redis.Scan(reply, &amp;value1, &amp;value2); err != nil {
  231. // handle error
  232. }
  233. </pre>
  234. </div>
  235. </div>
  236. <div id="example__zpop" class="toggle">
  237. <div class="collapsed">
  238. <p class="exampleHeading toggleButton"><span class="text">Example (Zpop)</span></p>
  239. </div>
  240. <div class="expanded">
  241. <p class="exampleHeading toggleButton"><span class="text">Example (Zpop)</span></p>
  242. <p>This example implements ZPOP as described at
  243. http://redis.io/topics/transactions using WATCH/MULTI/EXEC and scripting.
  244. </p>
  245. <p>Code:</p>
  246. <pre class="code">package redis_test
  247. import (
  248. &#34;fmt&#34;
  249. &#34;github.com/garyburd/redigo/redis&#34;
  250. )
  251. <span class="comment">// zpop pops a value from the ZSET key using WATCH/MULTI/EXEC commands.</span>
  252. func zpop(c redis.Conn, key string) (result string, err error) {
  253. defer func() {
  254. <span class="comment">// Return connection to normal state on error.</span>
  255. if err != nil {
  256. c.Do(&#34;DISCARD&#34;)
  257. }
  258. }()
  259. <span class="comment">// Loop until transaction is successful.</span>
  260. for {
  261. if _, err := c.Do(&#34;WATCH&#34;, key); err != nil {
  262. return &#34;&#34;, err
  263. }
  264. members, err := redis.Strings(c.Do(&#34;ZRANGE&#34;, key, 0, 0))
  265. if err != nil {
  266. return &#34;&#34;, err
  267. }
  268. if len(members) != 1 {
  269. return &#34;&#34;, redis.ErrNil
  270. }
  271. c.Send(&#34;MULTI&#34;)
  272. c.Send(&#34;ZREM&#34;, key, members[0])
  273. queued, err := c.Do(&#34;EXEC&#34;)
  274. if err != nil {
  275. return &#34;&#34;, err
  276. }
  277. if queued != nil {
  278. result = members[0]
  279. break
  280. }
  281. }
  282. return result, nil
  283. }
  284. <span class="comment">// zpopScript pops a value from a ZSET.</span>
  285. var zpopScript = redis.NewScript(1, `
  286. local r = redis.call(&#39;ZRANGE&#39;, KEYS[1], 0, 0)
  287. if r ~= nil then
  288. r = r[1]
  289. redis.call(&#39;ZREM&#39;, KEYS[1], r)
  290. end
  291. return r
  292. `)
  293. <span class="comment">// This example implements ZPOP as described at</span>
  294. <span class="comment">// http://redis.io/topics/transactions using WATCH/MULTI/EXEC and scripting.</span>
  295. func Example_zpop() {
  296. c, err := dial()
  297. if err != nil {
  298. fmt.Println(err)
  299. return
  300. }
  301. defer c.Close()
  302. <span class="comment">// Add test data using a pipeline.</span>
  303. for i, member := range []string{&#34;red&#34;, &#34;blue&#34;, &#34;green&#34;} {
  304. c.Send(&#34;ZADD&#34;, &#34;zset&#34;, i, member)
  305. }
  306. if _, err := c.Do(&#34;&#34;); err != nil {
  307. fmt.Println(err)
  308. return
  309. }
  310. <span class="comment">// Pop using WATCH/MULTI/EXEC</span>
  311. v, err := zpop(c, &#34;zset&#34;)
  312. if err != nil {
  313. fmt.Println(err)
  314. return
  315. }
  316. fmt.Println(v)
  317. <span class="comment">// Pop using a script.</span>
  318. v, err = redis.String(zpopScript.Do(c, &#34;zset&#34;))
  319. if err != nil {
  320. fmt.Println(err)
  321. return
  322. }
  323. fmt.Println(v)
  324. <span class="comment">// Output:</span>
  325. <span class="comment">// red</span>
  326. <span class="comment">// blue</span>
  327. }
  328. </pre>
  329. </div>
  330. </div>
  331. <div id="pkg-index" class="toggleVisible">
  332. <div class="collapsed">
  333. <h2 class="toggleButton" title="Click to show Index section">Index ▹</h2>
  334. </div>
  335. <div class="expanded">
  336. <h2 class="toggleButton" title="Click to hide Index section">Index ▾</h2>
  337. <!-- Table of contents for API; must be named manual-nav to turn off auto nav. -->
  338. <div id="manual-nav">
  339. <dl>
  340. <dd><a href="index.html#pkg-variables">Variables</a></dd>
  341. <dd><a href="index.html#Bool">func Bool(reply interface{}, err error) (bool, error)</a></dd>
  342. <dd><a href="index.html#ByteSlices">func ByteSlices(reply interface{}, err error) ([][]byte, error)</a></dd>
  343. <dd><a href="index.html#Bytes">func Bytes(reply interface{}, err error) ([]byte, error)</a></dd>
  344. <dd><a href="index.html#Float64">func Float64(reply interface{}, err error) (float64, error)</a></dd>
  345. <dd><a href="index.html#Int">func Int(reply interface{}, err error) (int, error)</a></dd>
  346. <dd><a href="index.html#Int64">func Int64(reply interface{}, err error) (int64, error)</a></dd>
  347. <dd><a href="index.html#Int64Map">func Int64Map(result interface{}, err error) (map[string]int64, error)</a></dd>
  348. <dd><a href="index.html#IntMap">func IntMap(result interface{}, err error) (map[string]int, error)</a></dd>
  349. <dd><a href="index.html#Ints">func Ints(reply interface{}, err error) ([]int, error)</a></dd>
  350. <dd><a href="index.html#MultiBulk">func MultiBulk(reply interface{}, err error) ([]interface{}, error)</a></dd>
  351. <dd><a href="index.html#Scan">func Scan(src []interface{}, dest ...interface{}) ([]interface{}, error)</a></dd>
  352. <dd><a href="index.html#ScanSlice">func ScanSlice(src []interface{}, dest interface{}, fieldNames ...string) error</a></dd>
  353. <dd><a href="index.html#ScanStruct">func ScanStruct(src []interface{}, dest interface{}) error</a></dd>
  354. <dd><a href="index.html#String">func String(reply interface{}, err error) (string, error)</a></dd>
  355. <dd><a href="index.html#StringMap">func StringMap(result interface{}, err error) (map[string]string, error)</a></dd>
  356. <dd><a href="index.html#Strings">func Strings(reply interface{}, err error) ([]string, error)</a></dd>
  357. <dd><a href="index.html#Uint64">func Uint64(reply interface{}, err error) (uint64, error)</a></dd>
  358. <dd><a href="index.html#Values">func Values(reply interface{}, err error) ([]interface{}, error)</a></dd>
  359. <dd><a href="index.html#Args">type Args</a></dd>
  360. <dd>&nbsp; &nbsp; <a href="index.html#Args.Add">func (args Args) Add(value ...interface{}) Args</a></dd>
  361. <dd>&nbsp; &nbsp; <a href="index.html#Args.AddFlat">func (args Args) AddFlat(v interface{}) Args</a></dd>
  362. <dd><a href="index.html#Conn">type Conn</a></dd>
  363. <dd>&nbsp; &nbsp; <a href="index.html#Dial">func Dial(network, address string, options ...DialOption) (Conn, error)</a></dd>
  364. <dd>&nbsp; &nbsp; <a href="index.html#DialTimeout">func DialTimeout(network, address string, connectTimeout, readTimeout, writeTimeout time.Duration) (Conn, error)</a></dd>
  365. <dd>&nbsp; &nbsp; <a href="index.html#DialURL">func DialURL(rawurl string, options ...DialOption) (Conn, error)</a></dd>
  366. <dd>&nbsp; &nbsp; <a href="index.html#NewConn">func NewConn(netConn net.Conn, readTimeout, writeTimeout time.Duration) Conn</a></dd>
  367. <dd>&nbsp; &nbsp; <a href="index.html#NewLoggingConn">func NewLoggingConn(conn Conn, logger *log.Logger, prefix string) Conn</a></dd>
  368. <dd><a href="index.html#DialOption">type DialOption</a></dd>
  369. <dd>&nbsp; &nbsp; <a href="index.html#DialConnectTimeout">func DialConnectTimeout(d time.Duration) DialOption</a></dd>
  370. <dd>&nbsp; &nbsp; <a href="index.html#DialDatabase">func DialDatabase(db int) DialOption</a></dd>
  371. <dd>&nbsp; &nbsp; <a href="index.html#DialNetDial">func DialNetDial(dial func(network, addr string) (net.Conn, error)) DialOption</a></dd>
  372. <dd>&nbsp; &nbsp; <a href="index.html#DialPassword">func DialPassword(password string) DialOption</a></dd>
  373. <dd>&nbsp; &nbsp; <a href="index.html#DialReadTimeout">func DialReadTimeout(d time.Duration) DialOption</a></dd>
  374. <dd>&nbsp; &nbsp; <a href="index.html#DialWriteTimeout">func DialWriteTimeout(d time.Duration) DialOption</a></dd>
  375. <dd><a href="index.html#Error">type Error</a></dd>
  376. <dd>&nbsp; &nbsp; <a href="index.html#Error.Error">func (err Error) Error() string</a></dd>
  377. <dd><a href="index.html#Message">type Message</a></dd>
  378. <dd><a href="index.html#PMessage">type PMessage</a></dd>
  379. <dd><a href="index.html#Pong">type Pong</a></dd>
  380. <dd><a href="index.html#Pool">type Pool</a></dd>
  381. <dd>&nbsp; &nbsp; <a href="index.html#NewPool">func NewPool(newFn func() (Conn, error), maxIdle int) *Pool</a></dd>
  382. <dd>&nbsp; &nbsp; <a href="index.html#Pool.ActiveCount">func (p *Pool) ActiveCount() int</a></dd>
  383. <dd>&nbsp; &nbsp; <a href="index.html#Pool.Close">func (p *Pool) Close() error</a></dd>
  384. <dd>&nbsp; &nbsp; <a href="index.html#Pool.Get">func (p *Pool) Get() Conn</a></dd>
  385. <dd><a href="index.html#PubSubConn">type PubSubConn</a></dd>
  386. <dd>&nbsp; &nbsp; <a href="index.html#PubSubConn.Close">func (c PubSubConn) Close() error</a></dd>
  387. <dd>&nbsp; &nbsp; <a href="index.html#PubSubConn.PSubscribe">func (c PubSubConn) PSubscribe(channel ...interface{}) error</a></dd>
  388. <dd>&nbsp; &nbsp; <a href="index.html#PubSubConn.PUnsubscribe">func (c PubSubConn) PUnsubscribe(channel ...interface{}) error</a></dd>
  389. <dd>&nbsp; &nbsp; <a href="index.html#PubSubConn.Ping">func (c PubSubConn) Ping(data string) error</a></dd>
  390. <dd>&nbsp; &nbsp; <a href="index.html#PubSubConn.Receive">func (c PubSubConn) Receive() interface{}</a></dd>
  391. <dd>&nbsp; &nbsp; <a href="index.html#PubSubConn.Subscribe">func (c PubSubConn) Subscribe(channel ...interface{}) error</a></dd>
  392. <dd>&nbsp; &nbsp; <a href="index.html#PubSubConn.Unsubscribe">func (c PubSubConn) Unsubscribe(channel ...interface{}) error</a></dd>
  393. <dd><a href="index.html#Script">type Script</a></dd>
  394. <dd>&nbsp; &nbsp; <a href="index.html#NewScript">func NewScript(keyCount int, src string) *Script</a></dd>
  395. <dd>&nbsp; &nbsp; <a href="index.html#Script.Do">func (s *Script) Do(c Conn, keysAndArgs ...interface{}) (interface{}, error)</a></dd>
  396. <dd>&nbsp; &nbsp; <a href="index.html#Script.Load">func (s *Script) Load(c Conn) error</a></dd>
  397. <dd>&nbsp; &nbsp; <a href="index.html#Script.Send">func (s *Script) Send(c Conn, keysAndArgs ...interface{}) error</a></dd>
  398. <dd>&nbsp; &nbsp; <a href="index.html#Script.SendHash">func (s *Script) SendHash(c Conn, keysAndArgs ...interface{}) error</a></dd>
  399. <dd><a href="index.html#Subscription">type Subscription</a></dd>
  400. </dl>
  401. </div><!-- #manual-nav -->
  402. <div id="pkg-examples">
  403. <h4>Examples</h4>
  404. <dl>
  405. <dd><a class="exampleLink" href="index.html#example_Args">Args</a></dd>
  406. <dd><a class="exampleLink" href="index.html#example_Bool">Bool</a></dd>
  407. <dd><a class="exampleLink" href="index.html#example_Dial">Dial</a></dd>
  408. <dd><a class="exampleLink" href="index.html#example_DialURL">DialURL</a></dd>
  409. <dd><a class="exampleLink" href="index.html#example_Int">Int</a></dd>
  410. <dd><a class="exampleLink" href="index.html#example_Ints">Ints</a></dd>
  411. <dd><a class="exampleLink" href="index.html#example_PubSubConn">PubSubConn</a></dd>
  412. <dd><a class="exampleLink" href="index.html#example_Scan">Scan</a></dd>
  413. <dd><a class="exampleLink" href="index.html#example_ScanSlice">ScanSlice</a></dd>
  414. <dd><a class="exampleLink" href="index.html#example_Script">Script</a></dd>
  415. <dd><a class="exampleLink" href="index.html#example_String">String</a></dd>
  416. <dd><a class="exampleLink" href="index.html#example__zpop">Package (Zpop)</a></dd>
  417. </dl>
  418. </div>
  419. <h4>Package files</h4>
  420. <p>
  421. <span style="font-size:90%">
  422. <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/conn.go">conn.go</a>
  423. <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/doc.go">doc.go</a>
  424. <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/log.go">log.go</a>
  425. <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/pool.go">pool.go</a>
  426. <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/pubsub.go">pubsub.go</a>
  427. <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/redis.go">redis.go</a>
  428. <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/reply.go">reply.go</a>
  429. <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/scan.go">scan.go</a>
  430. <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/script.go">script.go</a>
  431. </span>
  432. </p>
  433. </div><!-- .expanded -->
  434. </div><!-- #pkg-index -->
  435. <div id="pkg-callgraph" class="toggle" style="display: none">
  436. <div class="collapsed">
  437. <h2 class="toggleButton" title="Click to show Internal Call Graph section">Internal call graph ▹</h2>
  438. </div> <!-- .expanded -->
  439. <div class="expanded">
  440. <h2 class="toggleButton" title="Click to hide Internal Call Graph section">Internal call graph ▾</h2>
  441. <p>
  442. In the call graph viewer below, each node
  443. is a function belonging to this package
  444. and its children are the functions it
  445. calls&mdash;perhaps dynamically.
  446. </p>
  447. <p>
  448. The root nodes are the entry points of the
  449. package: functions that may be called from
  450. outside the package.
  451. There may be non-exported or anonymous
  452. functions among them if they are called
  453. dynamically from another package.
  454. </p>
  455. <p>
  456. Click a node to visit that function's source code.
  457. From there you can visit its callers by
  458. clicking its declaring <code>func</code>
  459. token.
  460. </p>
  461. <p>
  462. Functions may be omitted if they were
  463. determined to be unreachable in the
  464. particular programs or tests that were
  465. analyzed.
  466. </p>
  467. <!-- Zero means show all package entry points. -->
  468. <ul style="margin-left: 0.5in" id="callgraph-0" class="treeview"></ul>
  469. </div>
  470. </div> <!-- #pkg-callgraph -->
  471. <h2 id="pkg-variables">Variables</h2>
  472. <pre>var <span id="ErrNil">ErrNil</span> = <a href="../../../../errors/index.html">errors</a>.<a href="../../../../errors/index.html#New">New</a>(&#34;redigo: nil returned&#34;)</pre>
  473. <p>
  474. ErrNil indicates that a reply value is nil.
  475. </p>
  476. <pre>var <span id="ErrPoolExhausted">ErrPoolExhausted</span> = <a href="../../../../errors/index.html">errors</a>.<a href="../../../../errors/index.html#New">New</a>(&#34;redigo: connection pool exhausted&#34;)</pre>
  477. <p>
  478. ErrPoolExhausted is returned from a pool connection method (Do, Send,
  479. Receive, Flush, Err) when the maximum number of database connections in the
  480. pool has been reached.
  481. </p>
  482. <h2 id="Bool">func <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/reply.go?s=5571:5624#L191">Bool</a></h2>
  483. <pre>func Bool(reply interface{}, err <a href="../../../../builtin/index.html#error">error</a>) (<a href="../../../../builtin/index.html#bool">bool</a>, <a href="../../../../builtin/index.html#error">error</a>)</pre>
  484. <p>
  485. Bool is a helper that converts a command reply to a boolean. If err is not
  486. equal to nil, then Bool returns false, err. Otherwise Bool converts the
  487. reply to boolean as follows:
  488. </p>
  489. <pre>Reply type Result
  490. integer value != 0, nil
  491. bulk string strconv.ParseBool(reply)
  492. nil false, ErrNil
  493. other false, error
  494. </pre>
  495. <div id="example_Bool" class="toggle">
  496. <div class="collapsed">
  497. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  498. </div>
  499. <div class="expanded">
  500. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  501. <p>Code:</p>
  502. <pre class="code">c, err := dial()
  503. if err != nil {
  504. fmt.Println(err)
  505. return
  506. }
  507. defer c.Close()
  508. c.Do(&#34;SET&#34;, &#34;foo&#34;, 1)
  509. exists, _ := redis.Bool(c.Do(&#34;EXISTS&#34;, &#34;foo&#34;))
  510. fmt.Printf(&#34;%#v\n&#34;, exists)
  511. <span class="comment"></pre>
  512. <p>Output:</p>
  513. <pre class="output">true
  514. </pre>
  515. </div>
  516. </div>
  517. <h2 id="ByteSlices">func <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/reply.go?s=7948:8011#L270">ByteSlices</a></h2>
  518. <pre>func ByteSlices(reply interface{}, err <a href="../../../../builtin/index.html#error">error</a>) ([][]<a href="../../../../builtin/index.html#byte">byte</a>, <a href="../../../../builtin/index.html#error">error</a>)</pre>
  519. <p>
  520. ByteSlices is a helper that converts an array command reply to a [][]byte.
  521. If err is not equal to nil, then ByteSlices returns nil, err. Nil array
  522. items are stay nil. ByteSlices returns an error if an array item is not a
  523. bulk string or nil.
  524. </p>
  525. <h2 id="Bytes">func <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/reply.go?s=4851:4907#L165">Bytes</a></h2>
  526. <pre>func Bytes(reply interface{}, err <a href="../../../../builtin/index.html#error">error</a>) ([]<a href="../../../../builtin/index.html#byte">byte</a>, <a href="../../../../builtin/index.html#error">error</a>)</pre>
  527. <p>
  528. Bytes is a helper that converts a command reply to a slice of bytes. If err
  529. is not equal to nil, then Bytes returns nil, err. Otherwise Bytes converts
  530. the reply to a slice of bytes as follows:
  531. </p>
  532. <pre>Reply type Result
  533. bulk string reply, nil
  534. simple string []byte(reply), nil
  535. nil nil, ErrNil
  536. other nil, error
  537. </pre>
  538. <h2 id="Float64">func <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/reply.go?s=3422:3481#L114">Float64</a></h2>
  539. <pre>func Float64(reply interface{}, err <a href="../../../../builtin/index.html#error">error</a>) (<a href="../../../../builtin/index.html#float64">float64</a>, <a href="../../../../builtin/index.html#error">error</a>)</pre>
  540. <p>
  541. Float64 is a helper that converts a command reply to 64 bit float. If err is
  542. not equal to nil, then Float64 returns 0, err. Otherwise, Float64 converts
  543. the reply to an int as follows:
  544. </p>
  545. <pre>Reply type Result
  546. bulk string parsed reply, nil
  547. nil 0, ErrNil
  548. other 0, error
  549. </pre>
  550. <h2 id="Int">func <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/reply.go?s=1070:1121#L25">Int</a></h2>
  551. <pre>func Int(reply interface{}, err <a href="../../../../builtin/index.html#error">error</a>) (<a href="../../../../builtin/index.html#int">int</a>, <a href="../../../../builtin/index.html#error">error</a>)</pre>
  552. <p>
  553. Int is a helper that converts a command reply to an integer. If err is not
  554. equal to nil, then Int returns 0, err. Otherwise, Int converts the
  555. reply to an int as follows:
  556. </p>
  557. <pre>Reply type Result
  558. integer int(reply), nil
  559. bulk string parsed reply, nil
  560. nil 0, ErrNil
  561. other 0, error
  562. </pre>
  563. <div id="example_Int" class="toggle">
  564. <div class="collapsed">
  565. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  566. </div>
  567. <div class="expanded">
  568. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  569. <p>Code:</p>
  570. <pre class="code">c, err := dial()
  571. if err != nil {
  572. fmt.Println(err)
  573. return
  574. }
  575. defer c.Close()
  576. c.Do(&#34;SET&#34;, &#34;k1&#34;, 1)
  577. n, _ := redis.Int(c.Do(&#34;GET&#34;, &#34;k1&#34;))
  578. fmt.Printf(&#34;%#v\n&#34;, n)
  579. n, _ = redis.Int(c.Do(&#34;INCR&#34;, &#34;k1&#34;))
  580. fmt.Printf(&#34;%#v\n&#34;, n)
  581. <span class="comment"></pre>
  582. <p>Output:</p>
  583. <pre class="output">1
  584. 2
  585. </pre>
  586. </div>
  587. </div>
  588. <h2 id="Int64">func <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/reply.go?s=1865:1920#L56">Int64</a></h2>
  589. <pre>func Int64(reply interface{}, err <a href="../../../../builtin/index.html#error">error</a>) (<a href="../../../../builtin/index.html#int64">int64</a>, <a href="../../../../builtin/index.html#error">error</a>)</pre>
  590. <p>
  591. Int64 is a helper that converts a command reply to 64 bit integer. If err is
  592. not equal to nil, then Int returns 0, err. Otherwise, Int64 converts the
  593. reply to an int64 as follows:
  594. </p>
  595. <pre>Reply type Result
  596. integer reply, nil
  597. bulk string parsed reply, nil
  598. nil 0, ErrNil
  599. other 0, error
  600. </pre>
  601. <h2 id="Int64Map">func <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/reply.go?s=10725:10795#L362">Int64Map</a></h2>
  602. <pre>func Int64Map(result interface{}, err <a href="../../../../builtin/index.html#error">error</a>) (map[<a href="../../../../builtin/index.html#string">string</a>]<a href="../../../../builtin/index.html#int64">int64</a>, <a href="../../../../builtin/index.html#error">error</a>)</pre>
  603. <p>
  604. Int64Map is a helper that converts an array of strings (alternating key, value)
  605. into a map[string]int64. The HGETALL commands return replies in this format.
  606. Requires an even number of values in result.
  607. </p>
  608. <h2 id="IntMap">func <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/reply.go?s=9937:10003#L336">IntMap</a></h2>
  609. <pre>func IntMap(result interface{}, err <a href="../../../../builtin/index.html#error">error</a>) (map[<a href="../../../../builtin/index.html#string">string</a>]<a href="../../../../builtin/index.html#int">int</a>, <a href="../../../../builtin/index.html#error">error</a>)</pre>
  610. <p>
  611. IntMap is a helper that converts an array of strings (alternating key, value)
  612. into a map[string]int. The HGETALL commands return replies in this format.
  613. Requires an even number of values in result.
  614. </p>
  615. <h2 id="Ints">func <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/reply.go?s=8685:8739#L298">Ints</a></h2>
  616. <pre>func Ints(reply interface{}, err <a href="../../../../builtin/index.html#error">error</a>) ([]<a href="../../../../builtin/index.html#int">int</a>, <a href="../../../../builtin/index.html#error">error</a>)</pre>
  617. <p>
  618. Ints is a helper that converts an array command reply to a []int. If
  619. err is not equal to nil, then Ints returns nil, err.
  620. </p>
  621. <div id="example_Ints" class="toggle">
  622. <div class="collapsed">
  623. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  624. </div>
  625. <div class="expanded">
  626. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  627. <p>Code:</p>
  628. <pre class="code">c, err := dial()
  629. if err != nil {
  630. fmt.Println(err)
  631. return
  632. }
  633. defer c.Close()
  634. c.Do(&#34;SADD&#34;, &#34;set_with_integers&#34;, 4, 5, 6)
  635. ints, _ := redis.Ints(c.Do(&#34;SMEMBERS&#34;, &#34;set_with_integers&#34;))
  636. fmt.Printf(&#34;%#v\n&#34;, ints)
  637. <span class="comment"></pre>
  638. <p>Output:</p>
  639. <pre class="output">[]int{4, 5, 6}
  640. </pre>
  641. </div>
  642. </div>
  643. <h2 id="MultiBulk">func <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/reply.go?s=6070:6137#L211">MultiBulk</a></h2>
  644. <pre>func MultiBulk(reply interface{}, err <a href="../../../../builtin/index.html#error">error</a>) ([]interface{}, <a href="../../../../builtin/index.html#error">error</a>)</pre>
  645. <p>
  646. MultiBulk is a helper that converts an array command reply to a []interface{}.
  647. </p>
  648. <p>
  649. Deprecated: Use Values instead.
  650. </p>
  651. <h2 id="Scan">func <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/scan.go?s=5368:5440#L218">Scan</a></h2>
  652. <pre>func Scan(src []interface{}, dest ...interface{}) ([]interface{}, <a href="../../../../builtin/index.html#error">error</a>)</pre>
  653. <p>
  654. Scan copies from src to the values pointed at by dest.
  655. </p>
  656. <p>
  657. The values pointed at by dest must be an integer, float, boolean, string,
  658. []byte, interface{} or slices of these types. Scan uses the standard strconv
  659. package to convert bulk strings to numeric and boolean types.
  660. </p>
  661. <p>
  662. If a dest value is nil, then the corresponding src value is skipped.
  663. </p>
  664. <p>
  665. If a src element is nil, then the corresponding dest value is not modified.
  666. </p>
  667. <p>
  668. To enable easy use of Scan in a loop, Scan returns the slice of src
  669. following the copied values.
  670. </p>
  671. <div id="example_Scan" class="toggle">
  672. <div class="collapsed">
  673. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  674. </div>
  675. <div class="expanded">
  676. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  677. <p>Code:</p>
  678. <pre class="code">c, err := dial()
  679. if err != nil {
  680. fmt.Println(err)
  681. return
  682. }
  683. defer c.Close()
  684. c.Send(&#34;HMSET&#34;, &#34;album:1&#34;, &#34;title&#34;, &#34;Red&#34;, &#34;rating&#34;, 5)
  685. c.Send(&#34;HMSET&#34;, &#34;album:2&#34;, &#34;title&#34;, &#34;Earthbound&#34;, &#34;rating&#34;, 1)
  686. c.Send(&#34;HMSET&#34;, &#34;album:3&#34;, &#34;title&#34;, &#34;Beat&#34;)
  687. c.Send(&#34;LPUSH&#34;, &#34;albums&#34;, &#34;1&#34;)
  688. c.Send(&#34;LPUSH&#34;, &#34;albums&#34;, &#34;2&#34;)
  689. c.Send(&#34;LPUSH&#34;, &#34;albums&#34;, &#34;3&#34;)
  690. values, err := redis.Values(c.Do(&#34;SORT&#34;, &#34;albums&#34;,
  691. &#34;BY&#34;, &#34;album:*-&gt;rating&#34;,
  692. &#34;GET&#34;, &#34;album:*-&gt;title&#34;,
  693. &#34;GET&#34;, &#34;album:*-&gt;rating&#34;))
  694. if err != nil {
  695. fmt.Println(err)
  696. return
  697. }
  698. for len(values) &gt; 0 {
  699. var title string
  700. rating := -1 <span class="comment">// initialize to illegal value to detect nil.</span>
  701. values, err = redis.Scan(values, &amp;title, &amp;rating)
  702. if err != nil {
  703. fmt.Println(err)
  704. return
  705. }
  706. if rating == -1 {
  707. fmt.Println(title, &#34;not-rated&#34;)
  708. } else {
  709. fmt.Println(title, rating)
  710. }
  711. }
  712. <span class="comment"></pre>
  713. <p>Output:</p>
  714. <pre class="output">Beat not-rated
  715. Earthbound 1
  716. Red 5
  717. </pre>
  718. </div>
  719. </div>
  720. <h2 id="ScanSlice">func <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/scan.go?s=9900:9979#L398">ScanSlice</a></h2>
  721. <pre>func ScanSlice(src []interface{}, dest interface{}, fieldNames ...<a href="../../../../builtin/index.html#string">string</a>) <a href="../../../../builtin/index.html#error">error</a></pre>
  722. <p>
  723. ScanSlice scans src to the slice pointed to by dest. The elements the dest
  724. slice must be integer, float, boolean, string, struct or pointer to struct
  725. values.
  726. </p>
  727. <p>
  728. Struct fields must be integer, float, boolean or string values. All struct
  729. fields are used unless a subset is specified using fieldNames.
  730. </p>
  731. <div id="example_ScanSlice" class="toggle">
  732. <div class="collapsed">
  733. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  734. </div>
  735. <div class="expanded">
  736. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  737. <p>Code:</p>
  738. <pre class="code">c, err := dial()
  739. if err != nil {
  740. fmt.Println(err)
  741. return
  742. }
  743. defer c.Close()
  744. c.Send(&#34;HMSET&#34;, &#34;album:1&#34;, &#34;title&#34;, &#34;Red&#34;, &#34;rating&#34;, 5)
  745. c.Send(&#34;HMSET&#34;, &#34;album:2&#34;, &#34;title&#34;, &#34;Earthbound&#34;, &#34;rating&#34;, 1)
  746. c.Send(&#34;HMSET&#34;, &#34;album:3&#34;, &#34;title&#34;, &#34;Beat&#34;, &#34;rating&#34;, 4)
  747. c.Send(&#34;LPUSH&#34;, &#34;albums&#34;, &#34;1&#34;)
  748. c.Send(&#34;LPUSH&#34;, &#34;albums&#34;, &#34;2&#34;)
  749. c.Send(&#34;LPUSH&#34;, &#34;albums&#34;, &#34;3&#34;)
  750. values, err := redis.Values(c.Do(&#34;SORT&#34;, &#34;albums&#34;,
  751. &#34;BY&#34;, &#34;album:*-&gt;rating&#34;,
  752. &#34;GET&#34;, &#34;album:*-&gt;title&#34;,
  753. &#34;GET&#34;, &#34;album:*-&gt;rating&#34;))
  754. if err != nil {
  755. fmt.Println(err)
  756. return
  757. }
  758. var albums []struct {
  759. Title string
  760. Rating int
  761. }
  762. if err := redis.ScanSlice(values, &amp;albums); err != nil {
  763. fmt.Println(err)
  764. return
  765. }
  766. fmt.Printf(&#34;%v\n&#34;, albums)
  767. <span class="comment"></pre>
  768. <p>Output:</p>
  769. <pre class="output">[{Earthbound 1} {Beat 4} {Red 5}]
  770. </pre>
  771. </div>
  772. </div>
  773. <h2 id="ScanStruct">func <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/scan.go?s=8655:8713#L353">ScanStruct</a></h2>
  774. <pre>func ScanStruct(src []interface{}, dest interface{}) <a href="../../../../builtin/index.html#error">error</a></pre>
  775. <p>
  776. ScanStruct scans alternating names and values from src to a struct. The
  777. HGETALL and CONFIG GET commands return replies in this format.
  778. </p>
  779. <p>
  780. ScanStruct uses exported field names to match values in the response. Use
  781. &#39;redis&#39; field tag to override the name:
  782. </p>
  783. <pre>Field int `redis:&#34;myName&#34;`
  784. </pre>
  785. <p>
  786. Fields with the tag redis:&#34;-&#34; are ignored.
  787. </p>
  788. <p>
  789. Integer, float, boolean, string and []byte fields are supported. Scan uses the
  790. standard strconv package to convert bulk string values to numeric and
  791. boolean types.
  792. </p>
  793. <p>
  794. If a src element is nil, then the corresponding field is not modified.
  795. </p>
  796. <h2 id="String">func <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/reply.go?s=4131:4188#L139">String</a></h2>
  797. <pre>func String(reply interface{}, err <a href="../../../../builtin/index.html#error">error</a>) (<a href="../../../../builtin/index.html#string">string</a>, <a href="../../../../builtin/index.html#error">error</a>)</pre>
  798. <p>
  799. String is a helper that converts a command reply to a string. If err is not
  800. equal to nil, then String returns &#34;&#34;, err. Otherwise String converts the
  801. reply to a string as follows:
  802. </p>
  803. <pre>Reply type Result
  804. bulk string string(reply), nil
  805. simple string reply, nil
  806. nil &#34;&#34;, ErrNil
  807. other &#34;&#34;, error
  808. </pre>
  809. <div id="example_String" class="toggle">
  810. <div class="collapsed">
  811. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  812. </div>
  813. <div class="expanded">
  814. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  815. <p>Code:</p>
  816. <pre class="code">c, err := dial()
  817. if err != nil {
  818. fmt.Println(err)
  819. return
  820. }
  821. defer c.Close()
  822. c.Do(&#34;SET&#34;, &#34;hello&#34;, &#34;world&#34;)
  823. s, err := redis.String(c.Do(&#34;GET&#34;, &#34;hello&#34;))
  824. fmt.Printf(&#34;%#v\n&#34;, s)
  825. <span class="comment"></pre>
  826. <p>Output:</p>
  827. <pre class="output">&#34;world&#34;
  828. </pre>
  829. </div>
  830. </div>
  831. <h2 id="StringMap">func <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/reply.go?s=9153:9225#L313">StringMap</a></h2>
  832. <pre>func StringMap(result interface{}, err <a href="../../../../builtin/index.html#error">error</a>) (map[<a href="../../../../builtin/index.html#string">string</a>]<a href="../../../../builtin/index.html#string">string</a>, <a href="../../../../builtin/index.html#error">error</a>)</pre>
  833. <p>
  834. StringMap is a helper that converts an array of strings (alternating key, value)
  835. into a map[string]string. The HGETALL and CONFIG GET commands return replies in this format.
  836. Requires an even number of values in result.
  837. </p>
  838. <h2 id="Strings">func <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/reply.go?s=7087:7147#L240">Strings</a></h2>
  839. <pre>func Strings(reply interface{}, err <a href="../../../../builtin/index.html#error">error</a>) ([]<a href="../../../../builtin/index.html#string">string</a>, <a href="../../../../builtin/index.html#error">error</a>)</pre>
  840. <p>
  841. Strings is a helper that converts an array command reply to a []string. If
  842. err is not equal to nil, then Strings returns nil, err. Nil array items are
  843. converted to &#34;&#34; in the output slice. Strings returns an error if an array
  844. item is not a bulk string or nil.
  845. </p>
  846. <h2 id="Uint64">func <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/reply.go?s=2662:2719#L85">Uint64</a></h2>
  847. <pre>func Uint64(reply interface{}, err <a href="../../../../builtin/index.html#error">error</a>) (<a href="../../../../builtin/index.html#uint64">uint64</a>, <a href="../../../../builtin/index.html#error">error</a>)</pre>
  848. <p>
  849. Uint64 is a helper that converts a command reply to 64 bit integer. If err is
  850. not equal to nil, then Int returns 0, err. Otherwise, Int64 converts the
  851. reply to an int64 as follows:
  852. </p>
  853. <pre>Reply type Result
  854. integer reply, nil
  855. bulk string parsed reply, nil
  856. nil 0, ErrNil
  857. other 0, error
  858. </pre>
  859. <h2 id="Values">func <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/reply.go?s=6485:6549#L221">Values</a></h2>
  860. <pre>func Values(reply interface{}, err <a href="../../../../builtin/index.html#error">error</a>) ([]interface{}, <a href="../../../../builtin/index.html#error">error</a>)</pre>
  861. <p>
  862. Values is a helper that converts an array command reply to a []interface{}.
  863. If err is not equal to nil, then Values returns nil, err. Otherwise, Values
  864. converts the reply as follows:
  865. </p>
  866. <pre>Reply type Result
  867. array reply, nil
  868. nil nil, ErrNil
  869. other nil, error
  870. </pre>
  871. <h2 id="Args">type <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/scan.go?s=11617:11640#L472">Args</a></h2>
  872. <pre>type Args []interface{}</pre>
  873. <p>
  874. Args is a helper for constructing command arguments from structured values.
  875. </p>
  876. <div id="example_Args" class="toggle">
  877. <div class="collapsed">
  878. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  879. </div>
  880. <div class="expanded">
  881. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  882. <p>Code:</p>
  883. <pre class="code">c, err := dial()
  884. if err != nil {
  885. fmt.Println(err)
  886. return
  887. }
  888. defer c.Close()
  889. var p1, p2 struct {
  890. Title string `redis:&#34;title&#34;`
  891. Author string `redis:&#34;author&#34;`
  892. Body string `redis:&#34;body&#34;`
  893. }
  894. p1.Title = &#34;Example&#34;
  895. p1.Author = &#34;Gary&#34;
  896. p1.Body = &#34;Hello&#34;
  897. if _, err := c.Do(&#34;HMSET&#34;, redis.Args{}.Add(&#34;id1&#34;).AddFlat(&amp;p1)...); err != nil {
  898. fmt.Println(err)
  899. return
  900. }
  901. m := map[string]string{
  902. &#34;title&#34;: &#34;Example2&#34;,
  903. &#34;author&#34;: &#34;Steve&#34;,
  904. &#34;body&#34;: &#34;Map&#34;,
  905. }
  906. if _, err := c.Do(&#34;HMSET&#34;, redis.Args{}.Add(&#34;id2&#34;).AddFlat(m)...); err != nil {
  907. fmt.Println(err)
  908. return
  909. }
  910. for _, id := range []string{&#34;id1&#34;, &#34;id2&#34;} {
  911. v, err := redis.Values(c.Do(&#34;HGETALL&#34;, id))
  912. if err != nil {
  913. fmt.Println(err)
  914. return
  915. }
  916. if err := redis.ScanStruct(v, &amp;p2); err != nil {
  917. fmt.Println(err)
  918. return
  919. }
  920. fmt.Printf(&#34;%+v\n&#34;, p2)
  921. }
  922. <span class="comment"></pre>
  923. <p>Output:</p>
  924. <pre class="output">{Title:Example Author:Gary Body:Hello}
  925. {Title:Example2 Author:Steve Body:Map}
  926. </pre>
  927. </div>
  928. </div>
  929. <h3 id="Args.Add">func (Args) <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/scan.go?s=11696:11743#L475">Add</a></h3>
  930. <pre>func (args <a href="index.html#Args">Args</a>) Add(value ...interface{}) <a href="index.html#Args">Args</a></pre>
  931. <p>
  932. Add returns the result of appending value to args.
  933. </p>
  934. <h3 id="Args.AddFlat">func (Args) <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/scan.go?s=12346:12390#L491">AddFlat</a></h3>
  935. <pre>func (args <a href="index.html#Args">Args</a>) AddFlat(v interface{}) <a href="index.html#Args">Args</a></pre>
  936. <p>
  937. AddFlat returns the result of appending the flattened value of v to args.
  938. </p>
  939. <p>
  940. Maps are flattened by appending the alternating keys and map values to args.
  941. </p>
  942. <p>
  943. Slices are flattened by appending the slice elements to args.
  944. </p>
  945. <p>
  946. Structs are flattened by appending the alternating names and values of
  947. exported fields to args. If v is a nil struct pointer, then nothing is
  948. appended. The &#39;redis&#39; field tag overrides struct field names. See ScanStruct
  949. for more information on the use of the &#39;redis&#39; field tag.
  950. </p>
  951. <p>
  952. Other types are appended to args as is.
  953. </p>
  954. <h2 id="Conn">type <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/redis.go?s=786:1553#L13">Conn</a></h2>
  955. <pre>type Conn interface {
  956. <span class="comment">// Close closes the connection.</span>
  957. Close() <a href="../../../../builtin/index.html#error">error</a>
  958. <span class="comment">// Err returns a non-nil value if the connection is broken. The returned</span>
  959. <span class="comment">// value is either the first non-nil value returned from the underlying</span>
  960. <span class="comment">// network connection or a protocol parsing error. Applications should</span>
  961. <span class="comment">// close broken connections.</span>
  962. Err() <a href="../../../../builtin/index.html#error">error</a>
  963. <span class="comment">// Do sends a command to the server and returns the received reply.</span>
  964. Do(commandName <a href="../../../../builtin/index.html#string">string</a>, args ...interface{}) (reply interface{}, err <a href="../../../../builtin/index.html#error">error</a>)
  965. <span class="comment">// Send writes the command to the client&#39;s output buffer.</span>
  966. Send(commandName <a href="../../../../builtin/index.html#string">string</a>, args ...interface{}) <a href="../../../../builtin/index.html#error">error</a>
  967. <span class="comment">// Flush flushes the output buffer to the Redis server.</span>
  968. Flush() <a href="../../../../builtin/index.html#error">error</a>
  969. <span class="comment">// Receive receives a single reply from the Redis server</span>
  970. Receive() (reply interface{}, err <a href="../../../../builtin/index.html#error">error</a>)
  971. }</pre>
  972. <p>
  973. Conn represents a connection to a Redis server.
  974. </p>
  975. <h3 id="Dial">func <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/conn.go?s=3355:3426#L118">Dial</a></h3>
  976. <pre>func Dial(network, address <a href="../../../../builtin/index.html#string">string</a>, options ...<a href="index.html#DialOption">DialOption</a>) (<a href="index.html#Conn">Conn</a>, <a href="../../../../builtin/index.html#error">error</a>)</pre>
  977. <p>
  978. Dial connects to the Redis server at the given network and
  979. address using the specified options.
  980. </p>
  981. <div id="example_Dial" class="toggle">
  982. <div class="collapsed">
  983. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  984. </div>
  985. <div class="expanded">
  986. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  987. <p>Connect to local instance of Redis running on the default port.
  988. </p>
  989. <p>Code:</p>
  990. <pre class="code">
  991. c, err := redis.Dial(&#34;tcp&#34;, &#34;:6379&#34;)
  992. if err != nil {
  993. <span class="comment">// handle error</span>
  994. }
  995. defer c.Close()
  996. </pre>
  997. </div>
  998. </div>
  999. <h3 id="DialTimeout">func <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/conn.go?s=1355:1467#L50">DialTimeout</a></h3>
  1000. <pre>func DialTimeout(network, address <a href="../../../../builtin/index.html#string">string</a>, connectTimeout, readTimeout, writeTimeout <a href="../../../../time/index.html">time</a>.<a href="../../../../time/index.html#Duration">Duration</a>) (<a href="index.html#Conn">Conn</a>, <a href="../../../../builtin/index.html#error">error</a>)</pre>
  1001. <p>
  1002. DialTimeout acts like Dial but takes timeouts for establishing the
  1003. connection to the server, writing a command and reading a reply.
  1004. </p>
  1005. <p>
  1006. Deprecated: Use Dial with options instead.
  1007. </p>
  1008. <h3 id="DialURL">func <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/conn.go?s=4315:4379#L160">DialURL</a></h3>
  1009. <pre>func DialURL(rawurl <a href="../../../../builtin/index.html#string">string</a>, options ...<a href="index.html#DialOption">DialOption</a>) (<a href="index.html#Conn">Conn</a>, <a href="../../../../builtin/index.html#error">error</a>)</pre>
  1010. <p>
  1011. DialURL connects to a Redis server at the given URL using the Redis
  1012. URI scheme. URLs should follow the draft IANA specification for the
  1013. scheme (<a href="https://www.iana.org/assignments/uri-schemes/prov/redis">https://www.iana.org/assignments/uri-schemes/prov/redis</a>).
  1014. </p>
  1015. <div id="example_DialURL" class="toggle">
  1016. <div class="collapsed">
  1017. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  1018. </div>
  1019. <div class="expanded">
  1020. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  1021. <p>Connect to remote instance of Redis using a URL.
  1022. </p>
  1023. <p>Code:</p>
  1024. <pre class="code">
  1025. c, err := redis.DialURL(os.Getenv(&#34;REDIS_URL&#34;))
  1026. if err != nil {
  1027. <span class="comment">// handle connection error</span>
  1028. }
  1029. defer c.Close()
  1030. </pre>
  1031. </div>
  1032. </div>
  1033. <h3 id="NewConn">func <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/conn.go?s=5495:5571#L210">NewConn</a></h3>
  1034. <pre>func NewConn(netConn <a href="../../../../net/index.html">net</a>.<a href="../../../../net/index.html#Conn">Conn</a>, readTimeout, writeTimeout <a href="../../../../time/index.html">time</a>.<a href="../../../../time/index.html#Duration">Duration</a>) <a href="index.html#Conn">Conn</a></pre>
  1035. <p>
  1036. NewConn returns a new Redigo connection for the given net connection.
  1037. </p>
  1038. <h3 id="NewLoggingConn">func <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/log.go?s=702:772#L14">NewLoggingConn</a></h3>
  1039. <pre>func NewLoggingConn(conn <a href="index.html#Conn">Conn</a>, logger *<a href="../../../../log/index.html">log</a>.<a href="../../../../log/index.html#Logger">Logger</a>, prefix <a href="../../../../builtin/index.html#string">string</a>) <a href="index.html#Conn">Conn</a></pre>
  1040. <p>
  1041. NewLoggingConn returns a logging wrapper around a connection.
  1042. </p>
  1043. <h2 id="DialOption">type <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/conn.go?s=1670:1718#L58">DialOption</a></h2>
  1044. <pre>type DialOption struct {
  1045. <span class="comment">// contains filtered or unexported fields</span>
  1046. }</pre>
  1047. <p>
  1048. DialOption specifies an option for dialing a Redis server.
  1049. </p>
  1050. <h3 id="DialConnectTimeout">func <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/conn.go?s=2378:2429#L85">DialConnectTimeout</a></h3>
  1051. <pre>func DialConnectTimeout(d <a href="../../../../time/index.html">time</a>.<a href="../../../../time/index.html#Duration">Duration</a>) <a href="index.html#DialOption">DialOption</a></pre>
  1052. <p>
  1053. DialConnectTimeout specifies the timeout for connecting to the Redis server.
  1054. </p>
  1055. <h3 id="DialDatabase">func <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/conn.go?s=2942:2978#L102">DialDatabase</a></h3>
  1056. <pre>func DialDatabase(db <a href="../../../../builtin/index.html#int">int</a>) <a href="index.html#DialOption">DialOption</a></pre>
  1057. <p>
  1058. DialDatabase specifies the database to select when dialing a connection.
  1059. </p>
  1060. <h3 id="DialNetDial">func <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/conn.go?s=2718:2796#L95">DialNetDial</a></h3>
  1061. <pre>func DialNetDial(dial func(network, addr <a href="../../../../builtin/index.html#string">string</a>) (<a href="../../../../net/index.html">net</a>.<a href="../../../../net/index.html#Conn">Conn</a>, <a href="../../../../builtin/index.html#error">error</a>)) <a href="index.html#DialOption">DialOption</a></pre>
  1062. <p>
  1063. DialNetDial specifies a custom dial function for creating TCP
  1064. connections. If this option is left out, then net.Dial is
  1065. used. DialNetDial overrides DialConnectTimeout.
  1066. </p>
  1067. <h3 id="DialPassword">func <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/conn.go?s=3130:3175#L110">DialPassword</a></h3>
  1068. <pre>func DialPassword(password <a href="../../../../builtin/index.html#string">string</a>) <a href="index.html#DialOption">DialOption</a></pre>
  1069. <p>
  1070. DialPassword specifies the password to use when connecting to
  1071. the Redis server.
  1072. </p>
  1073. <h3 id="DialReadTimeout">func <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/conn.go?s=1980:2028#L71">DialReadTimeout</a></h3>
  1074. <pre>func DialReadTimeout(d <a href="../../../../time/index.html">time</a>.<a href="../../../../time/index.html#Duration">Duration</a>) <a href="index.html#DialOption">DialOption</a></pre>
  1075. <p>
  1076. DialReadTimeout specifies the timeout for reading a single command reply.
  1077. </p>
  1078. <h3 id="DialWriteTimeout">func <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/conn.go?s=2174:2223#L78">DialWriteTimeout</a></h3>
  1079. <pre>func DialWriteTimeout(d <a href="../../../../time/index.html">time</a>.<a href="../../../../time/index.html#Duration">Duration</a>) <a href="index.html#DialOption">DialOption</a></pre>
  1080. <p>
  1081. DialWriteTimeout specifies the timeout for writing a single command.
  1082. </p>
  1083. <h2 id="Error">type <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/redis.go?s=660:677#L8">Error</a></h2>
  1084. <pre>type Error <a href="../../../../builtin/index.html#string">string</a></pre>
  1085. <p>
  1086. Error represents an error returned in a command reply.
  1087. </p>
  1088. <h3 id="Error.Error">func (Error) <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/redis.go?s=679:710#L10">Error</a></h3>
  1089. <pre>func (err <a href="index.html#Error">Error</a>) Error() <a href="../../../../builtin/index.html#string">string</a></pre>
  1090. <h2 id="Message">type <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/pubsub.go?s=967:1072#L23">Message</a></h2>
  1091. <pre>type Message struct {
  1092. <span class="comment">// The originating channel.</span>
  1093. Channel <a href="../../../../builtin/index.html#string">string</a>
  1094. <span class="comment">// The message data.</span>
  1095. Data []<a href="../../../../builtin/index.html#byte">byte</a>
  1096. }</pre>
  1097. <p>
  1098. Message represents a message notification.
  1099. </p>
  1100. <h2 id="PMessage">type <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/pubsub.go?s=1122:1270#L33">PMessage</a></h2>
  1101. <pre>type PMessage struct {
  1102. <span class="comment">// The matched pattern.</span>
  1103. Pattern <a href="../../../../builtin/index.html#string">string</a>
  1104. <span class="comment">// The originating channel.</span>
  1105. Channel <a href="../../../../builtin/index.html#string">string</a>
  1106. <span class="comment">// The message data.</span>
  1107. Data []<a href="../../../../builtin/index.html#byte">byte</a>
  1108. }</pre>
  1109. <p>
  1110. PMessage represents a pmessage notification.
  1111. </p>
  1112. <h2 id="Pong">type <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/pubsub.go?s=1319:1352#L46">Pong</a></h2>
  1113. <pre>type Pong struct {
  1114. Data <a href="../../../../builtin/index.html#string">string</a>
  1115. }</pre>
  1116. <p>
  1117. Pong represents a pubsub pong notification.
  1118. </p>
  1119. <h2 id="Pool">type <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/pool.go?s=2976:4413#L88">Pool</a></h2>
  1120. <pre>type Pool struct {
  1121. <span class="comment">// Dial is an application supplied function for creating and configuring a</span>
  1122. <span class="comment">// connection.</span>
  1123. <span class="comment">//</span>
  1124. <span class="comment">// The connection returned from Dial must not be in a special state</span>
  1125. <span class="comment">// (subscribed to pubsub channel, transaction started, ...).</span>
  1126. Dial func() (<a href="index.html#Conn">Conn</a>, <a href="../../../../builtin/index.html#error">error</a>)
  1127. <span class="comment">// TestOnBorrow is an optional application supplied function for checking</span>
  1128. <span class="comment">// the health of an idle connection before the connection is used again by</span>
  1129. <span class="comment">// the application. Argument t is the time that the connection was returned</span>
  1130. <span class="comment">// to the pool. If the function returns an error, then the connection is</span>
  1131. <span class="comment">// closed.</span>
  1132. TestOnBorrow func(c <a href="index.html#Conn">Conn</a>, t <a href="../../../../time/index.html">time</a>.<a href="../../../../time/index.html#Time">Time</a>) <a href="../../../../builtin/index.html#error">error</a>
  1133. <span class="comment">// Maximum number of idle connections in the pool.</span>
  1134. MaxIdle <a href="../../../../builtin/index.html#int">int</a>
  1135. <span class="comment">// Maximum number of connections allocated by the pool at a given time.</span>
  1136. <span class="comment">// When zero, there is no limit on the number of connections in the pool.</span>
  1137. MaxActive <a href="../../../../builtin/index.html#int">int</a>
  1138. <span class="comment">// Close connections after remaining idle for this duration. If the value</span>
  1139. <span class="comment">// is zero, then idle connections are not closed. Applications should set</span>
  1140. <span class="comment">// the timeout to a value less than the server&#39;s timeout.</span>
  1141. IdleTimeout <a href="../../../../time/index.html">time</a>.<a href="../../../../time/index.html#Duration">Duration</a>
  1142. <span class="comment">// If Wait is true and the pool is at the MaxActive limit, then Get() waits</span>
  1143. <span class="comment">// for a connection to be returned to the pool before returning.</span>
  1144. Wait <a href="../../../../builtin/index.html#bool">bool</a>
  1145. <span class="comment">// contains filtered or unexported fields</span>
  1146. }</pre>
  1147. <p>
  1148. Pool maintains a pool of connections. The application calls the Get method
  1149. to get a connection from the pool and the connection&#39;s Close method to
  1150. return the connection&#39;s resources to the pool.
  1151. </p>
  1152. <p>
  1153. The following example shows how to use a pool in a web application. The
  1154. application creates a pool at application startup and makes it available to
  1155. request handlers using a global variable. The pool configuration used here
  1156. is an example, not a recommendation.
  1157. </p>
  1158. <pre>func newPool(server, password string) *redis.Pool {
  1159. return &amp;redis.Pool{
  1160. MaxIdle: 3,
  1161. IdleTimeout: 240 * time.Second,
  1162. Dial: func () (redis.Conn, error) {
  1163. c, err := redis.Dial(&#34;tcp&#34;, server)
  1164. if err != nil {
  1165. return nil, err
  1166. }
  1167. if _, err := c.Do(&#34;AUTH&#34;, password); err != nil {
  1168. c.Close()
  1169. return nil, err
  1170. }
  1171. return c, err
  1172. },
  1173. TestOnBorrow: func(c redis.Conn, t time.Time) error {
  1174. if time.Since(t) &lt; time.Minute {
  1175. return nil
  1176. }
  1177. _, err := c.Do(&#34;PING&#34;)
  1178. return err
  1179. },
  1180. }
  1181. }
  1182. var (
  1183. pool *redis.Pool
  1184. redisServer = flag.String(&#34;redisServer&#34;, &#34;:6379&#34;, &#34;&#34;)
  1185. redisPassword = flag.String(&#34;redisPassword&#34;, &#34;&#34;, &#34;&#34;)
  1186. )
  1187. func main() {
  1188. flag.Parse()
  1189. pool = newPool(*redisServer, *redisPassword)
  1190. ...
  1191. }
  1192. </pre>
  1193. <p>
  1194. A request handler gets a connection from the pool and closes the connection
  1195. when the handler is done:
  1196. </p>
  1197. <pre>func serveHome(w http.ResponseWriter, r *http.Request) {
  1198. conn := pool.Get()
  1199. defer conn.Close()
  1200. ....
  1201. }
  1202. </pre>
  1203. <h3 id="NewPool">func <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/pool.go?s=4566:4625#L138">NewPool</a></h3>
  1204. <pre>func NewPool(newFn func() (<a href="index.html#Conn">Conn</a>, <a href="../../../../builtin/index.html#error">error</a>), maxIdle <a href="../../../../builtin/index.html#int">int</a>) *<a href="index.html#Pool">Pool</a></pre>
  1205. <p>
  1206. NewPool creates a new pool.
  1207. </p>
  1208. <p>
  1209. Deprecated: Initialize the Pool directory as shown in the example.
  1210. </p>
  1211. <h3 id="Pool.ActiveCount">func (*Pool) <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/pool.go?s=5236:5268#L156">ActiveCount</a></h3>
  1212. <pre>func (p *<a href="index.html#Pool">Pool</a>) ActiveCount() <a href="../../../../builtin/index.html#int">int</a></pre>
  1213. <p>
  1214. ActiveCount returns the number of active connections in the pool.
  1215. </p>
  1216. <h3 id="Pool.Close">func (*Pool) <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/pool.go?s=5387:5415#L164">Close</a></h3>
  1217. <pre>func (p *<a href="index.html#Pool">Pool</a>) Close() <a href="../../../../builtin/index.html#error">error</a></pre>
  1218. <p>
  1219. Close releases the resources used by the pool.
  1220. </p>
  1221. <h3 id="Pool.Get">func (*Pool) <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/pool.go?s=5029:5054#L147">Get</a></h3>
  1222. <pre>func (p *<a href="index.html#Pool">Pool</a>) Get() <a href="index.html#Conn">Conn</a></pre>
  1223. <p>
  1224. Get gets a connection. The application must close the returned connection.
  1225. This method always returns a valid connection so that applications can defer
  1226. error handling to the first use of the connection. If there is an error
  1227. getting an underlying connection, then the connection Err, Do, Send, Flush
  1228. and Receive methods return that error.
  1229. </p>
  1230. <h2 id="PubSubConn">type <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/pubsub.go?s=1423:1460#L51">PubSubConn</a></h2>
  1231. <pre>type PubSubConn struct {
  1232. Conn <a href="index.html#Conn">Conn</a>
  1233. }</pre>
  1234. <p>
  1235. PubSubConn wraps a Conn with convenience methods for subscribers.
  1236. </p>
  1237. <div id="example_PubSubConn" class="toggle">
  1238. <div class="collapsed">
  1239. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  1240. </div>
  1241. <div class="expanded">
  1242. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  1243. <p>Applications can receive pushed messages from one goroutine and manage subscriptions from another goroutine.
  1244. </p>
  1245. <p>Code:</p>
  1246. <pre class="code">c, err := dial()
  1247. if err != nil {
  1248. fmt.Println(err)
  1249. return
  1250. }
  1251. defer c.Close()
  1252. var wg sync.WaitGroup
  1253. wg.Add(2)
  1254. psc := redis.PubSubConn{Conn: c}
  1255. <span class="comment">// This goroutine receives and prints pushed notifications from the server.</span>
  1256. <span class="comment">// The goroutine exits when the connection is unsubscribed from all</span>
  1257. <span class="comment">// channels or there is an error.</span>
  1258. go func() {
  1259. defer wg.Done()
  1260. for {
  1261. switch n := psc.Receive().(type) {
  1262. case redis.Message:
  1263. fmt.Printf(&#34;Message: %s %s\n&#34;, n.Channel, n.Data)
  1264. case redis.PMessage:
  1265. fmt.Printf(&#34;PMessage: %s %s %s\n&#34;, n.Pattern, n.Channel, n.Data)
  1266. case redis.Subscription:
  1267. fmt.Printf(&#34;Subscription: %s %s %d\n&#34;, n.Kind, n.Channel, n.Count)
  1268. if n.Count == 0 {
  1269. return
  1270. }
  1271. case error:
  1272. fmt.Printf(&#34;error: %v\n&#34;, n)
  1273. return
  1274. }
  1275. }
  1276. }()
  1277. <span class="comment">// This goroutine manages subscriptions for the connection.</span>
  1278. go func() {
  1279. defer wg.Done()
  1280. psc.Subscribe(&#34;example&#34;)
  1281. psc.PSubscribe(&#34;p*&#34;)
  1282. <span class="comment">// The following function calls publish a message using another</span>
  1283. <span class="comment">// connection to the Redis server.</span>
  1284. publish(&#34;example&#34;, &#34;hello&#34;)
  1285. publish(&#34;example&#34;, &#34;world&#34;)
  1286. publish(&#34;pexample&#34;, &#34;foo&#34;)
  1287. publish(&#34;pexample&#34;, &#34;bar&#34;)
  1288. <span class="comment">// Unsubscribe from all connections. This will cause the receiving</span>
  1289. <span class="comment">// goroutine to exit.</span>
  1290. psc.Unsubscribe()
  1291. psc.PUnsubscribe()
  1292. }()
  1293. wg.Wait()
  1294. <span class="comment"></pre>
  1295. <p>Output:</p>
  1296. <pre class="output">Subscription: subscribe example 1
  1297. Subscription: psubscribe p* 2
  1298. Message: example hello
  1299. Message: example world
  1300. PMessage: p* pexample foo
  1301. PMessage: p* pexample bar
  1302. Subscription: unsubscribe example 1
  1303. Subscription: punsubscribe p* 0
  1304. </pre>
  1305. </div>
  1306. </div>
  1307. <h3 id="PubSubConn.Close">func (PubSubConn) <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/pubsub.go?s=1494:1527#L56">Close</a></h3>
  1308. <pre>func (c <a href="index.html#PubSubConn">PubSubConn</a>) Close() <a href="../../../../builtin/index.html#error">error</a></pre>
  1309. <p>
  1310. Close closes the connection.
  1311. </p>
  1312. <h3 id="PubSubConn.PSubscribe">func (PubSubConn) <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/pubsub.go?s=1811:1871#L67">PSubscribe</a></h3>
  1313. <pre>func (c <a href="index.html#PubSubConn">PubSubConn</a>) PSubscribe(channel ...interface{}) <a href="../../../../builtin/index.html#error">error</a></pre>
  1314. <p>
  1315. PSubscribe subscribes the connection to the given patterns.
  1316. </p>
  1317. <h3 id="PubSubConn.PUnsubscribe">func (PubSubConn) <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/pubsub.go?s=2288:2350#L81">PUnsubscribe</a></h3>
  1318. <pre>func (c <a href="index.html#PubSubConn">PubSubConn</a>) PUnsubscribe(channel ...interface{}) <a href="../../../../builtin/index.html#error">error</a></pre>
  1319. <p>
  1320. PUnsubscribe unsubscribes the connection from the given patterns, or from all
  1321. of them if none is given.
  1322. </p>
  1323. <h3 id="PubSubConn.Ping">func (PubSubConn) <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/pubsub.go?s=2480:2523#L87">Ping</a></h3>
  1324. <pre>func (c <a href="index.html#PubSubConn">PubSubConn</a>) Ping(data <a href="../../../../builtin/index.html#string">string</a>) <a href="../../../../builtin/index.html#error">error</a></pre>
  1325. <p>
  1326. Ping sends a PING to the server with the specified data.
  1327. </p>
  1328. <h3 id="PubSubConn.Receive">func (PubSubConn) <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/pubsub.go?s=2782:2823#L95">Receive</a></h3>
  1329. <pre>func (c <a href="index.html#PubSubConn">PubSubConn</a>) Receive() interface{}</pre>
  1330. <p>
  1331. Receive returns a pushed message as a Subscription, Message, PMessage, Pong
  1332. or error. The return value is intended to be used directly in a type switch
  1333. as illustrated in the PubSubConn example.
  1334. </p>
  1335. <h3 id="PubSubConn.Subscribe">func (PubSubConn) <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/pubsub.go?s=1622:1681#L61">Subscribe</a></h3>
  1336. <pre>func (c <a href="index.html#PubSubConn">PubSubConn</a>) Subscribe(channel ...interface{}) <a href="../../../../builtin/index.html#error">error</a></pre>
  1337. <p>
  1338. Subscribe subscribes the connection to the specified channels.
  1339. </p>
  1340. <h3 id="PubSubConn.Unsubscribe">func (PubSubConn) <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/pubsub.go?s=2048:2109#L74">Unsubscribe</a></h3>
  1341. <pre>func (c <a href="index.html#PubSubConn">PubSubConn</a>) Unsubscribe(channel ...interface{}) <a href="../../../../builtin/index.html#error">error</a></pre>
  1342. <p>
  1343. Unsubscribe unsubscribes the connection from the given channels, or from all
  1344. of them if none is given.
  1345. </p>
  1346. <h2 id="Script">type <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/script.go?s=808:878#L16">Script</a></h2>
  1347. <pre>type Script struct {
  1348. <span class="comment">// contains filtered or unexported fields</span>
  1349. }</pre>
  1350. <p>
  1351. Script encapsulates the source, hash and key count for a Lua script. See
  1352. <a href="http://redis.io/commands/eval">http://redis.io/commands/eval</a> for information on scripts in Redis.
  1353. </p>
  1354. <div id="example_Script" class="toggle">
  1355. <div class="collapsed">
  1356. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  1357. </div>
  1358. <div class="expanded">
  1359. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  1360. <p>Code:</p>
  1361. <pre class="code">
  1362. <span class="comment">// Initialize a package-level variable with a script.</span>
  1363. var getScript = redis.NewScript(1, `return redis.call(&#39;get&#39;, KEYS[1])`)
  1364. <span class="comment">// In a function, use the script Do method to evaluate the script. The Do</span>
  1365. <span class="comment">// method optimistically uses the EVALSHA command. If the script is not</span>
  1366. <span class="comment">// loaded, then the Do method falls back to the EVAL command.</span>
  1367. reply, err = getScript.Do(c, &#34;foo&#34;)
  1368. </pre>
  1369. </div>
  1370. </div>
  1371. <h3 id="NewScript">func <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/script.go?s=1212:1260#L27">NewScript</a></h3>
  1372. <pre>func NewScript(keyCount <a href="../../../../builtin/index.html#int">int</a>, src <a href="../../../../builtin/index.html#string">string</a>) *<a href="index.html#Script">Script</a></pre>
  1373. <p>
  1374. NewScript returns a new script object. If keyCount is greater than or equal
  1375. to zero, then the count is automatically inserted in the EVAL command
  1376. argument list. If keyCount is less than zero, then the application supplies
  1377. the count as the first value in the keysAndArgs argument to the Do, Send and
  1378. SendHash methods.
  1379. </p>
  1380. <h3 id="Script.Do">func (*Script) <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/script.go?s=2000:2076#L52">Do</a></h3>
  1381. <pre>func (s *<a href="index.html#Script">Script</a>) Do(c <a href="index.html#Conn">Conn</a>, keysAndArgs ...interface{}) (interface{}, <a href="../../../../builtin/index.html#error">error</a>)</pre>
  1382. <p>
  1383. Do evaluates the script. Under the covers, Do optimistically evaluates the
  1384. script using the EVALSHA command. If the command fails because the script is
  1385. not loaded, then Do evaluates the script using the EVAL command (thus
  1386. causing the script to load).
  1387. </p>
  1388. <h3 id="Script.Load">func (*Script) <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/script.go?s=2874:2909#L73">Load</a></h3>
  1389. <pre>func (s *<a href="index.html#Script">Script</a>) Load(c <a href="index.html#Conn">Conn</a>) <a href="../../../../builtin/index.html#error">error</a></pre>
  1390. <p>
  1391. Load loads the script without evaluating it.
  1392. </p>
  1393. <h3 id="Script.Send">func (*Script) <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/script.go?s=2703:2766#L68">Send</a></h3>
  1394. <pre>func (s *<a href="index.html#Script">Script</a>) Send(c <a href="index.html#Conn">Conn</a>, keysAndArgs ...interface{}) <a href="../../../../builtin/index.html#error">error</a></pre>
  1395. <p>
  1396. Send evaluates the script without waiting for the reply.
  1397. </p>
  1398. <h3 id="Script.SendHash">func (*Script) <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/script.go?s=2512:2579#L63">SendHash</a></h3>
  1399. <pre>func (s *<a href="index.html#Script">Script</a>) SendHash(c <a href="index.html#Conn">Conn</a>, keysAndArgs ...interface{}) <a href="../../../../builtin/index.html#error">error</a></pre>
  1400. <p>
  1401. SendHash evaluates the script without waiting for the reply. The script is
  1402. evaluated with the EVALSHA command. The application must ensure that the
  1403. script is loaded by a previous call to Send, Do or Load methods.
  1404. </p>
  1405. <h2 id="Subscription">type <a href="http://localhost:6060/src/github.com/garyburd/redigo/redis/pubsub.go?s=687:919#L10">Subscription</a></h2>
  1406. <pre>type Subscription struct {
  1407. <span class="comment">// Kind is &#34;subscribe&#34;, &#34;unsubscribe&#34;, &#34;psubscribe&#34; or &#34;punsubscribe&#34;</span>
  1408. Kind <a href="../../../../builtin/index.html#string">string</a>
  1409. <span class="comment">// The channel that was changed.</span>
  1410. Channel <a href="../../../../builtin/index.html#string">string</a>
  1411. <span class="comment">// The current number of subscriptions for connection.</span>
  1412. Count <a href="../../../../builtin/index.html#int">int</a>
  1413. }</pre>
  1414. <p>
  1415. Subscription represents a subscribe or unsubscribe notification.
  1416. </p>
  1417. <div id="footer">
  1418. Build version go1.6.<br>
  1419. Except as <a href="https://developers.google.com/site-policies#restrictions">noted</a>,
  1420. the content of this page is licensed under the
  1421. Creative Commons Attribution 3.0 License,
  1422. and code is licensed under a <a href="http://localhost:6060/LICENSE">BSD license</a>.<br>
  1423. <a href="http://localhost:6060/doc/tos.html">Terms of Service</a> |
  1424. <a href="http://www.google.com/intl/en/policies/privacy/">Privacy Policy</a>
  1425. </div>
  1426. </div><!-- .container -->
  1427. </div><!-- #page -->
  1428. <!-- TODO(adonovan): load these from <head> using "defer" attribute? -->
  1429. <script type="text/javascript" src="../../../../../lib/godoc/jquery.js"></script>
  1430. <script type="text/javascript" src="../../../../../lib/godoc/jquery.treeview.js"></script>
  1431. <script type="text/javascript" src="../../../../../lib/godoc/jquery.treeview.edit.js"></script>
  1432. <script type="text/javascript" src="../../../../../lib/godoc/godocs.js"></script>
  1433. </body>
  1434. </html>