commands.go 53 KB

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