commands.go 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078
  1. package redis
  2. import (
  3. "io"
  4. "strconv"
  5. "time"
  6. "gopkg.in/redis.v5/internal"
  7. )
  8. func readTimeout(timeout time.Duration) time.Duration {
  9. if timeout == 0 {
  10. return 0
  11. }
  12. return timeout + time.Second
  13. }
  14. func usePrecise(dur time.Duration) bool {
  15. return dur < time.Second || dur%time.Second != 0
  16. }
  17. func formatMs(dur time.Duration) string {
  18. if dur > 0 && dur < time.Millisecond {
  19. internal.Logf(
  20. "specified duration is %s, but minimal supported value is %s",
  21. dur, time.Millisecond,
  22. )
  23. }
  24. return strconv.FormatInt(int64(dur/time.Millisecond), 10)
  25. }
  26. func formatSec(dur time.Duration) string {
  27. if dur > 0 && dur < time.Second {
  28. internal.Logf(
  29. "specified duration is %s, but minimal supported value is %s",
  30. dur, time.Second,
  31. )
  32. }
  33. return strconv.FormatInt(int64(dur/time.Second), 10)
  34. }
  35. type Cmdable interface {
  36. Pipeline() *Pipeline
  37. Pipelined(fn func(*Pipeline) error) ([]Cmder, error)
  38. Echo(message interface{}) *StringCmd
  39. Ping() *StatusCmd
  40. Quit() *StatusCmd
  41. Del(keys ...string) *IntCmd
  42. Unlink(keys ...string) *IntCmd
  43. Dump(key string) *StringCmd
  44. Exists(key string) *BoolCmd
  45. // TODO: merge with Exists in v6
  46. ExistsMulti(keys ...string) *IntCmd
  47. Expire(key string, expiration time.Duration) *BoolCmd
  48. ExpireAt(key string, tm time.Time) *BoolCmd
  49. Keys(pattern string) *StringSliceCmd
  50. Migrate(host, port, key string, db int64, timeout time.Duration) *StatusCmd
  51. Move(key string, db int64) *BoolCmd
  52. ObjectRefCount(key string) *IntCmd
  53. ObjectEncoding(key string) *StringCmd
  54. ObjectIdleTime(key string) *DurationCmd
  55. Persist(key string) *BoolCmd
  56. PExpire(key string, expiration time.Duration) *BoolCmd
  57. PExpireAt(key string, tm time.Time) *BoolCmd
  58. PTTL(key string) *DurationCmd
  59. RandomKey() *StringCmd
  60. Rename(key, newkey string) *StatusCmd
  61. RenameNX(key, newkey string) *BoolCmd
  62. Restore(key string, ttl time.Duration, value string) *StatusCmd
  63. RestoreReplace(key string, ttl time.Duration, value string) *StatusCmd
  64. Sort(key string, sort Sort) *StringSliceCmd
  65. SortInterfaces(key string, sort Sort) *SliceCmd
  66. TTL(key string) *DurationCmd
  67. Type(key string) *StatusCmd
  68. Scan(cursor uint64, match string, count int64) *ScanCmd
  69. SScan(key string, cursor uint64, match string, count int64) *ScanCmd
  70. HScan(key string, cursor uint64, match string, count int64) *ScanCmd
  71. ZScan(key string, cursor uint64, match string, count int64) *ScanCmd
  72. Append(key, value string) *IntCmd
  73. BitCount(key string, bitCount *BitCount) *IntCmd
  74. BitOpAnd(destKey string, keys ...string) *IntCmd
  75. BitOpOr(destKey string, keys ...string) *IntCmd
  76. BitOpXor(destKey string, keys ...string) *IntCmd
  77. BitOpNot(destKey string, key string) *IntCmd
  78. BitPos(key string, bit int64, pos ...int64) *IntCmd
  79. Decr(key string) *IntCmd
  80. DecrBy(key string, decrement int64) *IntCmd
  81. Get(key string) *StringCmd
  82. GetBit(key string, offset int64) *IntCmd
  83. GetRange(key string, start, end int64) *StringCmd
  84. GetSet(key string, value interface{}) *StringCmd
  85. Incr(key string) *IntCmd
  86. IncrBy(key string, value int64) *IntCmd
  87. IncrByFloat(key string, value float64) *FloatCmd
  88. MGet(keys ...string) *SliceCmd
  89. MSet(pairs ...interface{}) *StatusCmd
  90. MSetNX(pairs ...interface{}) *BoolCmd
  91. Set(key string, value interface{}, expiration time.Duration) *StatusCmd
  92. SetBit(key string, offset int64, value int) *IntCmd
  93. SetNX(key string, value interface{}, expiration time.Duration) *BoolCmd
  94. SetXX(key string, value interface{}, expiration time.Duration) *BoolCmd
  95. SetRange(key string, offset int64, value string) *IntCmd
  96. StrLen(key string) *IntCmd
  97. HDel(key string, fields ...string) *IntCmd
  98. HExists(key, field string) *BoolCmd
  99. HGet(key, field string) *StringCmd
  100. HGetAll(key string) *StringStringMapCmd
  101. HIncrBy(key, field string, incr int64) *IntCmd
  102. HIncrByFloat(key, field string, incr float64) *FloatCmd
  103. HKeys(key string) *StringSliceCmd
  104. HLen(key string) *IntCmd
  105. HMGet(key string, fields ...string) *SliceCmd
  106. HMSet(key string, fields map[string]string) *StatusCmd
  107. HSet(key, field string, value interface{}) *BoolCmd
  108. HSetNX(key, field string, value interface{}) *BoolCmd
  109. HVals(key string) *StringSliceCmd
  110. BLPop(timeout time.Duration, keys ...string) *StringSliceCmd
  111. BRPop(timeout time.Duration, keys ...string) *StringSliceCmd
  112. BRPopLPush(source, destination string, timeout time.Duration) *StringCmd
  113. LIndex(key string, index int64) *StringCmd
  114. LInsert(key, op string, pivot, value interface{}) *IntCmd
  115. LInsertBefore(key string, pivot, value interface{}) *IntCmd
  116. LInsertAfter(key string, pivot, value interface{}) *IntCmd
  117. LLen(key string) *IntCmd
  118. LPop(key string) *StringCmd
  119. LPush(key string, values ...interface{}) *IntCmd
  120. LPushX(key string, value interface{}) *IntCmd
  121. LRange(key string, start, stop int64) *StringSliceCmd
  122. LRem(key string, count int64, value interface{}) *IntCmd
  123. LSet(key string, index int64, value interface{}) *StatusCmd
  124. LTrim(key string, start, stop int64) *StatusCmd
  125. RPop(key string) *StringCmd
  126. RPopLPush(source, destination string) *StringCmd
  127. RPush(key string, values ...interface{}) *IntCmd
  128. RPushX(key string, value interface{}) *IntCmd
  129. SAdd(key string, members ...interface{}) *IntCmd
  130. SCard(key string) *IntCmd
  131. SDiff(keys ...string) *StringSliceCmd
  132. SDiffStore(destination string, keys ...string) *IntCmd
  133. SInter(keys ...string) *StringSliceCmd
  134. SInterStore(destination string, keys ...string) *IntCmd
  135. SIsMember(key string, member interface{}) *BoolCmd
  136. SMembers(key string) *StringSliceCmd
  137. SMove(source, destination string, member interface{}) *BoolCmd
  138. SPop(key string) *StringCmd
  139. SPopN(key string, count int64) *StringSliceCmd
  140. SRandMember(key string) *StringCmd
  141. SRandMemberN(key string, count int64) *StringSliceCmd
  142. SRem(key string, members ...interface{}) *IntCmd
  143. SUnion(keys ...string) *StringSliceCmd
  144. SUnionStore(destination string, keys ...string) *IntCmd
  145. ZAdd(key string, members ...Z) *IntCmd
  146. ZAddNX(key string, members ...Z) *IntCmd
  147. ZAddXX(key string, members ...Z) *IntCmd
  148. ZAddCh(key string, members ...Z) *IntCmd
  149. ZAddNXCh(key string, members ...Z) *IntCmd
  150. ZAddXXCh(key string, members ...Z) *IntCmd
  151. ZIncr(key string, member Z) *FloatCmd
  152. ZIncrNX(key string, member Z) *FloatCmd
  153. ZIncrXX(key string, member Z) *FloatCmd
  154. ZCard(key string) *IntCmd
  155. ZCount(key, min, max string) *IntCmd
  156. ZIncrBy(key string, increment float64, member string) *FloatCmd
  157. ZInterStore(destination string, store ZStore, keys ...string) *IntCmd
  158. ZRange(key string, start, stop int64) *StringSliceCmd
  159. ZRangeWithScores(key string, start, stop int64) *ZSliceCmd
  160. ZRangeByScore(key string, opt ZRangeBy) *StringSliceCmd
  161. ZRangeByLex(key string, opt ZRangeBy) *StringSliceCmd
  162. ZRangeByScoreWithScores(key string, opt ZRangeBy) *ZSliceCmd
  163. ZRank(key, member string) *IntCmd
  164. ZRem(key string, members ...interface{}) *IntCmd
  165. ZRemRangeByRank(key string, start, stop int64) *IntCmd
  166. ZRemRangeByScore(key, min, max string) *IntCmd
  167. ZRemRangeByLex(key, min, max string) *IntCmd
  168. ZRevRange(key string, start, stop int64) *StringSliceCmd
  169. ZRevRangeWithScores(key string, start, stop int64) *ZSliceCmd
  170. ZRevRangeByScore(key string, opt ZRangeBy) *StringSliceCmd
  171. ZRevRangeByLex(key string, opt ZRangeBy) *StringSliceCmd
  172. ZRevRangeByScoreWithScores(key string, opt ZRangeBy) *ZSliceCmd
  173. ZRevRank(key, member string) *IntCmd
  174. ZScore(key, member string) *FloatCmd
  175. ZUnionStore(dest string, store ZStore, keys ...string) *IntCmd
  176. PFAdd(key string, els ...interface{}) *IntCmd
  177. PFCount(keys ...string) *IntCmd
  178. PFMerge(dest string, keys ...string) *StatusCmd
  179. BgRewriteAOF() *StatusCmd
  180. BgSave() *StatusCmd
  181. ClientKill(ipPort string) *StatusCmd
  182. ClientList() *StringCmd
  183. ClientPause(dur time.Duration) *BoolCmd
  184. ConfigGet(parameter string) *SliceCmd
  185. ConfigResetStat() *StatusCmd
  186. ConfigSet(parameter, value string) *StatusCmd
  187. DbSize() *IntCmd
  188. FlushAll() *StatusCmd
  189. FlushDb() *StatusCmd
  190. Info(section ...string) *StringCmd
  191. LastSave() *IntCmd
  192. Save() *StatusCmd
  193. Shutdown() *StatusCmd
  194. ShutdownSave() *StatusCmd
  195. ShutdownNoSave() *StatusCmd
  196. SlaveOf(host, port string) *StatusCmd
  197. Time() *TimeCmd
  198. Eval(script string, keys []string, args ...interface{}) *Cmd
  199. EvalSha(sha1 string, keys []string, args ...interface{}) *Cmd
  200. ScriptExists(scripts ...string) *BoolSliceCmd
  201. ScriptFlush() *StatusCmd
  202. ScriptKill() *StatusCmd
  203. ScriptLoad(script string) *StringCmd
  204. DebugObject(key string) *StringCmd
  205. PubSubChannels(pattern string) *StringSliceCmd
  206. PubSubNumSub(channels ...string) *StringIntMapCmd
  207. PubSubNumPat() *IntCmd
  208. ClusterSlots() *ClusterSlotsCmd
  209. ClusterNodes() *StringCmd
  210. ClusterMeet(host, port string) *StatusCmd
  211. ClusterForget(nodeID string) *StatusCmd
  212. ClusterReplicate(nodeID string) *StatusCmd
  213. ClusterResetSoft() *StatusCmd
  214. ClusterResetHard() *StatusCmd
  215. ClusterInfo() *StringCmd
  216. ClusterKeySlot(key string) *IntCmd
  217. ClusterCountFailureReports(nodeID string) *IntCmd
  218. ClusterCountKeysInSlot(slot int) *IntCmd
  219. ClusterDelSlots(slots ...int) *StatusCmd
  220. ClusterDelSlotsRange(min, max int) *StatusCmd
  221. ClusterSaveConfig() *StatusCmd
  222. ClusterSlaves(nodeID string) *StringSliceCmd
  223. ClusterFailover() *StatusCmd
  224. ClusterAddSlots(slots ...int) *StatusCmd
  225. ClusterAddSlotsRange(min, max int) *StatusCmd
  226. GeoAdd(key string, geoLocation ...*GeoLocation) *IntCmd
  227. GeoPos(key string, members ...string) *GeoPosCmd
  228. GeoRadius(key string, longitude, latitude float64, query *GeoRadiusQuery) *GeoLocationCmd
  229. GeoRadiusByMember(key, member string, query *GeoRadiusQuery) *GeoLocationCmd
  230. GeoDist(key string, member1, member2, unit string) *FloatCmd
  231. GeoHash(key string, members ...string) *StringSliceCmd
  232. Command() *CommandsInfoCmd
  233. }
  234. var _ Cmdable = (*Client)(nil)
  235. var _ Cmdable = (*Tx)(nil)
  236. var _ Cmdable = (*Ring)(nil)
  237. var _ Cmdable = (*ClusterClient)(nil)
  238. type cmdable struct {
  239. process func(cmd Cmder) error
  240. }
  241. type statefulCmdable struct {
  242. process func(cmd Cmder) error
  243. }
  244. //------------------------------------------------------------------------------
  245. func (c *statefulCmdable) Auth(password string) *StatusCmd {
  246. cmd := NewStatusCmd("auth", password)
  247. c.process(cmd)
  248. return cmd
  249. }
  250. func (c *cmdable) Echo(message interface{}) *StringCmd {
  251. cmd := NewStringCmd("echo", message)
  252. c.process(cmd)
  253. return cmd
  254. }
  255. func (c *cmdable) Ping() *StatusCmd {
  256. cmd := NewStatusCmd("ping")
  257. c.process(cmd)
  258. return cmd
  259. }
  260. func (c *cmdable) Wait(numSlaves int, timeout time.Duration) *IntCmd {
  261. cmd := NewIntCmd("wait", numSlaves, int(timeout/time.Millisecond))
  262. c.process(cmd)
  263. return cmd
  264. }
  265. func (c *cmdable) Quit() *StatusCmd {
  266. panic("not implemented")
  267. }
  268. func (c *statefulCmdable) Select(index int) *StatusCmd {
  269. cmd := NewStatusCmd("select", index)
  270. c.process(cmd)
  271. return cmd
  272. }
  273. //------------------------------------------------------------------------------
  274. func (c *cmdable) Del(keys ...string) *IntCmd {
  275. args := make([]interface{}, 1+len(keys))
  276. args[0] = "del"
  277. for i, key := range keys {
  278. args[1+i] = key
  279. }
  280. cmd := NewIntCmd(args...)
  281. c.process(cmd)
  282. return cmd
  283. }
  284. func (c *cmdable) Unlink(keys ...string) *IntCmd {
  285. args := make([]interface{}, 1+len(keys))
  286. args[0] = "unlink"
  287. for i, key := range keys {
  288. args[1+i] = key
  289. }
  290. cmd := NewIntCmd(args...)
  291. c.process(cmd)
  292. return cmd
  293. }
  294. func (c *cmdable) Dump(key string) *StringCmd {
  295. cmd := NewStringCmd("dump", key)
  296. c.process(cmd)
  297. return cmd
  298. }
  299. func (c *cmdable) Exists(key string) *BoolCmd {
  300. cmd := NewBoolCmd("exists", key)
  301. c.process(cmd)
  302. return cmd
  303. }
  304. func (c *cmdable) ExistsMulti(keys ...string) *IntCmd {
  305. args := make([]interface{}, 1+len(keys))
  306. args[0] = "exists"
  307. for i, key := range keys {
  308. args[1+i] = key
  309. }
  310. cmd := NewIntCmd(args...)
  311. c.process(cmd)
  312. return cmd
  313. }
  314. func (c *cmdable) Expire(key string, expiration time.Duration) *BoolCmd {
  315. cmd := NewBoolCmd("expire", key, formatSec(expiration))
  316. c.process(cmd)
  317. return cmd
  318. }
  319. func (c *cmdable) ExpireAt(key string, tm time.Time) *BoolCmd {
  320. cmd := NewBoolCmd("expireat", key, tm.Unix())
  321. c.process(cmd)
  322. return cmd
  323. }
  324. func (c *cmdable) Keys(pattern string) *StringSliceCmd {
  325. cmd := NewStringSliceCmd("keys", pattern)
  326. c.process(cmd)
  327. return cmd
  328. }
  329. func (c *cmdable) Migrate(host, port, key string, db int64, timeout time.Duration) *StatusCmd {
  330. cmd := NewStatusCmd(
  331. "migrate",
  332. host,
  333. port,
  334. key,
  335. db,
  336. formatMs(timeout),
  337. )
  338. cmd.setReadTimeout(readTimeout(timeout))
  339. c.process(cmd)
  340. return cmd
  341. }
  342. func (c *cmdable) Move(key string, db int64) *BoolCmd {
  343. cmd := NewBoolCmd("move", key, db)
  344. c.process(cmd)
  345. return cmd
  346. }
  347. func (c *cmdable) ObjectRefCount(key string) *IntCmd {
  348. cmd := NewIntCmd("object", "refcount", key)
  349. c.process(cmd)
  350. return cmd
  351. }
  352. func (c *cmdable) ObjectEncoding(key string) *StringCmd {
  353. cmd := NewStringCmd("object", "encoding", key)
  354. c.process(cmd)
  355. return cmd
  356. }
  357. func (c *cmdable) ObjectIdleTime(key string) *DurationCmd {
  358. cmd := NewDurationCmd(time.Second, "object", "idletime", key)
  359. c.process(cmd)
  360. return cmd
  361. }
  362. func (c *cmdable) Persist(key string) *BoolCmd {
  363. cmd := NewBoolCmd("persist", key)
  364. c.process(cmd)
  365. return cmd
  366. }
  367. func (c *cmdable) PExpire(key string, expiration time.Duration) *BoolCmd {
  368. cmd := NewBoolCmd("pexpire", key, formatMs(expiration))
  369. c.process(cmd)
  370. return cmd
  371. }
  372. func (c *cmdable) PExpireAt(key string, tm time.Time) *BoolCmd {
  373. cmd := NewBoolCmd(
  374. "pexpireat",
  375. key,
  376. tm.UnixNano()/int64(time.Millisecond),
  377. )
  378. c.process(cmd)
  379. return cmd
  380. }
  381. func (c *cmdable) PTTL(key string) *DurationCmd {
  382. cmd := NewDurationCmd(time.Millisecond, "pttl", key)
  383. c.process(cmd)
  384. return cmd
  385. }
  386. func (c *cmdable) RandomKey() *StringCmd {
  387. cmd := NewStringCmd("randomkey")
  388. c.process(cmd)
  389. return cmd
  390. }
  391. func (c *cmdable) Rename(key, newkey string) *StatusCmd {
  392. cmd := NewStatusCmd("rename", key, newkey)
  393. c.process(cmd)
  394. return cmd
  395. }
  396. func (c *cmdable) RenameNX(key, newkey string) *BoolCmd {
  397. cmd := NewBoolCmd("renamenx", key, newkey)
  398. c.process(cmd)
  399. return cmd
  400. }
  401. func (c *cmdable) Restore(key string, ttl time.Duration, value string) *StatusCmd {
  402. cmd := NewStatusCmd(
  403. "restore",
  404. key,
  405. formatMs(ttl),
  406. value,
  407. )
  408. c.process(cmd)
  409. return cmd
  410. }
  411. func (c *cmdable) RestoreReplace(key string, ttl time.Duration, value string) *StatusCmd {
  412. cmd := NewStatusCmd(
  413. "restore",
  414. key,
  415. formatMs(ttl),
  416. value,
  417. "replace",
  418. )
  419. c.process(cmd)
  420. return cmd
  421. }
  422. type Sort struct {
  423. By string
  424. Offset, Count float64
  425. Get []string
  426. Order string
  427. IsAlpha bool
  428. Store string
  429. }
  430. func (sort *Sort) args(key string) []interface{} {
  431. args := []interface{}{"sort", key}
  432. if sort.By != "" {
  433. args = append(args, "by", sort.By)
  434. }
  435. if sort.Offset != 0 || sort.Count != 0 {
  436. args = append(args, "limit", sort.Offset, sort.Count)
  437. }
  438. for _, get := range sort.Get {
  439. args = append(args, "get", get)
  440. }
  441. if sort.Order != "" {
  442. args = append(args, sort.Order)
  443. }
  444. if sort.IsAlpha {
  445. args = append(args, "alpha")
  446. }
  447. if sort.Store != "" {
  448. args = append(args, "store", sort.Store)
  449. }
  450. return args
  451. }
  452. func (c *cmdable) Sort(key string, sort Sort) *StringSliceCmd {
  453. cmd := NewStringSliceCmd(sort.args(key)...)
  454. c.process(cmd)
  455. return cmd
  456. }
  457. func (c *cmdable) SortInterfaces(key string, sort Sort) *SliceCmd {
  458. cmd := NewSliceCmd(sort.args(key)...)
  459. c.process(cmd)
  460. return cmd
  461. }
  462. func (c *cmdable) TTL(key string) *DurationCmd {
  463. cmd := NewDurationCmd(time.Second, "ttl", key)
  464. c.process(cmd)
  465. return cmd
  466. }
  467. func (c *cmdable) Type(key string) *StatusCmd {
  468. cmd := NewStatusCmd("type", key)
  469. c.process(cmd)
  470. return cmd
  471. }
  472. func (c *cmdable) Scan(cursor uint64, match string, count int64) *ScanCmd {
  473. args := []interface{}{"scan", cursor}
  474. if match != "" {
  475. args = append(args, "match", match)
  476. }
  477. if count > 0 {
  478. args = append(args, "count", count)
  479. }
  480. cmd := NewScanCmd(c.process, args...)
  481. c.process(cmd)
  482. return cmd
  483. }
  484. func (c *cmdable) SScan(key string, cursor uint64, match string, count int64) *ScanCmd {
  485. args := []interface{}{"sscan", key, cursor}
  486. if match != "" {
  487. args = append(args, "match", match)
  488. }
  489. if count > 0 {
  490. args = append(args, "count", count)
  491. }
  492. cmd := NewScanCmd(c.process, args...)
  493. c.process(cmd)
  494. return cmd
  495. }
  496. func (c *cmdable) HScan(key string, cursor uint64, match string, count int64) *ScanCmd {
  497. args := []interface{}{"hscan", key, cursor}
  498. if match != "" {
  499. args = append(args, "match", match)
  500. }
  501. if count > 0 {
  502. args = append(args, "count", count)
  503. }
  504. cmd := NewScanCmd(c.process, args...)
  505. c.process(cmd)
  506. return cmd
  507. }
  508. func (c *cmdable) ZScan(key string, cursor uint64, match string, count int64) *ScanCmd {
  509. args := []interface{}{"zscan", key, cursor}
  510. if match != "" {
  511. args = append(args, "match", match)
  512. }
  513. if count > 0 {
  514. args = append(args, "count", count)
  515. }
  516. cmd := NewScanCmd(c.process, args...)
  517. c.process(cmd)
  518. return cmd
  519. }
  520. //------------------------------------------------------------------------------
  521. func (c *cmdable) Append(key, value string) *IntCmd {
  522. cmd := NewIntCmd("append", key, value)
  523. c.process(cmd)
  524. return cmd
  525. }
  526. type BitCount struct {
  527. Start, End int64
  528. }
  529. func (c *cmdable) BitCount(key string, bitCount *BitCount) *IntCmd {
  530. args := []interface{}{"bitcount", key}
  531. if bitCount != nil {
  532. args = append(
  533. args,
  534. bitCount.Start,
  535. bitCount.End,
  536. )
  537. }
  538. cmd := NewIntCmd(args...)
  539. c.process(cmd)
  540. return cmd
  541. }
  542. func (c *cmdable) bitOp(op, destKey string, keys ...string) *IntCmd {
  543. args := make([]interface{}, 3+len(keys))
  544. args[0] = "bitop"
  545. args[1] = op
  546. args[2] = destKey
  547. for i, key := range keys {
  548. args[3+i] = key
  549. }
  550. cmd := NewIntCmd(args...)
  551. c.process(cmd)
  552. return cmd
  553. }
  554. func (c *cmdable) BitOpAnd(destKey string, keys ...string) *IntCmd {
  555. return c.bitOp("and", destKey, keys...)
  556. }
  557. func (c *cmdable) BitOpOr(destKey string, keys ...string) *IntCmd {
  558. return c.bitOp("or", destKey, keys...)
  559. }
  560. func (c *cmdable) BitOpXor(destKey string, keys ...string) *IntCmd {
  561. return c.bitOp("xor", destKey, keys...)
  562. }
  563. func (c *cmdable) BitOpNot(destKey string, key string) *IntCmd {
  564. return c.bitOp("not", destKey, key)
  565. }
  566. func (c *cmdable) BitPos(key string, bit int64, pos ...int64) *IntCmd {
  567. args := make([]interface{}, 3+len(pos))
  568. args[0] = "bitpos"
  569. args[1] = key
  570. args[2] = bit
  571. switch len(pos) {
  572. case 0:
  573. case 1:
  574. args[3] = pos[0]
  575. case 2:
  576. args[3] = pos[0]
  577. args[4] = pos[1]
  578. default:
  579. panic("too many arguments")
  580. }
  581. cmd := NewIntCmd(args...)
  582. c.process(cmd)
  583. return cmd
  584. }
  585. func (c *cmdable) Decr(key string) *IntCmd {
  586. cmd := NewIntCmd("decr", key)
  587. c.process(cmd)
  588. return cmd
  589. }
  590. func (c *cmdable) DecrBy(key string, decrement int64) *IntCmd {
  591. cmd := NewIntCmd("decrby", key, decrement)
  592. c.process(cmd)
  593. return cmd
  594. }
  595. func (c *cmdable) Get(key string) *StringCmd {
  596. cmd := NewStringCmd("get", key)
  597. c.process(cmd)
  598. return cmd
  599. }
  600. func (c *cmdable) GetBit(key string, offset int64) *IntCmd {
  601. cmd := NewIntCmd("getbit", key, offset)
  602. c.process(cmd)
  603. return cmd
  604. }
  605. func (c *cmdable) GetRange(key string, start, end int64) *StringCmd {
  606. cmd := NewStringCmd("getrange", key, start, end)
  607. c.process(cmd)
  608. return cmd
  609. }
  610. func (c *cmdable) GetSet(key string, value interface{}) *StringCmd {
  611. cmd := NewStringCmd("getset", key, value)
  612. c.process(cmd)
  613. return cmd
  614. }
  615. func (c *cmdable) Incr(key string) *IntCmd {
  616. cmd := NewIntCmd("incr", key)
  617. c.process(cmd)
  618. return cmd
  619. }
  620. func (c *cmdable) IncrBy(key string, value int64) *IntCmd {
  621. cmd := NewIntCmd("incrby", key, value)
  622. c.process(cmd)
  623. return cmd
  624. }
  625. func (c *cmdable) IncrByFloat(key string, value float64) *FloatCmd {
  626. cmd := NewFloatCmd("incrbyfloat", key, value)
  627. c.process(cmd)
  628. return cmd
  629. }
  630. func (c *cmdable) MGet(keys ...string) *SliceCmd {
  631. args := make([]interface{}, 1+len(keys))
  632. args[0] = "mget"
  633. for i, key := range keys {
  634. args[1+i] = key
  635. }
  636. cmd := NewSliceCmd(args...)
  637. c.process(cmd)
  638. return cmd
  639. }
  640. func (c *cmdable) MSet(pairs ...interface{}) *StatusCmd {
  641. args := make([]interface{}, 1+len(pairs))
  642. args[0] = "mset"
  643. for i, pair := range pairs {
  644. args[1+i] = pair
  645. }
  646. cmd := NewStatusCmd(args...)
  647. c.process(cmd)
  648. return cmd
  649. }
  650. func (c *cmdable) MSetNX(pairs ...interface{}) *BoolCmd {
  651. args := make([]interface{}, 1+len(pairs))
  652. args[0] = "msetnx"
  653. for i, pair := range pairs {
  654. args[1+i] = pair
  655. }
  656. cmd := NewBoolCmd(args...)
  657. c.process(cmd)
  658. return cmd
  659. }
  660. // Redis `SET key value [expiration]` command.
  661. //
  662. // Use expiration for `SETEX`-like behavior.
  663. // Zero expiration means the key has no expiration time.
  664. func (c *cmdable) Set(key string, value interface{}, expiration time.Duration) *StatusCmd {
  665. args := make([]interface{}, 3, 4)
  666. args[0] = "set"
  667. args[1] = key
  668. args[2] = value
  669. if expiration > 0 {
  670. if usePrecise(expiration) {
  671. args = append(args, "px", formatMs(expiration))
  672. } else {
  673. args = append(args, "ex", formatSec(expiration))
  674. }
  675. }
  676. cmd := NewStatusCmd(args...)
  677. c.process(cmd)
  678. return cmd
  679. }
  680. func (c *cmdable) SetBit(key string, offset int64, value int) *IntCmd {
  681. cmd := NewIntCmd(
  682. "setbit",
  683. key,
  684. offset,
  685. value,
  686. )
  687. c.process(cmd)
  688. return cmd
  689. }
  690. // Redis `SET key value [expiration] NX` command.
  691. //
  692. // Zero expiration means the key has no expiration time.
  693. func (c *cmdable) SetNX(key string, value interface{}, expiration time.Duration) *BoolCmd {
  694. var cmd *BoolCmd
  695. if expiration == 0 {
  696. // Use old `SETNX` to support old Redis versions.
  697. cmd = NewBoolCmd("setnx", key, value)
  698. } else {
  699. if usePrecise(expiration) {
  700. cmd = NewBoolCmd("set", key, value, "px", formatMs(expiration), "nx")
  701. } else {
  702. cmd = NewBoolCmd("set", key, value, "ex", formatSec(expiration), "nx")
  703. }
  704. }
  705. c.process(cmd)
  706. return cmd
  707. }
  708. // Redis `SET key value [expiration] XX` command.
  709. //
  710. // Zero expiration means the key has no expiration time.
  711. func (c *cmdable) SetXX(key string, value interface{}, expiration time.Duration) *BoolCmd {
  712. var cmd *BoolCmd
  713. if expiration == 0 {
  714. cmd = NewBoolCmd("set", key, value, "xx")
  715. } else {
  716. if usePrecise(expiration) {
  717. cmd = NewBoolCmd("set", key, value, "px", formatMs(expiration), "xx")
  718. } else {
  719. cmd = NewBoolCmd("set", key, value, "ex", formatSec(expiration), "xx")
  720. }
  721. }
  722. c.process(cmd)
  723. return cmd
  724. }
  725. func (c *cmdable) SetRange(key string, offset int64, value string) *IntCmd {
  726. cmd := NewIntCmd("setrange", key, offset, value)
  727. c.process(cmd)
  728. return cmd
  729. }
  730. func (c *cmdable) StrLen(key string) *IntCmd {
  731. cmd := NewIntCmd("strlen", key)
  732. c.process(cmd)
  733. return cmd
  734. }
  735. //------------------------------------------------------------------------------
  736. func (c *cmdable) HDel(key string, fields ...string) *IntCmd {
  737. args := make([]interface{}, 2+len(fields))
  738. args[0] = "hdel"
  739. args[1] = key
  740. for i, field := range fields {
  741. args[2+i] = field
  742. }
  743. cmd := NewIntCmd(args...)
  744. c.process(cmd)
  745. return cmd
  746. }
  747. func (c *cmdable) HExists(key, field string) *BoolCmd {
  748. cmd := NewBoolCmd("hexists", key, field)
  749. c.process(cmd)
  750. return cmd
  751. }
  752. func (c *cmdable) HGet(key, field string) *StringCmd {
  753. cmd := NewStringCmd("hget", key, field)
  754. c.process(cmd)
  755. return cmd
  756. }
  757. func (c *cmdable) HGetAll(key string) *StringStringMapCmd {
  758. cmd := NewStringStringMapCmd("hgetall", key)
  759. c.process(cmd)
  760. return cmd
  761. }
  762. func (c *cmdable) HIncrBy(key, field string, incr int64) *IntCmd {
  763. cmd := NewIntCmd("hincrby", key, field, incr)
  764. c.process(cmd)
  765. return cmd
  766. }
  767. func (c *cmdable) HIncrByFloat(key, field string, incr float64) *FloatCmd {
  768. cmd := NewFloatCmd("hincrbyfloat", key, field, incr)
  769. c.process(cmd)
  770. return cmd
  771. }
  772. func (c *cmdable) HKeys(key string) *StringSliceCmd {
  773. cmd := NewStringSliceCmd("hkeys", key)
  774. c.process(cmd)
  775. return cmd
  776. }
  777. func (c *cmdable) HLen(key string) *IntCmd {
  778. cmd := NewIntCmd("hlen", key)
  779. c.process(cmd)
  780. return cmd
  781. }
  782. func (c *cmdable) HMGet(key string, fields ...string) *SliceCmd {
  783. args := make([]interface{}, 2+len(fields))
  784. args[0] = "hmget"
  785. args[1] = key
  786. for i, field := range fields {
  787. args[2+i] = field
  788. }
  789. cmd := NewSliceCmd(args...)
  790. c.process(cmd)
  791. return cmd
  792. }
  793. func (c *cmdable) HMSet(key string, fields map[string]string) *StatusCmd {
  794. args := make([]interface{}, 2+len(fields)*2)
  795. args[0] = "hmset"
  796. args[1] = key
  797. i := 2
  798. for k, v := range fields {
  799. args[i] = k
  800. args[i+1] = v
  801. i += 2
  802. }
  803. cmd := NewStatusCmd(args...)
  804. c.process(cmd)
  805. return cmd
  806. }
  807. func (c *cmdable) HSet(key, field string, value interface{}) *BoolCmd {
  808. cmd := NewBoolCmd("hset", key, field, value)
  809. c.process(cmd)
  810. return cmd
  811. }
  812. func (c *cmdable) HSetNX(key, field string, value interface{}) *BoolCmd {
  813. cmd := NewBoolCmd("hsetnx", key, field, value)
  814. c.process(cmd)
  815. return cmd
  816. }
  817. func (c *cmdable) HVals(key string) *StringSliceCmd {
  818. cmd := NewStringSliceCmd("hvals", key)
  819. c.process(cmd)
  820. return cmd
  821. }
  822. //------------------------------------------------------------------------------
  823. func (c *cmdable) BLPop(timeout time.Duration, keys ...string) *StringSliceCmd {
  824. args := make([]interface{}, 1+len(keys)+1)
  825. args[0] = "blpop"
  826. for i, key := range keys {
  827. args[1+i] = key
  828. }
  829. args[len(args)-1] = formatSec(timeout)
  830. cmd := NewStringSliceCmd(args...)
  831. cmd.setReadTimeout(readTimeout(timeout))
  832. c.process(cmd)
  833. return cmd
  834. }
  835. func (c *cmdable) BRPop(timeout time.Duration, keys ...string) *StringSliceCmd {
  836. args := make([]interface{}, 1+len(keys)+1)
  837. args[0] = "brpop"
  838. for i, key := range keys {
  839. args[1+i] = key
  840. }
  841. args[len(keys)+1] = formatSec(timeout)
  842. cmd := NewStringSliceCmd(args...)
  843. cmd.setReadTimeout(readTimeout(timeout))
  844. c.process(cmd)
  845. return cmd
  846. }
  847. func (c *cmdable) BRPopLPush(source, destination string, timeout time.Duration) *StringCmd {
  848. cmd := NewStringCmd(
  849. "brpoplpush",
  850. source,
  851. destination,
  852. formatSec(timeout),
  853. )
  854. cmd.setReadTimeout(readTimeout(timeout))
  855. c.process(cmd)
  856. return cmd
  857. }
  858. func (c *cmdable) LIndex(key string, index int64) *StringCmd {
  859. cmd := NewStringCmd("lindex", key, index)
  860. c.process(cmd)
  861. return cmd
  862. }
  863. func (c *cmdable) LInsert(key, op string, pivot, value interface{}) *IntCmd {
  864. cmd := NewIntCmd("linsert", key, op, pivot, value)
  865. c.process(cmd)
  866. return cmd
  867. }
  868. func (c *cmdable) LInsertBefore(key string, pivot, value interface{}) *IntCmd {
  869. cmd := NewIntCmd("linsert", key, "before", pivot, value)
  870. c.process(cmd)
  871. return cmd
  872. }
  873. func (c *cmdable) LInsertAfter(key string, pivot, value interface{}) *IntCmd {
  874. cmd := NewIntCmd("linsert", key, "after", pivot, value)
  875. c.process(cmd)
  876. return cmd
  877. }
  878. func (c *cmdable) LLen(key string) *IntCmd {
  879. cmd := NewIntCmd("llen", key)
  880. c.process(cmd)
  881. return cmd
  882. }
  883. func (c *cmdable) LPop(key string) *StringCmd {
  884. cmd := NewStringCmd("lpop", key)
  885. c.process(cmd)
  886. return cmd
  887. }
  888. func (c *cmdable) LPush(key string, values ...interface{}) *IntCmd {
  889. args := make([]interface{}, 2+len(values))
  890. args[0] = "lpush"
  891. args[1] = key
  892. for i, value := range values {
  893. args[2+i] = value
  894. }
  895. cmd := NewIntCmd(args...)
  896. c.process(cmd)
  897. return cmd
  898. }
  899. func (c *cmdable) LPushX(key string, value interface{}) *IntCmd {
  900. cmd := NewIntCmd("lpushx", key, value)
  901. c.process(cmd)
  902. return cmd
  903. }
  904. func (c *cmdable) LRange(key string, start, stop int64) *StringSliceCmd {
  905. cmd := NewStringSliceCmd(
  906. "lrange",
  907. key,
  908. start,
  909. stop,
  910. )
  911. c.process(cmd)
  912. return cmd
  913. }
  914. func (c *cmdable) LRem(key string, count int64, value interface{}) *IntCmd {
  915. cmd := NewIntCmd("lrem", key, count, value)
  916. c.process(cmd)
  917. return cmd
  918. }
  919. func (c *cmdable) LSet(key string, index int64, value interface{}) *StatusCmd {
  920. cmd := NewStatusCmd("lset", key, index, value)
  921. c.process(cmd)
  922. return cmd
  923. }
  924. func (c *cmdable) LTrim(key string, start, stop int64) *StatusCmd {
  925. cmd := NewStatusCmd(
  926. "ltrim",
  927. key,
  928. start,
  929. stop,
  930. )
  931. c.process(cmd)
  932. return cmd
  933. }
  934. func (c *cmdable) RPop(key string) *StringCmd {
  935. cmd := NewStringCmd("rpop", key)
  936. c.process(cmd)
  937. return cmd
  938. }
  939. func (c *cmdable) RPopLPush(source, destination string) *StringCmd {
  940. cmd := NewStringCmd("rpoplpush", source, destination)
  941. c.process(cmd)
  942. return cmd
  943. }
  944. func (c *cmdable) RPush(key string, values ...interface{}) *IntCmd {
  945. args := make([]interface{}, 2+len(values))
  946. args[0] = "rpush"
  947. args[1] = key
  948. for i, value := range values {
  949. args[2+i] = value
  950. }
  951. cmd := NewIntCmd(args...)
  952. c.process(cmd)
  953. return cmd
  954. }
  955. func (c *cmdable) RPushX(key string, value interface{}) *IntCmd {
  956. cmd := NewIntCmd("rpushx", key, value)
  957. c.process(cmd)
  958. return cmd
  959. }
  960. //------------------------------------------------------------------------------
  961. func (c *cmdable) SAdd(key string, members ...interface{}) *IntCmd {
  962. args := make([]interface{}, 2+len(members))
  963. args[0] = "sadd"
  964. args[1] = key
  965. for i, member := range members {
  966. args[2+i] = member
  967. }
  968. cmd := NewIntCmd(args...)
  969. c.process(cmd)
  970. return cmd
  971. }
  972. func (c *cmdable) SCard(key string) *IntCmd {
  973. cmd := NewIntCmd("scard", key)
  974. c.process(cmd)
  975. return cmd
  976. }
  977. func (c *cmdable) SDiff(keys ...string) *StringSliceCmd {
  978. args := make([]interface{}, 1+len(keys))
  979. args[0] = "sdiff"
  980. for i, key := range keys {
  981. args[1+i] = key
  982. }
  983. cmd := NewStringSliceCmd(args...)
  984. c.process(cmd)
  985. return cmd
  986. }
  987. func (c *cmdable) SDiffStore(destination string, keys ...string) *IntCmd {
  988. args := make([]interface{}, 2+len(keys))
  989. args[0] = "sdiffstore"
  990. args[1] = destination
  991. for i, key := range keys {
  992. args[2+i] = key
  993. }
  994. cmd := NewIntCmd(args...)
  995. c.process(cmd)
  996. return cmd
  997. }
  998. func (c *cmdable) SInter(keys ...string) *StringSliceCmd {
  999. args := make([]interface{}, 1+len(keys))
  1000. args[0] = "sinter"
  1001. for i, key := range keys {
  1002. args[1+i] = key
  1003. }
  1004. cmd := NewStringSliceCmd(args...)
  1005. c.process(cmd)
  1006. return cmd
  1007. }
  1008. func (c *cmdable) SInterStore(destination string, keys ...string) *IntCmd {
  1009. args := make([]interface{}, 2+len(keys))
  1010. args[0] = "sinterstore"
  1011. args[1] = destination
  1012. for i, key := range keys {
  1013. args[2+i] = key
  1014. }
  1015. cmd := NewIntCmd(args...)
  1016. c.process(cmd)
  1017. return cmd
  1018. }
  1019. func (c *cmdable) SIsMember(key string, member interface{}) *BoolCmd {
  1020. cmd := NewBoolCmd("sismember", key, member)
  1021. c.process(cmd)
  1022. return cmd
  1023. }
  1024. func (c *cmdable) SMembers(key string) *StringSliceCmd {
  1025. cmd := NewStringSliceCmd("smembers", key)
  1026. c.process(cmd)
  1027. return cmd
  1028. }
  1029. func (c *cmdable) SMove(source, destination string, member interface{}) *BoolCmd {
  1030. cmd := NewBoolCmd("smove", source, destination, member)
  1031. c.process(cmd)
  1032. return cmd
  1033. }
  1034. // Redis `SPOP key` command.
  1035. func (c *cmdable) SPop(key string) *StringCmd {
  1036. cmd := NewStringCmd("spop", key)
  1037. c.process(cmd)
  1038. return cmd
  1039. }
  1040. // Redis `SPOP key count` command.
  1041. func (c *cmdable) SPopN(key string, count int64) *StringSliceCmd {
  1042. cmd := NewStringSliceCmd("spop", key, count)
  1043. c.process(cmd)
  1044. return cmd
  1045. }
  1046. // Redis `SRANDMEMBER key` command.
  1047. func (c *cmdable) SRandMember(key string) *StringCmd {
  1048. cmd := NewStringCmd("srandmember", key)
  1049. c.process(cmd)
  1050. return cmd
  1051. }
  1052. // Redis `SRANDMEMBER key count` command.
  1053. func (c *cmdable) SRandMemberN(key string, count int64) *StringSliceCmd {
  1054. cmd := NewStringSliceCmd("srandmember", key, count)
  1055. c.process(cmd)
  1056. return cmd
  1057. }
  1058. func (c *cmdable) SRem(key string, members ...interface{}) *IntCmd {
  1059. args := make([]interface{}, 2+len(members))
  1060. args[0] = "srem"
  1061. args[1] = key
  1062. for i, member := range members {
  1063. args[2+i] = member
  1064. }
  1065. cmd := NewIntCmd(args...)
  1066. c.process(cmd)
  1067. return cmd
  1068. }
  1069. func (c *cmdable) SUnion(keys ...string) *StringSliceCmd {
  1070. args := make([]interface{}, 1+len(keys))
  1071. args[0] = "sunion"
  1072. for i, key := range keys {
  1073. args[1+i] = key
  1074. }
  1075. cmd := NewStringSliceCmd(args...)
  1076. c.process(cmd)
  1077. return cmd
  1078. }
  1079. func (c *cmdable) SUnionStore(destination string, keys ...string) *IntCmd {
  1080. args := make([]interface{}, 2+len(keys))
  1081. args[0] = "sunionstore"
  1082. args[1] = destination
  1083. for i, key := range keys {
  1084. args[2+i] = key
  1085. }
  1086. cmd := NewIntCmd(args...)
  1087. c.process(cmd)
  1088. return cmd
  1089. }
  1090. //------------------------------------------------------------------------------
  1091. // Z represents sorted set member.
  1092. type Z struct {
  1093. Score float64
  1094. Member interface{}
  1095. }
  1096. // ZStore is used as an arg to ZInterStore and ZUnionStore.
  1097. type ZStore struct {
  1098. Weights []float64
  1099. // Can be SUM, MIN or MAX.
  1100. Aggregate string
  1101. }
  1102. func (c *cmdable) zAdd(a []interface{}, n int, members ...Z) *IntCmd {
  1103. for i, m := range members {
  1104. a[n+2*i] = m.Score
  1105. a[n+2*i+1] = m.Member
  1106. }
  1107. cmd := NewIntCmd(a...)
  1108. c.process(cmd)
  1109. return cmd
  1110. }
  1111. // Redis `ZADD key score member [score member ...]` command.
  1112. func (c *cmdable) ZAdd(key string, members ...Z) *IntCmd {
  1113. const n = 2
  1114. a := make([]interface{}, n+2*len(members))
  1115. a[0], a[1] = "zadd", key
  1116. return c.zAdd(a, n, members...)
  1117. }
  1118. // Redis `ZADD key NX score member [score member ...]` command.
  1119. func (c *cmdable) ZAddNX(key string, members ...Z) *IntCmd {
  1120. const n = 3
  1121. a := make([]interface{}, n+2*len(members))
  1122. a[0], a[1], a[2] = "zadd", key, "nx"
  1123. return c.zAdd(a, n, members...)
  1124. }
  1125. // Redis `ZADD key XX score member [score member ...]` command.
  1126. func (c *cmdable) ZAddXX(key string, members ...Z) *IntCmd {
  1127. const n = 3
  1128. a := make([]interface{}, n+2*len(members))
  1129. a[0], a[1], a[2] = "zadd", key, "xx"
  1130. return c.zAdd(a, n, members...)
  1131. }
  1132. // Redis `ZADD key CH score member [score member ...]` command.
  1133. func (c *cmdable) ZAddCh(key string, members ...Z) *IntCmd {
  1134. const n = 3
  1135. a := make([]interface{}, n+2*len(members))
  1136. a[0], a[1], a[2] = "zadd", key, "ch"
  1137. return c.zAdd(a, n, members...)
  1138. }
  1139. // Redis `ZADD key NX CH score member [score member ...]` command.
  1140. func (c *cmdable) ZAddNXCh(key string, members ...Z) *IntCmd {
  1141. const n = 4
  1142. a := make([]interface{}, n+2*len(members))
  1143. a[0], a[1], a[2], a[3] = "zadd", key, "nx", "ch"
  1144. return c.zAdd(a, n, members...)
  1145. }
  1146. // Redis `ZADD key XX CH score member [score member ...]` command.
  1147. func (c *cmdable) ZAddXXCh(key string, members ...Z) *IntCmd {
  1148. const n = 4
  1149. a := make([]interface{}, n+2*len(members))
  1150. a[0], a[1], a[2], a[3] = "zadd", key, "xx", "ch"
  1151. return c.zAdd(a, n, members...)
  1152. }
  1153. func (c *cmdable) zIncr(a []interface{}, n int, members ...Z) *FloatCmd {
  1154. for i, m := range members {
  1155. a[n+2*i] = m.Score
  1156. a[n+2*i+1] = m.Member
  1157. }
  1158. cmd := NewFloatCmd(a...)
  1159. c.process(cmd)
  1160. return cmd
  1161. }
  1162. // Redis `ZADD key INCR score member` command.
  1163. func (c *cmdable) ZIncr(key string, member Z) *FloatCmd {
  1164. const n = 3
  1165. a := make([]interface{}, n+2)
  1166. a[0], a[1], a[2] = "zadd", key, "incr"
  1167. return c.zIncr(a, n, member)
  1168. }
  1169. // Redis `ZADD key NX INCR score member` command.
  1170. func (c *cmdable) ZIncrNX(key string, member Z) *FloatCmd {
  1171. const n = 4
  1172. a := make([]interface{}, n+2)
  1173. a[0], a[1], a[2], a[3] = "zadd", key, "incr", "nx"
  1174. return c.zIncr(a, n, member)
  1175. }
  1176. // Redis `ZADD key XX INCR score member` command.
  1177. func (c *cmdable) ZIncrXX(key string, member Z) *FloatCmd {
  1178. const n = 4
  1179. a := make([]interface{}, n+2)
  1180. a[0], a[1], a[2], a[3] = "zadd", key, "incr", "xx"
  1181. return c.zIncr(a, n, member)
  1182. }
  1183. func (c *cmdable) ZCard(key string) *IntCmd {
  1184. cmd := NewIntCmd("zcard", key)
  1185. c.process(cmd)
  1186. return cmd
  1187. }
  1188. func (c *cmdable) ZCount(key, min, max string) *IntCmd {
  1189. cmd := NewIntCmd("zcount", key, min, max)
  1190. c.process(cmd)
  1191. return cmd
  1192. }
  1193. func (c *cmdable) ZIncrBy(key string, increment float64, member string) *FloatCmd {
  1194. cmd := NewFloatCmd("zincrby", key, increment, member)
  1195. c.process(cmd)
  1196. return cmd
  1197. }
  1198. func (c *cmdable) ZInterStore(destination string, store ZStore, keys ...string) *IntCmd {
  1199. args := make([]interface{}, 3+len(keys))
  1200. args[0] = "zinterstore"
  1201. args[1] = destination
  1202. args[2] = strconv.Itoa(len(keys))
  1203. for i, key := range keys {
  1204. args[3+i] = key
  1205. }
  1206. if len(store.Weights) > 0 {
  1207. args = append(args, "weights")
  1208. for _, weight := range store.Weights {
  1209. args = append(args, weight)
  1210. }
  1211. }
  1212. if store.Aggregate != "" {
  1213. args = append(args, "aggregate", store.Aggregate)
  1214. }
  1215. cmd := NewIntCmd(args...)
  1216. c.process(cmd)
  1217. return cmd
  1218. }
  1219. func (c *cmdable) zRange(key string, start, stop int64, withScores bool) *StringSliceCmd {
  1220. args := []interface{}{
  1221. "zrange",
  1222. key,
  1223. start,
  1224. stop,
  1225. }
  1226. if withScores {
  1227. args = append(args, "withscores")
  1228. }
  1229. cmd := NewStringSliceCmd(args...)
  1230. c.process(cmd)
  1231. return cmd
  1232. }
  1233. func (c *cmdable) ZRange(key string, start, stop int64) *StringSliceCmd {
  1234. return c.zRange(key, start, stop, false)
  1235. }
  1236. func (c *cmdable) ZRangeWithScores(key string, start, stop int64) *ZSliceCmd {
  1237. cmd := NewZSliceCmd("zrange", key, start, stop, "withscores")
  1238. c.process(cmd)
  1239. return cmd
  1240. }
  1241. type ZRangeBy struct {
  1242. Min, Max string
  1243. Offset, Count int64
  1244. }
  1245. func (c *cmdable) zRangeBy(zcmd, key string, opt ZRangeBy, withScores bool) *StringSliceCmd {
  1246. args := []interface{}{zcmd, key, opt.Min, opt.Max}
  1247. if withScores {
  1248. args = append(args, "withscores")
  1249. }
  1250. if opt.Offset != 0 || opt.Count != 0 {
  1251. args = append(
  1252. args,
  1253. "limit",
  1254. opt.Offset,
  1255. opt.Count,
  1256. )
  1257. }
  1258. cmd := NewStringSliceCmd(args...)
  1259. c.process(cmd)
  1260. return cmd
  1261. }
  1262. func (c *cmdable) ZRangeByScore(key string, opt ZRangeBy) *StringSliceCmd {
  1263. return c.zRangeBy("zrangebyscore", key, opt, false)
  1264. }
  1265. func (c *cmdable) ZRangeByLex(key string, opt ZRangeBy) *StringSliceCmd {
  1266. return c.zRangeBy("zrangebylex", key, opt, false)
  1267. }
  1268. func (c *cmdable) ZRangeByScoreWithScores(key string, opt ZRangeBy) *ZSliceCmd {
  1269. args := []interface{}{"zrangebyscore", key, opt.Min, opt.Max, "withscores"}
  1270. if opt.Offset != 0 || opt.Count != 0 {
  1271. args = append(
  1272. args,
  1273. "limit",
  1274. opt.Offset,
  1275. opt.Count,
  1276. )
  1277. }
  1278. cmd := NewZSliceCmd(args...)
  1279. c.process(cmd)
  1280. return cmd
  1281. }
  1282. func (c *cmdable) ZRank(key, member string) *IntCmd {
  1283. cmd := NewIntCmd("zrank", key, member)
  1284. c.process(cmd)
  1285. return cmd
  1286. }
  1287. func (c *cmdable) ZRem(key string, members ...interface{}) *IntCmd {
  1288. args := make([]interface{}, 2+len(members))
  1289. args[0] = "zrem"
  1290. args[1] = key
  1291. for i, member := range members {
  1292. args[2+i] = member
  1293. }
  1294. cmd := NewIntCmd(args...)
  1295. c.process(cmd)
  1296. return cmd
  1297. }
  1298. func (c *cmdable) ZRemRangeByRank(key string, start, stop int64) *IntCmd {
  1299. cmd := NewIntCmd(
  1300. "zremrangebyrank",
  1301. key,
  1302. start,
  1303. stop,
  1304. )
  1305. c.process(cmd)
  1306. return cmd
  1307. }
  1308. func (c *cmdable) ZRemRangeByScore(key, min, max string) *IntCmd {
  1309. cmd := NewIntCmd("zremrangebyscore", key, min, max)
  1310. c.process(cmd)
  1311. return cmd
  1312. }
  1313. func (c *cmdable) ZRemRangeByLex(key, min, max string) *IntCmd {
  1314. cmd := NewIntCmd("zremrangebylex", key, min, max)
  1315. c.process(cmd)
  1316. return cmd
  1317. }
  1318. func (c *cmdable) ZRevRange(key string, start, stop int64) *StringSliceCmd {
  1319. cmd := NewStringSliceCmd("zrevrange", key, start, stop)
  1320. c.process(cmd)
  1321. return cmd
  1322. }
  1323. func (c *cmdable) ZRevRangeWithScores(key string, start, stop int64) *ZSliceCmd {
  1324. cmd := NewZSliceCmd("zrevrange", key, start, stop, "withscores")
  1325. c.process(cmd)
  1326. return cmd
  1327. }
  1328. func (c *cmdable) zRevRangeBy(zcmd, key string, opt ZRangeBy) *StringSliceCmd {
  1329. args := []interface{}{zcmd, key, opt.Max, opt.Min}
  1330. if opt.Offset != 0 || opt.Count != 0 {
  1331. args = append(
  1332. args,
  1333. "limit",
  1334. opt.Offset,
  1335. opt.Count,
  1336. )
  1337. }
  1338. cmd := NewStringSliceCmd(args...)
  1339. c.process(cmd)
  1340. return cmd
  1341. }
  1342. func (c *cmdable) ZRevRangeByScore(key string, opt ZRangeBy) *StringSliceCmd {
  1343. return c.zRevRangeBy("zrevrangebyscore", key, opt)
  1344. }
  1345. func (c *cmdable) ZRevRangeByLex(key string, opt ZRangeBy) *StringSliceCmd {
  1346. return c.zRevRangeBy("zrevrangebylex", key, opt)
  1347. }
  1348. func (c *cmdable) ZRevRangeByScoreWithScores(key string, opt ZRangeBy) *ZSliceCmd {
  1349. args := []interface{}{"zrevrangebyscore", key, opt.Max, opt.Min, "withscores"}
  1350. if opt.Offset != 0 || opt.Count != 0 {
  1351. args = append(
  1352. args,
  1353. "limit",
  1354. opt.Offset,
  1355. opt.Count,
  1356. )
  1357. }
  1358. cmd := NewZSliceCmd(args...)
  1359. c.process(cmd)
  1360. return cmd
  1361. }
  1362. func (c *cmdable) ZRevRank(key, member string) *IntCmd {
  1363. cmd := NewIntCmd("zrevrank", key, member)
  1364. c.process(cmd)
  1365. return cmd
  1366. }
  1367. func (c *cmdable) ZScore(key, member string) *FloatCmd {
  1368. cmd := NewFloatCmd("zscore", key, member)
  1369. c.process(cmd)
  1370. return cmd
  1371. }
  1372. func (c *cmdable) ZUnionStore(dest string, store ZStore, keys ...string) *IntCmd {
  1373. args := make([]interface{}, 3+len(keys))
  1374. args[0] = "zunionstore"
  1375. args[1] = dest
  1376. args[2] = strconv.Itoa(len(keys))
  1377. for i, key := range keys {
  1378. args[3+i] = key
  1379. }
  1380. if len(store.Weights) > 0 {
  1381. args = append(args, "weights")
  1382. for _, weight := range store.Weights {
  1383. args = append(args, weight)
  1384. }
  1385. }
  1386. if store.Aggregate != "" {
  1387. args = append(args, "aggregate", store.Aggregate)
  1388. }
  1389. cmd := NewIntCmd(args...)
  1390. c.process(cmd)
  1391. return cmd
  1392. }
  1393. //------------------------------------------------------------------------------
  1394. func (c *cmdable) PFAdd(key string, els ...interface{}) *IntCmd {
  1395. args := make([]interface{}, 2+len(els))
  1396. args[0] = "pfadd"
  1397. args[1] = key
  1398. for i, el := range els {
  1399. args[2+i] = el
  1400. }
  1401. cmd := NewIntCmd(args...)
  1402. c.process(cmd)
  1403. return cmd
  1404. }
  1405. func (c *cmdable) PFCount(keys ...string) *IntCmd {
  1406. args := make([]interface{}, 1+len(keys))
  1407. args[0] = "pfcount"
  1408. for i, key := range keys {
  1409. args[1+i] = key
  1410. }
  1411. cmd := NewIntCmd(args...)
  1412. c.process(cmd)
  1413. return cmd
  1414. }
  1415. func (c *cmdable) PFMerge(dest string, keys ...string) *StatusCmd {
  1416. args := make([]interface{}, 2+len(keys))
  1417. args[0] = "pfmerge"
  1418. args[1] = dest
  1419. for i, key := range keys {
  1420. args[2+i] = key
  1421. }
  1422. cmd := NewStatusCmd(args...)
  1423. c.process(cmd)
  1424. return cmd
  1425. }
  1426. //------------------------------------------------------------------------------
  1427. func (c *cmdable) BgRewriteAOF() *StatusCmd {
  1428. cmd := NewStatusCmd("bgrewriteaof")
  1429. c.process(cmd)
  1430. return cmd
  1431. }
  1432. func (c *cmdable) BgSave() *StatusCmd {
  1433. cmd := NewStatusCmd("bgsave")
  1434. c.process(cmd)
  1435. return cmd
  1436. }
  1437. func (c *cmdable) ClientKill(ipPort string) *StatusCmd {
  1438. cmd := NewStatusCmd("client", "kill", ipPort)
  1439. c.process(cmd)
  1440. return cmd
  1441. }
  1442. func (c *cmdable) ClientList() *StringCmd {
  1443. cmd := NewStringCmd("client", "list")
  1444. c.process(cmd)
  1445. return cmd
  1446. }
  1447. func (c *cmdable) ClientPause(dur time.Duration) *BoolCmd {
  1448. cmd := NewBoolCmd("client", "pause", formatMs(dur))
  1449. c.process(cmd)
  1450. return cmd
  1451. }
  1452. // ClientSetName assigns a name to the connection.
  1453. func (c *statefulCmdable) ClientSetName(name string) *BoolCmd {
  1454. cmd := NewBoolCmd("client", "setname", name)
  1455. c.process(cmd)
  1456. return cmd
  1457. }
  1458. // ClientGetName returns the name of the connection.
  1459. func (c *statefulCmdable) ClientGetName() *StringCmd {
  1460. cmd := NewStringCmd("client", "getname")
  1461. c.process(cmd)
  1462. return cmd
  1463. }
  1464. func (c *cmdable) ConfigGet(parameter string) *SliceCmd {
  1465. cmd := NewSliceCmd("config", "get", parameter)
  1466. c.process(cmd)
  1467. return cmd
  1468. }
  1469. func (c *cmdable) ConfigResetStat() *StatusCmd {
  1470. cmd := NewStatusCmd("config", "resetstat")
  1471. c.process(cmd)
  1472. return cmd
  1473. }
  1474. func (c *cmdable) ConfigSet(parameter, value string) *StatusCmd {
  1475. cmd := NewStatusCmd("config", "set", parameter, value)
  1476. c.process(cmd)
  1477. return cmd
  1478. }
  1479. func (c *cmdable) DbSize() *IntCmd {
  1480. cmd := NewIntCmd("dbsize")
  1481. c.process(cmd)
  1482. return cmd
  1483. }
  1484. func (c *cmdable) FlushAll() *StatusCmd {
  1485. cmd := NewStatusCmd("flushall")
  1486. c.process(cmd)
  1487. return cmd
  1488. }
  1489. func (c *cmdable) FlushDb() *StatusCmd {
  1490. cmd := NewStatusCmd("flushdb")
  1491. c.process(cmd)
  1492. return cmd
  1493. }
  1494. func (c *cmdable) Info(section ...string) *StringCmd {
  1495. args := []interface{}{"info"}
  1496. if len(section) > 0 {
  1497. args = append(args, section[0])
  1498. }
  1499. cmd := NewStringCmd(args...)
  1500. c.process(cmd)
  1501. return cmd
  1502. }
  1503. func (c *cmdable) LastSave() *IntCmd {
  1504. cmd := NewIntCmd("lastsave")
  1505. c.process(cmd)
  1506. return cmd
  1507. }
  1508. func (c *cmdable) Save() *StatusCmd {
  1509. cmd := NewStatusCmd("save")
  1510. c.process(cmd)
  1511. return cmd
  1512. }
  1513. func (c *cmdable) shutdown(modifier string) *StatusCmd {
  1514. var args []interface{}
  1515. if modifier == "" {
  1516. args = []interface{}{"shutdown"}
  1517. } else {
  1518. args = []interface{}{"shutdown", modifier}
  1519. }
  1520. cmd := NewStatusCmd(args...)
  1521. c.process(cmd)
  1522. if err := cmd.Err(); err != nil {
  1523. if err == io.EOF {
  1524. // Server quit as expected.
  1525. cmd.err = nil
  1526. }
  1527. } else {
  1528. // Server did not quit. String reply contains the reason.
  1529. cmd.err = internal.RedisError(cmd.val)
  1530. cmd.val = ""
  1531. }
  1532. return cmd
  1533. }
  1534. func (c *cmdable) Shutdown() *StatusCmd {
  1535. return c.shutdown("")
  1536. }
  1537. func (c *cmdable) ShutdownSave() *StatusCmd {
  1538. return c.shutdown("save")
  1539. }
  1540. func (c *cmdable) ShutdownNoSave() *StatusCmd {
  1541. return c.shutdown("nosave")
  1542. }
  1543. func (c *cmdable) SlaveOf(host, port string) *StatusCmd {
  1544. cmd := NewStatusCmd("slaveof", host, port)
  1545. c.process(cmd)
  1546. return cmd
  1547. }
  1548. func (c *cmdable) SlowLog() {
  1549. panic("not implemented")
  1550. }
  1551. func (c *cmdable) Sync() {
  1552. panic("not implemented")
  1553. }
  1554. func (c *cmdable) Time() *TimeCmd {
  1555. cmd := NewTimeCmd("time")
  1556. c.process(cmd)
  1557. return cmd
  1558. }
  1559. //------------------------------------------------------------------------------
  1560. func (c *cmdable) Eval(script string, keys []string, args ...interface{}) *Cmd {
  1561. cmdArgs := make([]interface{}, 3+len(keys)+len(args))
  1562. cmdArgs[0] = "eval"
  1563. cmdArgs[1] = script
  1564. cmdArgs[2] = strconv.Itoa(len(keys))
  1565. for i, key := range keys {
  1566. cmdArgs[3+i] = key
  1567. }
  1568. pos := 3 + len(keys)
  1569. for i, arg := range args {
  1570. cmdArgs[pos+i] = arg
  1571. }
  1572. cmd := NewCmd(cmdArgs...)
  1573. c.process(cmd)
  1574. return cmd
  1575. }
  1576. func (c *cmdable) EvalSha(sha1 string, keys []string, args ...interface{}) *Cmd {
  1577. cmdArgs := make([]interface{}, 3+len(keys)+len(args))
  1578. cmdArgs[0] = "evalsha"
  1579. cmdArgs[1] = sha1
  1580. cmdArgs[2] = strconv.Itoa(len(keys))
  1581. for i, key := range keys {
  1582. cmdArgs[3+i] = key
  1583. }
  1584. pos := 3 + len(keys)
  1585. for i, arg := range args {
  1586. cmdArgs[pos+i] = arg
  1587. }
  1588. cmd := NewCmd(cmdArgs...)
  1589. c.process(cmd)
  1590. return cmd
  1591. }
  1592. func (c *cmdable) ScriptExists(scripts ...string) *BoolSliceCmd {
  1593. args := make([]interface{}, 2+len(scripts))
  1594. args[0] = "script"
  1595. args[1] = "exists"
  1596. for i, script := range scripts {
  1597. args[2+i] = script
  1598. }
  1599. cmd := NewBoolSliceCmd(args...)
  1600. c.process(cmd)
  1601. return cmd
  1602. }
  1603. func (c *cmdable) ScriptFlush() *StatusCmd {
  1604. cmd := NewStatusCmd("script", "flush")
  1605. c.process(cmd)
  1606. return cmd
  1607. }
  1608. func (c *cmdable) ScriptKill() *StatusCmd {
  1609. cmd := NewStatusCmd("script", "kill")
  1610. c.process(cmd)
  1611. return cmd
  1612. }
  1613. func (c *cmdable) ScriptLoad(script string) *StringCmd {
  1614. cmd := NewStringCmd("script", "load", script)
  1615. c.process(cmd)
  1616. return cmd
  1617. }
  1618. //------------------------------------------------------------------------------
  1619. func (c *cmdable) DebugObject(key string) *StringCmd {
  1620. cmd := NewStringCmd("debug", "object", key)
  1621. c.process(cmd)
  1622. return cmd
  1623. }
  1624. //------------------------------------------------------------------------------
  1625. // Publish posts the message to the channel.
  1626. func (c *cmdable) Publish(channel, message string) *IntCmd {
  1627. cmd := NewIntCmd("PUBLISH", channel, message)
  1628. c.process(cmd)
  1629. return cmd
  1630. }
  1631. func (c *cmdable) PubSubChannels(pattern string) *StringSliceCmd {
  1632. args := []interface{}{"pubsub", "channels"}
  1633. if pattern != "*" {
  1634. args = append(args, pattern)
  1635. }
  1636. cmd := NewStringSliceCmd(args...)
  1637. c.process(cmd)
  1638. return cmd
  1639. }
  1640. func (c *cmdable) PubSubNumSub(channels ...string) *StringIntMapCmd {
  1641. args := make([]interface{}, 2+len(channels))
  1642. args[0] = "pubsub"
  1643. args[1] = "numsub"
  1644. for i, channel := range channels {
  1645. args[2+i] = channel
  1646. }
  1647. cmd := NewStringIntMapCmd(args...)
  1648. c.process(cmd)
  1649. return cmd
  1650. }
  1651. func (c *cmdable) PubSubNumPat() *IntCmd {
  1652. cmd := NewIntCmd("pubsub", "numpat")
  1653. c.process(cmd)
  1654. return cmd
  1655. }
  1656. //------------------------------------------------------------------------------
  1657. func (c *cmdable) ClusterSlots() *ClusterSlotsCmd {
  1658. cmd := NewClusterSlotsCmd("cluster", "slots")
  1659. c.process(cmd)
  1660. return cmd
  1661. }
  1662. func (c *cmdable) ClusterNodes() *StringCmd {
  1663. cmd := NewStringCmd("cluster", "nodes")
  1664. c.process(cmd)
  1665. return cmd
  1666. }
  1667. func (c *cmdable) ClusterMeet(host, port string) *StatusCmd {
  1668. cmd := NewStatusCmd("cluster", "meet", host, port)
  1669. c.process(cmd)
  1670. return cmd
  1671. }
  1672. func (c *cmdable) ClusterForget(nodeID string) *StatusCmd {
  1673. cmd := NewStatusCmd("cluster", "forget", nodeID)
  1674. c.process(cmd)
  1675. return cmd
  1676. }
  1677. func (c *cmdable) ClusterReplicate(nodeID string) *StatusCmd {
  1678. cmd := NewStatusCmd("cluster", "replicate", nodeID)
  1679. c.process(cmd)
  1680. return cmd
  1681. }
  1682. func (c *cmdable) ClusterResetSoft() *StatusCmd {
  1683. cmd := NewStatusCmd("cluster", "reset", "soft")
  1684. c.process(cmd)
  1685. return cmd
  1686. }
  1687. func (c *cmdable) ClusterResetHard() *StatusCmd {
  1688. cmd := NewStatusCmd("cluster", "reset", "hard")
  1689. c.process(cmd)
  1690. return cmd
  1691. }
  1692. func (c *cmdable) ClusterInfo() *StringCmd {
  1693. cmd := NewStringCmd("cluster", "info")
  1694. c.process(cmd)
  1695. return cmd
  1696. }
  1697. func (c *cmdable) ClusterKeySlot(key string) *IntCmd {
  1698. cmd := NewIntCmd("cluster", "keyslot", key)
  1699. c.process(cmd)
  1700. return cmd
  1701. }
  1702. func (c *cmdable) ClusterCountFailureReports(nodeID string) *IntCmd {
  1703. cmd := NewIntCmd("cluster", "count-failure-reports", nodeID)
  1704. c.process(cmd)
  1705. return cmd
  1706. }
  1707. func (c *cmdable) ClusterCountKeysInSlot(slot int) *IntCmd {
  1708. cmd := NewIntCmd("cluster", "countkeysinslot", slot)
  1709. c.process(cmd)
  1710. return cmd
  1711. }
  1712. func (c *cmdable) ClusterDelSlots(slots ...int) *StatusCmd {
  1713. args := make([]interface{}, 2+len(slots))
  1714. args[0] = "cluster"
  1715. args[1] = "delslots"
  1716. for i, slot := range slots {
  1717. args[2+i] = slot
  1718. }
  1719. cmd := NewStatusCmd(args...)
  1720. c.process(cmd)
  1721. return cmd
  1722. }
  1723. func (c *cmdable) ClusterDelSlotsRange(min, max int) *StatusCmd {
  1724. size := max - min + 1
  1725. slots := make([]int, size)
  1726. for i := 0; i < size; i++ {
  1727. slots[i] = min + i
  1728. }
  1729. return c.ClusterDelSlots(slots...)
  1730. }
  1731. func (c *cmdable) ClusterSaveConfig() *StatusCmd {
  1732. cmd := NewStatusCmd("cluster", "saveconfig")
  1733. c.process(cmd)
  1734. return cmd
  1735. }
  1736. func (c *cmdable) ClusterSlaves(nodeID string) *StringSliceCmd {
  1737. cmd := NewStringSliceCmd("cluster", "slaves", nodeID)
  1738. c.process(cmd)
  1739. return cmd
  1740. }
  1741. func (c *statefulCmdable) ReadOnly() *StatusCmd {
  1742. cmd := NewStatusCmd("readonly")
  1743. c.process(cmd)
  1744. return cmd
  1745. }
  1746. func (c *statefulCmdable) ReadWrite() *StatusCmd {
  1747. cmd := NewStatusCmd("readwrite")
  1748. c.process(cmd)
  1749. return cmd
  1750. }
  1751. func (c *cmdable) ClusterFailover() *StatusCmd {
  1752. cmd := NewStatusCmd("cluster", "failover")
  1753. c.process(cmd)
  1754. return cmd
  1755. }
  1756. func (c *cmdable) ClusterAddSlots(slots ...int) *StatusCmd {
  1757. args := make([]interface{}, 2+len(slots))
  1758. args[0] = "cluster"
  1759. args[1] = "addslots"
  1760. for i, num := range slots {
  1761. args[2+i] = strconv.Itoa(num)
  1762. }
  1763. cmd := NewStatusCmd(args...)
  1764. c.process(cmd)
  1765. return cmd
  1766. }
  1767. func (c *cmdable) ClusterAddSlotsRange(min, max int) *StatusCmd {
  1768. size := max - min + 1
  1769. slots := make([]int, size)
  1770. for i := 0; i < size; i++ {
  1771. slots[i] = min + i
  1772. }
  1773. return c.ClusterAddSlots(slots...)
  1774. }
  1775. //------------------------------------------------------------------------------
  1776. func (c *cmdable) GeoAdd(key string, geoLocation ...*GeoLocation) *IntCmd {
  1777. args := make([]interface{}, 2+3*len(geoLocation))
  1778. args[0] = "geoadd"
  1779. args[1] = key
  1780. for i, eachLoc := range geoLocation {
  1781. args[2+3*i] = eachLoc.Longitude
  1782. args[2+3*i+1] = eachLoc.Latitude
  1783. args[2+3*i+2] = eachLoc.Name
  1784. }
  1785. cmd := NewIntCmd(args...)
  1786. c.process(cmd)
  1787. return cmd
  1788. }
  1789. func (c *cmdable) GeoRadius(key string, longitude, latitude float64, query *GeoRadiusQuery) *GeoLocationCmd {
  1790. cmd := NewGeoLocationCmd(query, "georadius", key, longitude, latitude)
  1791. c.process(cmd)
  1792. return cmd
  1793. }
  1794. func (c *cmdable) GeoRadiusByMember(key, member string, query *GeoRadiusQuery) *GeoLocationCmd {
  1795. cmd := NewGeoLocationCmd(query, "georadiusbymember", key, member)
  1796. c.process(cmd)
  1797. return cmd
  1798. }
  1799. func (c *cmdable) GeoDist(key string, member1, member2, unit string) *FloatCmd {
  1800. if unit == "" {
  1801. unit = "km"
  1802. }
  1803. cmd := NewFloatCmd("geodist", key, member1, member2, unit)
  1804. c.process(cmd)
  1805. return cmd
  1806. }
  1807. func (c *cmdable) GeoHash(key string, members ...string) *StringSliceCmd {
  1808. args := make([]interface{}, 2+len(members))
  1809. args[0] = "geohash"
  1810. args[1] = key
  1811. for i, member := range members {
  1812. args[2+i] = member
  1813. }
  1814. cmd := NewStringSliceCmd(args...)
  1815. c.process(cmd)
  1816. return cmd
  1817. }
  1818. func (c *cmdable) GeoPos(key string, members ...string) *GeoPosCmd {
  1819. args := make([]interface{}, 2+len(members))
  1820. args[0] = "geopos"
  1821. args[1] = key
  1822. for i, member := range members {
  1823. args[2+i] = member
  1824. }
  1825. cmd := NewGeoPosCmd(args...)
  1826. c.process(cmd)
  1827. return cmd
  1828. }
  1829. //------------------------------------------------------------------------------
  1830. func (c *cmdable) Command() *CommandsInfoCmd {
  1831. cmd := NewCommandsInfoCmd("command")
  1832. c.process(cmd)
  1833. return cmd
  1834. }