commands.go 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246
  1. package redis
  2. import (
  3. "io"
  4. "strconv"
  5. "time"
  6. )
  7. func formatFloat(f float64) string {
  8. return strconv.FormatFloat(f, 'f', -1, 64)
  9. }
  10. func readTimeout(sec int64) time.Duration {
  11. if sec == 0 {
  12. return 0
  13. }
  14. return time.Duration(sec+1) * time.Second
  15. }
  16. //------------------------------------------------------------------------------
  17. func (c *Client) Auth(password string) *StatusCmd {
  18. cmd := NewStatusCmd("AUTH", password)
  19. c.Process(cmd)
  20. return cmd
  21. }
  22. func (c *Client) Echo(message string) *StringCmd {
  23. cmd := NewStringCmd("ECHO", message)
  24. c.Process(cmd)
  25. return cmd
  26. }
  27. func (c *Client) Ping() *StatusCmd {
  28. cmd := NewStatusCmd("PING")
  29. c.Process(cmd)
  30. return cmd
  31. }
  32. func (c *Client) Quit() *StatusCmd {
  33. panic("not implemented")
  34. }
  35. func (c *Client) Select(index int64) *StatusCmd {
  36. cmd := NewStatusCmd("SELECT", strconv.FormatInt(index, 10))
  37. c.Process(cmd)
  38. return cmd
  39. }
  40. //------------------------------------------------------------------------------
  41. func (c *Client) Del(keys ...string) *IntCmd {
  42. args := append([]string{"DEL"}, keys...)
  43. cmd := NewIntCmd(args...)
  44. c.Process(cmd)
  45. return cmd
  46. }
  47. func (c *Client) Dump(key string) *StringCmd {
  48. cmd := NewStringCmd("DUMP", key)
  49. c.Process(cmd)
  50. return cmd
  51. }
  52. func (c *Client) Exists(key string) *BoolCmd {
  53. cmd := NewBoolCmd("EXISTS", key)
  54. c.Process(cmd)
  55. return cmd
  56. }
  57. func (c *Client) Expire(key string, dur time.Duration) *BoolCmd {
  58. cmd := NewBoolCmd("EXPIRE", key, strconv.FormatInt(int64(dur/time.Second), 10))
  59. c.Process(cmd)
  60. return cmd
  61. }
  62. func (c *Client) ExpireAt(key string, tm time.Time) *BoolCmd {
  63. cmd := NewBoolCmd("EXPIREAT", key, strconv.FormatInt(tm.Unix(), 10))
  64. c.Process(cmd)
  65. return cmd
  66. }
  67. func (c *Client) Keys(pattern string) *StringSliceCmd {
  68. cmd := NewStringSliceCmd("KEYS", pattern)
  69. c.Process(cmd)
  70. return cmd
  71. }
  72. func (c *Client) Migrate(host, port, key string, db, timeout int64) *StatusCmd {
  73. cmd := NewStatusCmd(
  74. "MIGRATE",
  75. host,
  76. port,
  77. key,
  78. strconv.FormatInt(db, 10),
  79. strconv.FormatInt(timeout, 10),
  80. )
  81. cmd.setReadTimeout(readTimeout(timeout))
  82. c.Process(cmd)
  83. return cmd
  84. }
  85. func (c *Client) Move(key string, db int64) *BoolCmd {
  86. cmd := NewBoolCmd("MOVE", key, strconv.FormatInt(db, 10))
  87. c.Process(cmd)
  88. return cmd
  89. }
  90. func (c *Client) ObjectRefCount(keys ...string) *IntCmd {
  91. args := append([]string{"OBJECT", "REFCOUNT"}, keys...)
  92. cmd := NewIntCmd(args...)
  93. c.Process(cmd)
  94. return cmd
  95. }
  96. func (c *Client) ObjectEncoding(keys ...string) *StringCmd {
  97. args := append([]string{"OBJECT", "ENCODING"}, keys...)
  98. cmd := NewStringCmd(args...)
  99. c.Process(cmd)
  100. return cmd
  101. }
  102. func (c *Client) ObjectIdleTime(keys ...string) *DurationCmd {
  103. args := append([]string{"OBJECT", "IDLETIME"}, keys...)
  104. cmd := NewDurationCmd(time.Second, args...)
  105. c.Process(cmd)
  106. return cmd
  107. }
  108. func (c *Client) Persist(key string) *BoolCmd {
  109. cmd := NewBoolCmd("PERSIST", key)
  110. c.Process(cmd)
  111. return cmd
  112. }
  113. func (c *Client) PExpire(key string, dur time.Duration) *BoolCmd {
  114. cmd := NewBoolCmd("PEXPIRE", key, strconv.FormatInt(int64(dur/time.Millisecond), 10))
  115. c.Process(cmd)
  116. return cmd
  117. }
  118. func (c *Client) PExpireAt(key string, tm time.Time) *BoolCmd {
  119. cmd := NewBoolCmd(
  120. "PEXPIREAT",
  121. key,
  122. strconv.FormatInt(tm.UnixNano()/int64(time.Millisecond), 10),
  123. )
  124. c.Process(cmd)
  125. return cmd
  126. }
  127. func (c *Client) PTTL(key string) *DurationCmd {
  128. cmd := NewDurationCmd(time.Millisecond, "PTTL", key)
  129. c.Process(cmd)
  130. return cmd
  131. }
  132. func (c *Client) RandomKey() *StringCmd {
  133. cmd := NewStringCmd("RANDOMKEY")
  134. c.Process(cmd)
  135. return cmd
  136. }
  137. func (c *Client) Rename(key, newkey string) *StatusCmd {
  138. cmd := NewStatusCmd("RENAME", key, newkey)
  139. c.Process(cmd)
  140. return cmd
  141. }
  142. func (c *Client) RenameNX(key, newkey string) *BoolCmd {
  143. cmd := NewBoolCmd("RENAMENX", key, newkey)
  144. c.Process(cmd)
  145. return cmd
  146. }
  147. func (c *Client) Restore(key string, ttl int64, value string) *StatusCmd {
  148. cmd := NewStatusCmd(
  149. "RESTORE",
  150. key,
  151. strconv.FormatInt(ttl, 10),
  152. value,
  153. )
  154. c.Process(cmd)
  155. return cmd
  156. }
  157. type Sort struct {
  158. By string
  159. Offset, Count float64
  160. Get []string
  161. Order string
  162. IsAlpha bool
  163. Store string
  164. }
  165. func (c *Client) Sort(key string, sort Sort) *StringSliceCmd {
  166. args := []string{"SORT", key}
  167. if sort.By != "" {
  168. args = append(args, "BY", sort.By)
  169. }
  170. if sort.Offset != 0 || sort.Count != 0 {
  171. args = append(args, "LIMIT", formatFloat(sort.Offset), formatFloat(sort.Count))
  172. }
  173. for _, get := range sort.Get {
  174. args = append(args, "GET", get)
  175. }
  176. if sort.Order != "" {
  177. args = append(args, sort.Order)
  178. }
  179. if sort.IsAlpha {
  180. args = append(args, "ALPHA")
  181. }
  182. if sort.Store != "" {
  183. args = append(args, "STORE", sort.Store)
  184. }
  185. cmd := NewStringSliceCmd(args...)
  186. c.Process(cmd)
  187. return cmd
  188. }
  189. func (c *Client) TTL(key string) *DurationCmd {
  190. cmd := NewDurationCmd(time.Second, "TTL", key)
  191. c.Process(cmd)
  192. return cmd
  193. }
  194. func (c *Client) Type(key string) *StatusCmd {
  195. cmd := NewStatusCmd("TYPE", key)
  196. c.Process(cmd)
  197. return cmd
  198. }
  199. func (c *Client) Scan(cursor int64, match string, count int64) *ScanCmd {
  200. args := []string{"SCAN", strconv.FormatInt(cursor, 10)}
  201. if match != "" {
  202. args = append(args, "MATCH", match)
  203. }
  204. if count > 0 {
  205. args = append(args, "COUNT", strconv.FormatInt(count, 10))
  206. }
  207. cmd := NewScanCmd(args...)
  208. c.Process(cmd)
  209. return cmd
  210. }
  211. func (c *Client) SScan(key string, cursor int64, match string, count int64) *ScanCmd {
  212. args := []string{"SSCAN", key, strconv.FormatInt(cursor, 10)}
  213. if match != "" {
  214. args = append(args, "MATCH", match)
  215. }
  216. if count > 0 {
  217. args = append(args, "COUNT", strconv.FormatInt(count, 10))
  218. }
  219. cmd := NewScanCmd(args...)
  220. c.Process(cmd)
  221. return cmd
  222. }
  223. func (c *Client) HScan(key string, cursor int64, match string, count int64) *ScanCmd {
  224. args := []string{"HSCAN", key, strconv.FormatInt(cursor, 10)}
  225. if match != "" {
  226. args = append(args, "MATCH", match)
  227. }
  228. if count > 0 {
  229. args = append(args, "COUNT", strconv.FormatInt(count, 10))
  230. }
  231. cmd := NewScanCmd(args...)
  232. c.Process(cmd)
  233. return cmd
  234. }
  235. func (c *Client) ZScan(key string, cursor int64, match string, count int64) *ScanCmd {
  236. args := []string{"ZSCAN", key, strconv.FormatInt(cursor, 10)}
  237. if match != "" {
  238. args = append(args, "MATCH", match)
  239. }
  240. if count > 0 {
  241. args = append(args, "COUNT", strconv.FormatInt(count, 10))
  242. }
  243. cmd := NewScanCmd(args...)
  244. c.Process(cmd)
  245. return cmd
  246. }
  247. //------------------------------------------------------------------------------
  248. func (c *Client) Append(key, value string) *IntCmd {
  249. cmd := NewIntCmd("APPEND", key, value)
  250. c.Process(cmd)
  251. return cmd
  252. }
  253. type BitCount struct {
  254. Start, End int64
  255. }
  256. func (c *Client) BitCount(key string, bitCount *BitCount) *IntCmd {
  257. args := []string{"BITCOUNT", key}
  258. if bitCount != nil {
  259. args = append(
  260. args,
  261. strconv.FormatInt(bitCount.Start, 10),
  262. strconv.FormatInt(bitCount.End, 10),
  263. )
  264. }
  265. cmd := NewIntCmd(args...)
  266. c.Process(cmd)
  267. return cmd
  268. }
  269. func (c *Client) bitOp(op, destKey string, keys ...string) *IntCmd {
  270. args := []string{"BITOP", op, destKey}
  271. args = append(args, keys...)
  272. cmd := NewIntCmd(args...)
  273. c.Process(cmd)
  274. return cmd
  275. }
  276. func (c *Client) BitOpAnd(destKey string, keys ...string) *IntCmd {
  277. return c.bitOp("AND", destKey, keys...)
  278. }
  279. func (c *Client) BitOpOr(destKey string, keys ...string) *IntCmd {
  280. return c.bitOp("OR", destKey, keys...)
  281. }
  282. func (c *Client) BitOpXor(destKey string, keys ...string) *IntCmd {
  283. return c.bitOp("XOR", destKey, keys...)
  284. }
  285. func (c *Client) BitOpNot(destKey string, key string) *IntCmd {
  286. return c.bitOp("NOT", destKey, key)
  287. }
  288. func (c *Client) Decr(key string) *IntCmd {
  289. cmd := NewIntCmd("DECR", key)
  290. c.Process(cmd)
  291. return cmd
  292. }
  293. func (c *Client) DecrBy(key string, decrement int64) *IntCmd {
  294. cmd := NewIntCmd("DECRBY", key, strconv.FormatInt(decrement, 10))
  295. c.Process(cmd)
  296. return cmd
  297. }
  298. func (c *Client) Get(key string) *StringCmd {
  299. cmd := NewStringCmd("GET", key)
  300. c.Process(cmd)
  301. return cmd
  302. }
  303. func (c *Client) GetBit(key string, offset int64) *IntCmd {
  304. cmd := NewIntCmd("GETBIT", key, strconv.FormatInt(offset, 10))
  305. c.Process(cmd)
  306. return cmd
  307. }
  308. func (c *Client) GetRange(key string, start, end int64) *StringCmd {
  309. cmd := NewStringCmd(
  310. "GETRANGE",
  311. key,
  312. strconv.FormatInt(start, 10),
  313. strconv.FormatInt(end, 10),
  314. )
  315. c.Process(cmd)
  316. return cmd
  317. }
  318. func (c *Client) GetSet(key, value string) *StringCmd {
  319. cmd := NewStringCmd("GETSET", key, value)
  320. c.Process(cmd)
  321. return cmd
  322. }
  323. func (c *Client) Incr(key string) *IntCmd {
  324. cmd := NewIntCmd("INCR", key)
  325. c.Process(cmd)
  326. return cmd
  327. }
  328. func (c *Client) IncrBy(key string, value int64) *IntCmd {
  329. cmd := NewIntCmd("INCRBY", key, strconv.FormatInt(value, 10))
  330. c.Process(cmd)
  331. return cmd
  332. }
  333. func (c *Client) IncrByFloat(key string, value float64) *FloatCmd {
  334. cmd := NewFloatCmd("INCRBYFLOAT", key, formatFloat(value))
  335. c.Process(cmd)
  336. return cmd
  337. }
  338. func (c *Client) MGet(keys ...string) *SliceCmd {
  339. args := append([]string{"MGET"}, keys...)
  340. cmd := NewSliceCmd(args...)
  341. c.Process(cmd)
  342. return cmd
  343. }
  344. func (c *Client) MSet(pairs ...string) *StatusCmd {
  345. args := append([]string{"MSET"}, pairs...)
  346. cmd := NewStatusCmd(args...)
  347. c.Process(cmd)
  348. return cmd
  349. }
  350. func (c *Client) MSetNX(pairs ...string) *BoolCmd {
  351. args := append([]string{"MSETNX"}, pairs...)
  352. cmd := NewBoolCmd(args...)
  353. c.Process(cmd)
  354. return cmd
  355. }
  356. func (c *Client) PSetEx(key string, dur time.Duration, value string) *StatusCmd {
  357. cmd := NewStatusCmd(
  358. "PSETEX",
  359. key,
  360. strconv.FormatInt(int64(dur/time.Millisecond), 10),
  361. value,
  362. )
  363. c.Process(cmd)
  364. return cmd
  365. }
  366. func (c *Client) Set(key, value string) *StatusCmd {
  367. cmd := NewStatusCmd("SET", key, value)
  368. c.Process(cmd)
  369. return cmd
  370. }
  371. func (c *Client) SetBit(key string, offset int64, value int) *IntCmd {
  372. cmd := NewIntCmd(
  373. "SETBIT",
  374. key,
  375. strconv.FormatInt(offset, 10),
  376. strconv.FormatInt(int64(value), 10),
  377. )
  378. c.Process(cmd)
  379. return cmd
  380. }
  381. func (c *Client) SetEx(key string, dur time.Duration, value string) *StatusCmd {
  382. cmd := NewStatusCmd("SETEX", key, strconv.FormatInt(int64(dur/time.Second), 10), value)
  383. c.Process(cmd)
  384. return cmd
  385. }
  386. func (c *Client) SetNX(key, value string) *BoolCmd {
  387. cmd := NewBoolCmd("SETNX", key, value)
  388. c.Process(cmd)
  389. return cmd
  390. }
  391. func (c *Client) SetRange(key string, offset int64, value string) *IntCmd {
  392. cmd := NewIntCmd("SETRANGE", key, strconv.FormatInt(offset, 10), value)
  393. c.Process(cmd)
  394. return cmd
  395. }
  396. func (c *Client) StrLen(key string) *IntCmd {
  397. cmd := NewIntCmd("STRLEN", key)
  398. c.Process(cmd)
  399. return cmd
  400. }
  401. //------------------------------------------------------------------------------
  402. func (c *Client) HDel(key string, fields ...string) *IntCmd {
  403. args := append([]string{"HDEL", key}, fields...)
  404. cmd := NewIntCmd(args...)
  405. c.Process(cmd)
  406. return cmd
  407. }
  408. func (c *Client) HExists(key, field string) *BoolCmd {
  409. cmd := NewBoolCmd("HEXISTS", key, field)
  410. c.Process(cmd)
  411. return cmd
  412. }
  413. func (c *Client) HGet(key, field string) *StringCmd {
  414. cmd := NewStringCmd("HGET", key, field)
  415. c.Process(cmd)
  416. return cmd
  417. }
  418. func (c *Client) HGetAll(key string) *StringSliceCmd {
  419. cmd := NewStringSliceCmd("HGETALL", key)
  420. c.Process(cmd)
  421. return cmd
  422. }
  423. func (c *Client) HGetAllMap(key string) *StringStringMapCmd {
  424. cmd := NewStringStringMapCmd("HGETALL", key)
  425. c.Process(cmd)
  426. return cmd
  427. }
  428. func (c *Client) HIncrBy(key, field string, incr int64) *IntCmd {
  429. cmd := NewIntCmd("HINCRBY", key, field, strconv.FormatInt(incr, 10))
  430. c.Process(cmd)
  431. return cmd
  432. }
  433. func (c *Client) HIncrByFloat(key, field string, incr float64) *FloatCmd {
  434. cmd := NewFloatCmd("HINCRBYFLOAT", key, field, formatFloat(incr))
  435. c.Process(cmd)
  436. return cmd
  437. }
  438. func (c *Client) HKeys(key string) *StringSliceCmd {
  439. cmd := NewStringSliceCmd("HKEYS", key)
  440. c.Process(cmd)
  441. return cmd
  442. }
  443. func (c *Client) HLen(key string) *IntCmd {
  444. cmd := NewIntCmd("HLEN", key)
  445. c.Process(cmd)
  446. return cmd
  447. }
  448. func (c *Client) HMGet(key string, fields ...string) *SliceCmd {
  449. args := append([]string{"HMGET", key}, fields...)
  450. cmd := NewSliceCmd(args...)
  451. c.Process(cmd)
  452. return cmd
  453. }
  454. func (c *Client) HMSet(key, field, value string, pairs ...string) *StatusCmd {
  455. args := append([]string{"HMSET", key, field, value}, pairs...)
  456. cmd := NewStatusCmd(args...)
  457. c.Process(cmd)
  458. return cmd
  459. }
  460. func (c *Client) HSet(key, field, value string) *BoolCmd {
  461. cmd := NewBoolCmd("HSET", key, field, value)
  462. c.Process(cmd)
  463. return cmd
  464. }
  465. func (c *Client) HSetNX(key, field, value string) *BoolCmd {
  466. cmd := NewBoolCmd("HSETNX", key, field, value)
  467. c.Process(cmd)
  468. return cmd
  469. }
  470. func (c *Client) HVals(key string) *StringSliceCmd {
  471. cmd := NewStringSliceCmd("HVALS", key)
  472. c.Process(cmd)
  473. return cmd
  474. }
  475. //------------------------------------------------------------------------------
  476. func (c *Client) BLPop(timeout int64, keys ...string) *StringSliceCmd {
  477. args := append([]string{"BLPOP"}, keys...)
  478. args = append(args, strconv.FormatInt(timeout, 10))
  479. cmd := NewStringSliceCmd(args...)
  480. cmd.setReadTimeout(readTimeout(timeout))
  481. c.Process(cmd)
  482. return cmd
  483. }
  484. func (c *Client) BRPop(timeout int64, keys ...string) *StringSliceCmd {
  485. args := append([]string{"BRPOP"}, keys...)
  486. args = append(args, strconv.FormatInt(timeout, 10))
  487. cmd := NewStringSliceCmd(args...)
  488. cmd.setReadTimeout(readTimeout(timeout))
  489. c.Process(cmd)
  490. return cmd
  491. }
  492. func (c *Client) BRPopLPush(source, destination string, timeout int64) *StringCmd {
  493. cmd := NewStringCmd(
  494. "BRPOPLPUSH",
  495. source,
  496. destination,
  497. strconv.FormatInt(timeout, 10),
  498. )
  499. cmd.setReadTimeout(readTimeout(timeout))
  500. c.Process(cmd)
  501. return cmd
  502. }
  503. func (c *Client) LIndex(key string, index int64) *StringCmd {
  504. cmd := NewStringCmd("LINDEX", key, strconv.FormatInt(index, 10))
  505. c.Process(cmd)
  506. return cmd
  507. }
  508. func (c *Client) LInsert(key, op, pivot, value string) *IntCmd {
  509. cmd := NewIntCmd("LINSERT", key, op, pivot, value)
  510. c.Process(cmd)
  511. return cmd
  512. }
  513. func (c *Client) LLen(key string) *IntCmd {
  514. cmd := NewIntCmd("LLEN", key)
  515. c.Process(cmd)
  516. return cmd
  517. }
  518. func (c *Client) LPop(key string) *StringCmd {
  519. cmd := NewStringCmd("LPOP", key)
  520. c.Process(cmd)
  521. return cmd
  522. }
  523. func (c *Client) LPush(key string, values ...string) *IntCmd {
  524. args := append([]string{"LPUSH", key}, values...)
  525. cmd := NewIntCmd(args...)
  526. c.Process(cmd)
  527. return cmd
  528. }
  529. func (c *Client) LPushX(key, value string) *IntCmd {
  530. cmd := NewIntCmd("LPUSHX", key, value)
  531. c.Process(cmd)
  532. return cmd
  533. }
  534. func (c *Client) LRange(key string, start, stop int64) *StringSliceCmd {
  535. cmd := NewStringSliceCmd(
  536. "LRANGE",
  537. key,
  538. strconv.FormatInt(start, 10),
  539. strconv.FormatInt(stop, 10),
  540. )
  541. c.Process(cmd)
  542. return cmd
  543. }
  544. func (c *Client) LRem(key string, count int64, value string) *IntCmd {
  545. cmd := NewIntCmd("LREM", key, strconv.FormatInt(count, 10), value)
  546. c.Process(cmd)
  547. return cmd
  548. }
  549. func (c *Client) LSet(key string, index int64, value string) *StatusCmd {
  550. cmd := NewStatusCmd("LSET", key, strconv.FormatInt(index, 10), value)
  551. c.Process(cmd)
  552. return cmd
  553. }
  554. func (c *Client) LTrim(key string, start, stop int64) *StatusCmd {
  555. cmd := NewStatusCmd(
  556. "LTRIM",
  557. key,
  558. strconv.FormatInt(start, 10),
  559. strconv.FormatInt(stop, 10),
  560. )
  561. c.Process(cmd)
  562. return cmd
  563. }
  564. func (c *Client) RPop(key string) *StringCmd {
  565. cmd := NewStringCmd("RPOP", key)
  566. c.Process(cmd)
  567. return cmd
  568. }
  569. func (c *Client) RPopLPush(source, destination string) *StringCmd {
  570. cmd := NewStringCmd("RPOPLPUSH", source, destination)
  571. c.Process(cmd)
  572. return cmd
  573. }
  574. func (c *Client) RPush(key string, values ...string) *IntCmd {
  575. args := append([]string{"RPUSH", key}, values...)
  576. cmd := NewIntCmd(args...)
  577. c.Process(cmd)
  578. return cmd
  579. }
  580. func (c *Client) RPushX(key string, value string) *IntCmd {
  581. cmd := NewIntCmd("RPUSHX", key, value)
  582. c.Process(cmd)
  583. return cmd
  584. }
  585. //------------------------------------------------------------------------------
  586. func (c *Client) SAdd(key string, members ...string) *IntCmd {
  587. args := append([]string{"SADD", key}, members...)
  588. cmd := NewIntCmd(args...)
  589. c.Process(cmd)
  590. return cmd
  591. }
  592. func (c *Client) SCard(key string) *IntCmd {
  593. cmd := NewIntCmd("SCARD", key)
  594. c.Process(cmd)
  595. return cmd
  596. }
  597. func (c *Client) SDiff(keys ...string) *StringSliceCmd {
  598. args := append([]string{"SDIFF"}, keys...)
  599. cmd := NewStringSliceCmd(args...)
  600. c.Process(cmd)
  601. return cmd
  602. }
  603. func (c *Client) SDiffStore(destination string, keys ...string) *IntCmd {
  604. args := append([]string{"SDIFFSTORE", destination}, keys...)
  605. cmd := NewIntCmd(args...)
  606. c.Process(cmd)
  607. return cmd
  608. }
  609. func (c *Client) SInter(keys ...string) *StringSliceCmd {
  610. args := append([]string{"SINTER"}, keys...)
  611. cmd := NewStringSliceCmd(args...)
  612. c.Process(cmd)
  613. return cmd
  614. }
  615. func (c *Client) SInterStore(destination string, keys ...string) *IntCmd {
  616. args := append([]string{"SINTERSTORE", destination}, keys...)
  617. cmd := NewIntCmd(args...)
  618. c.Process(cmd)
  619. return cmd
  620. }
  621. func (c *Client) SIsMember(key, member string) *BoolCmd {
  622. cmd := NewBoolCmd("SISMEMBER", key, member)
  623. c.Process(cmd)
  624. return cmd
  625. }
  626. func (c *Client) SMembers(key string) *StringSliceCmd {
  627. cmd := NewStringSliceCmd("SMEMBERS", key)
  628. c.Process(cmd)
  629. return cmd
  630. }
  631. func (c *Client) SMove(source, destination, member string) *BoolCmd {
  632. cmd := NewBoolCmd("SMOVE", source, destination, member)
  633. c.Process(cmd)
  634. return cmd
  635. }
  636. func (c *Client) SPop(key string) *StringCmd {
  637. cmd := NewStringCmd("SPOP", key)
  638. c.Process(cmd)
  639. return cmd
  640. }
  641. func (c *Client) SRandMember(key string) *StringCmd {
  642. cmd := NewStringCmd("SRANDMEMBER", key)
  643. c.Process(cmd)
  644. return cmd
  645. }
  646. func (c *Client) SRem(key string, members ...string) *IntCmd {
  647. args := append([]string{"SREM", key}, members...)
  648. cmd := NewIntCmd(args...)
  649. c.Process(cmd)
  650. return cmd
  651. }
  652. func (c *Client) SUnion(keys ...string) *StringSliceCmd {
  653. args := append([]string{"SUNION"}, keys...)
  654. cmd := NewStringSliceCmd(args...)
  655. c.Process(cmd)
  656. return cmd
  657. }
  658. func (c *Client) SUnionStore(destination string, keys ...string) *IntCmd {
  659. args := append([]string{"SUNIONSTORE", destination}, keys...)
  660. cmd := NewIntCmd(args...)
  661. c.Process(cmd)
  662. return cmd
  663. }
  664. //------------------------------------------------------------------------------
  665. type Z struct {
  666. Score float64
  667. Member string
  668. }
  669. type ZStore struct {
  670. Weights []int64
  671. Aggregate string
  672. }
  673. func (c *Client) ZAdd(key string, members ...Z) *IntCmd {
  674. args := []string{"ZADD", key}
  675. for _, m := range members {
  676. args = append(args, formatFloat(m.Score), m.Member)
  677. }
  678. cmd := NewIntCmd(args...)
  679. c.Process(cmd)
  680. return cmd
  681. }
  682. func (c *Client) ZCard(key string) *IntCmd {
  683. cmd := NewIntCmd("ZCARD", key)
  684. c.Process(cmd)
  685. return cmd
  686. }
  687. func (c *Client) ZCount(key, min, max string) *IntCmd {
  688. cmd := NewIntCmd("ZCOUNT", key, min, max)
  689. c.Process(cmd)
  690. return cmd
  691. }
  692. func (c *Client) ZIncrBy(key string, increment float64, member string) *FloatCmd {
  693. cmd := NewFloatCmd("ZINCRBY", key, formatFloat(increment), member)
  694. c.Process(cmd)
  695. return cmd
  696. }
  697. func (c *Client) ZInterStore(
  698. destination string,
  699. store ZStore,
  700. keys ...string,
  701. ) *IntCmd {
  702. args := []string{"ZINTERSTORE", destination, strconv.FormatInt(int64(len(keys)), 10)}
  703. args = append(args, keys...)
  704. if len(store.Weights) > 0 {
  705. args = append(args, "WEIGHTS")
  706. for _, weight := range store.Weights {
  707. args = append(args, strconv.FormatInt(weight, 10))
  708. }
  709. }
  710. if store.Aggregate != "" {
  711. args = append(args, "AGGREGATE", store.Aggregate)
  712. }
  713. cmd := NewIntCmd(args...)
  714. c.Process(cmd)
  715. return cmd
  716. }
  717. func (c *Client) zRange(key string, start, stop int64, withScores bool) *StringSliceCmd {
  718. args := []string{
  719. "ZRANGE",
  720. key,
  721. strconv.FormatInt(start, 10),
  722. strconv.FormatInt(stop, 10),
  723. }
  724. if withScores {
  725. args = append(args, "WITHSCORES")
  726. }
  727. cmd := NewStringSliceCmd(args...)
  728. c.Process(cmd)
  729. return cmd
  730. }
  731. func (c *Client) ZRange(key string, start, stop int64) *StringSliceCmd {
  732. return c.zRange(key, start, stop, false)
  733. }
  734. func (c *Client) ZRangeWithScores(key string, start, stop int64) *ZSliceCmd {
  735. args := []string{
  736. "ZRANGE",
  737. key,
  738. strconv.FormatInt(start, 10),
  739. strconv.FormatInt(stop, 10),
  740. "WITHSCORES",
  741. }
  742. cmd := NewZSliceCmd(args...)
  743. c.Process(cmd)
  744. return cmd
  745. }
  746. type ZRangeByScore struct {
  747. Min, Max string
  748. Offset, Count int64
  749. }
  750. func (c *Client) zRangeByScore(key string, opt ZRangeByScore, withScores bool) *StringSliceCmd {
  751. args := []string{"ZRANGEBYSCORE", key, opt.Min, opt.Max}
  752. if withScores {
  753. args = append(args, "WITHSCORES")
  754. }
  755. if opt.Offset != 0 || opt.Count != 0 {
  756. args = append(
  757. args,
  758. "LIMIT",
  759. strconv.FormatInt(opt.Offset, 10),
  760. strconv.FormatInt(opt.Count, 10),
  761. )
  762. }
  763. cmd := NewStringSliceCmd(args...)
  764. c.Process(cmd)
  765. return cmd
  766. }
  767. func (c *Client) ZRangeByScore(key string, opt ZRangeByScore) *StringSliceCmd {
  768. return c.zRangeByScore(key, opt, false)
  769. }
  770. func (c *Client) ZRangeByScoreWithScores(key string, opt ZRangeByScore) *ZSliceCmd {
  771. args := []string{"ZRANGEBYSCORE", key, opt.Min, opt.Max, "WITHSCORES"}
  772. if opt.Offset != 0 || opt.Count != 0 {
  773. args = append(
  774. args,
  775. "LIMIT",
  776. strconv.FormatInt(opt.Offset, 10),
  777. strconv.FormatInt(opt.Count, 10),
  778. )
  779. }
  780. cmd := NewZSliceCmd(args...)
  781. c.Process(cmd)
  782. return cmd
  783. }
  784. func (c *Client) ZRank(key, member string) *IntCmd {
  785. cmd := NewIntCmd("ZRANK", key, member)
  786. c.Process(cmd)
  787. return cmd
  788. }
  789. func (c *Client) ZRem(key string, members ...string) *IntCmd {
  790. args := append([]string{"ZREM", key}, members...)
  791. cmd := NewIntCmd(args...)
  792. c.Process(cmd)
  793. return cmd
  794. }
  795. func (c *Client) ZRemRangeByRank(key string, start, stop int64) *IntCmd {
  796. cmd := NewIntCmd(
  797. "ZREMRANGEBYRANK",
  798. key,
  799. strconv.FormatInt(start, 10),
  800. strconv.FormatInt(stop, 10),
  801. )
  802. c.Process(cmd)
  803. return cmd
  804. }
  805. func (c *Client) ZRemRangeByScore(key, min, max string) *IntCmd {
  806. cmd := NewIntCmd("ZREMRANGEBYSCORE", key, min, max)
  807. c.Process(cmd)
  808. return cmd
  809. }
  810. func (c *Client) zRevRange(key, start, stop string, withScores bool) *StringSliceCmd {
  811. args := []string{"ZREVRANGE", key, start, stop}
  812. if withScores {
  813. args = append(args, "WITHSCORES")
  814. }
  815. cmd := NewStringSliceCmd(args...)
  816. c.Process(cmd)
  817. return cmd
  818. }
  819. func (c *Client) ZRevRange(key, start, stop string) *StringSliceCmd {
  820. return c.zRevRange(key, start, stop, false)
  821. }
  822. func (c *Client) ZRevRangeWithScores(key, start, stop string) *ZSliceCmd {
  823. args := []string{"ZREVRANGE", key, start, stop, "WITHSCORES"}
  824. cmd := NewZSliceCmd(args...)
  825. c.Process(cmd)
  826. return cmd
  827. }
  828. func (c *Client) zRevRangeByScore(key string, opt ZRangeByScore, withScores bool) *StringSliceCmd {
  829. args := []string{"ZREVRANGEBYSCORE", key, opt.Max, opt.Min}
  830. if withScores {
  831. args = append(args, "WITHSCORES")
  832. }
  833. if opt.Offset != 0 || opt.Count != 0 {
  834. args = append(
  835. args,
  836. "LIMIT",
  837. strconv.FormatInt(opt.Offset, 10),
  838. strconv.FormatInt(opt.Count, 10),
  839. )
  840. }
  841. cmd := NewStringSliceCmd(args...)
  842. c.Process(cmd)
  843. return cmd
  844. }
  845. func (c *Client) ZRevRangeByScore(key string, opt ZRangeByScore) *StringSliceCmd {
  846. return c.zRevRangeByScore(key, opt, false)
  847. }
  848. func (c *Client) ZRevRangeByScoreWithScores(key string, opt ZRangeByScore) *ZSliceCmd {
  849. args := []string{"ZREVRANGEBYSCORE", key, opt.Max, opt.Min, "WITHSCORES"}
  850. if opt.Offset != 0 || opt.Count != 0 {
  851. args = append(
  852. args,
  853. "LIMIT",
  854. strconv.FormatInt(opt.Offset, 10),
  855. strconv.FormatInt(opt.Count, 10),
  856. )
  857. }
  858. cmd := NewZSliceCmd(args...)
  859. c.Process(cmd)
  860. return cmd
  861. }
  862. func (c *Client) ZRevRank(key, member string) *IntCmd {
  863. cmd := NewIntCmd("ZREVRANK", key, member)
  864. c.Process(cmd)
  865. return cmd
  866. }
  867. func (c *Client) ZScore(key, member string) *FloatCmd {
  868. cmd := NewFloatCmd("ZSCORE", key, member)
  869. c.Process(cmd)
  870. return cmd
  871. }
  872. func (c *Client) ZUnionStore(
  873. destination string,
  874. store ZStore,
  875. keys ...string,
  876. ) *IntCmd {
  877. args := []string{"ZUNIONSTORE", destination, strconv.FormatInt(int64(len(keys)), 10)}
  878. args = append(args, keys...)
  879. if len(store.Weights) > 0 {
  880. args = append(args, "WEIGHTS")
  881. for _, weight := range store.Weights {
  882. args = append(args, strconv.FormatInt(weight, 10))
  883. }
  884. }
  885. if store.Aggregate != "" {
  886. args = append(args, "AGGREGATE", store.Aggregate)
  887. }
  888. cmd := NewIntCmd(args...)
  889. c.Process(cmd)
  890. return cmd
  891. }
  892. //------------------------------------------------------------------------------
  893. func (c *Client) BgRewriteAOF() *StatusCmd {
  894. cmd := NewStatusCmd("BGREWRITEAOF")
  895. c.Process(cmd)
  896. return cmd
  897. }
  898. func (c *Client) BgSave() *StatusCmd {
  899. cmd := NewStatusCmd("BGSAVE")
  900. c.Process(cmd)
  901. return cmd
  902. }
  903. func (c *Client) ClientKill(ipPort string) *StatusCmd {
  904. cmd := NewStatusCmd("CLIENT", "KILL", ipPort)
  905. c.Process(cmd)
  906. return cmd
  907. }
  908. func (c *Client) ClientList() *StringCmd {
  909. cmd := NewStringCmd("CLIENT", "LIST")
  910. c.Process(cmd)
  911. return cmd
  912. }
  913. func (c *Client) ConfigGet(parameter string) *SliceCmd {
  914. cmd := NewSliceCmd("CONFIG", "GET", parameter)
  915. c.Process(cmd)
  916. return cmd
  917. }
  918. func (c *Client) ConfigResetStat() *StatusCmd {
  919. cmd := NewStatusCmd("CONFIG", "RESETSTAT")
  920. c.Process(cmd)
  921. return cmd
  922. }
  923. func (c *Client) ConfigSet(parameter, value string) *StatusCmd {
  924. cmd := NewStatusCmd("CONFIG", "SET", parameter, value)
  925. c.Process(cmd)
  926. return cmd
  927. }
  928. func (c *Client) DbSize() *IntCmd {
  929. cmd := NewIntCmd("DBSIZE")
  930. c.Process(cmd)
  931. return cmd
  932. }
  933. func (c *Client) FlushAll() *StatusCmd {
  934. cmd := NewStatusCmd("FLUSHALL")
  935. c.Process(cmd)
  936. return cmd
  937. }
  938. func (c *Client) FlushDb() *StatusCmd {
  939. cmd := NewStatusCmd("FLUSHDB")
  940. c.Process(cmd)
  941. return cmd
  942. }
  943. func (c *Client) Info() *StringCmd {
  944. cmd := NewStringCmd("INFO")
  945. c.Process(cmd)
  946. return cmd
  947. }
  948. func (c *Client) LastSave() *IntCmd {
  949. cmd := NewIntCmd("LASTSAVE")
  950. c.Process(cmd)
  951. return cmd
  952. }
  953. func (c *Client) Save() *StatusCmd {
  954. cmd := NewStatusCmd("SAVE")
  955. c.Process(cmd)
  956. return cmd
  957. }
  958. func (c *Client) shutdown(modifier string) *StatusCmd {
  959. var args []string
  960. if modifier == "" {
  961. args = []string{"SHUTDOWN"}
  962. } else {
  963. args = []string{"SHUTDOWN", modifier}
  964. }
  965. cmd := NewStatusCmd(args...)
  966. c.Process(cmd)
  967. if err := cmd.Err(); err != nil {
  968. if err == io.EOF {
  969. // Server quit as expected.
  970. cmd.err = nil
  971. }
  972. } else {
  973. // Server did not quit. String reply contains the reason.
  974. cmd.err = errorf(cmd.val)
  975. cmd.val = ""
  976. }
  977. return cmd
  978. }
  979. func (c *Client) Shutdown() *StatusCmd {
  980. return c.shutdown("")
  981. }
  982. func (c *Client) ShutdownSave() *StatusCmd {
  983. return c.shutdown("SAVE")
  984. }
  985. func (c *Client) ShutdownNoSave() *StatusCmd {
  986. return c.shutdown("NOSAVE")
  987. }
  988. func (c *Client) SlaveOf(host, port string) *StatusCmd {
  989. cmd := NewStatusCmd("SLAVEOF", host, port)
  990. c.Process(cmd)
  991. return cmd
  992. }
  993. func (c *Client) SlowLog() {
  994. panic("not implemented")
  995. }
  996. func (c *Client) Sync() {
  997. panic("not implemented")
  998. }
  999. func (c *Client) Time() *StringSliceCmd {
  1000. cmd := NewStringSliceCmd("TIME")
  1001. c.Process(cmd)
  1002. return cmd
  1003. }
  1004. //------------------------------------------------------------------------------
  1005. func (c *Client) Eval(script string, keys []string, args []string) *Cmd {
  1006. cmdArgs := []string{"EVAL", script, strconv.FormatInt(int64(len(keys)), 10)}
  1007. cmdArgs = append(cmdArgs, keys...)
  1008. cmdArgs = append(cmdArgs, args...)
  1009. cmd := NewCmd(cmdArgs...)
  1010. c.Process(cmd)
  1011. return cmd
  1012. }
  1013. func (c *Client) EvalSha(sha1 string, keys []string, args []string) *Cmd {
  1014. cmdArgs := []string{"EVALSHA", sha1, strconv.FormatInt(int64(len(keys)), 10)}
  1015. cmdArgs = append(cmdArgs, keys...)
  1016. cmdArgs = append(cmdArgs, args...)
  1017. cmd := NewCmd(cmdArgs...)
  1018. c.Process(cmd)
  1019. return cmd
  1020. }
  1021. func (c *Client) ScriptExists(scripts ...string) *BoolSliceCmd {
  1022. args := append([]string{"SCRIPT", "EXISTS"}, scripts...)
  1023. cmd := NewBoolSliceCmd(args...)
  1024. c.Process(cmd)
  1025. return cmd
  1026. }
  1027. func (c *Client) ScriptFlush() *StatusCmd {
  1028. cmd := NewStatusCmd("SCRIPT", "FLUSH")
  1029. c.Process(cmd)
  1030. return cmd
  1031. }
  1032. func (c *Client) ScriptKill() *StatusCmd {
  1033. cmd := NewStatusCmd("SCRIPT", "KILL")
  1034. c.Process(cmd)
  1035. return cmd
  1036. }
  1037. func (c *Client) ScriptLoad(script string) *StringCmd {
  1038. cmd := NewStringCmd("SCRIPT", "LOAD", script)
  1039. c.Process(cmd)
  1040. return cmd
  1041. }
  1042. //------------------------------------------------------------------------------
  1043. func (c *Client) DebugObject(key string) *StringCmd {
  1044. cmd := NewStringCmd("DEBUG", "OBJECT", key)
  1045. c.Process(cmd)
  1046. return cmd
  1047. }
  1048. //------------------------------------------------------------------------------
  1049. func (c *Client) PubSubChannels(pattern string) *StringSliceCmd {
  1050. args := []string{"PUBSUB", "CHANNELS"}
  1051. if pattern != "*" {
  1052. args = append(args, pattern)
  1053. }
  1054. cmd := NewStringSliceCmd(args...)
  1055. c.Process(cmd)
  1056. return cmd
  1057. }
  1058. func (c *Client) PubSubNumSub(channels ...string) *SliceCmd {
  1059. args := []string{"PUBSUB", "NUMSUB"}
  1060. args = append(args, channels...)
  1061. cmd := NewSliceCmd(args...)
  1062. c.Process(cmd)
  1063. return cmd
  1064. }
  1065. func (c *Client) PubSubNumPat() *IntCmd {
  1066. cmd := NewIntCmd("PUBSUB", "NUMPAT")
  1067. c.Process(cmd)
  1068. return cmd
  1069. }