commands.go 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628
  1. package redis
  2. import (
  3. "errors"
  4. "io"
  5. "time"
  6. "github.com/go-redis/redis/internal"
  7. )
  8. func usePrecise(dur time.Duration) bool {
  9. return dur < time.Second || dur%time.Second != 0
  10. }
  11. func formatMs(dur time.Duration) int64 {
  12. if dur > 0 && dur < time.Millisecond {
  13. internal.Logger.Printf(
  14. "specified duration is %s, but minimal supported value is %s",
  15. dur, time.Millisecond,
  16. )
  17. }
  18. return int64(dur / time.Millisecond)
  19. }
  20. func formatSec(dur time.Duration) int64 {
  21. if dur > 0 && dur < time.Second {
  22. internal.Logger.Printf(
  23. "specified duration is %s, but minimal supported value is %s",
  24. dur, time.Second,
  25. )
  26. }
  27. return int64(dur / time.Second)
  28. }
  29. func appendArgs(dst, src []interface{}) []interface{} {
  30. if len(src) == 1 {
  31. switch v := src[0].(type) {
  32. case []string:
  33. for _, s := range v {
  34. dst = append(dst, s)
  35. }
  36. return dst
  37. case map[string]interface{}:
  38. for k, v := range v {
  39. dst = append(dst, k, v)
  40. }
  41. return dst
  42. }
  43. }
  44. dst = append(dst, src...)
  45. return dst
  46. }
  47. type Cmdable interface {
  48. Pipeline() Pipeliner
  49. Pipelined(fn func(Pipeliner) error) ([]Cmder, error)
  50. TxPipelined(fn func(Pipeliner) error) ([]Cmder, error)
  51. TxPipeline() Pipeliner
  52. Command() *CommandsInfoCmd
  53. ClientGetName() *StringCmd
  54. Echo(message interface{}) *StringCmd
  55. Ping() *StatusCmd
  56. Quit() *StatusCmd
  57. Del(keys ...string) *IntCmd
  58. Unlink(keys ...string) *IntCmd
  59. Dump(key string) *StringCmd
  60. Exists(keys ...string) *IntCmd
  61. Expire(key string, expiration time.Duration) *BoolCmd
  62. ExpireAt(key string, tm time.Time) *BoolCmd
  63. Keys(pattern string) *StringSliceCmd
  64. Migrate(host, port, key string, db int, timeout time.Duration) *StatusCmd
  65. Move(key string, db int) *BoolCmd
  66. ObjectRefCount(key string) *IntCmd
  67. ObjectEncoding(key string) *StringCmd
  68. ObjectIdleTime(key string) *DurationCmd
  69. Persist(key string) *BoolCmd
  70. PExpire(key string, expiration time.Duration) *BoolCmd
  71. PExpireAt(key string, tm time.Time) *BoolCmd
  72. PTTL(key string) *DurationCmd
  73. RandomKey() *StringCmd
  74. Rename(key, newkey string) *StatusCmd
  75. RenameNX(key, newkey string) *BoolCmd
  76. Restore(key string, ttl time.Duration, value string) *StatusCmd
  77. RestoreReplace(key string, ttl time.Duration, value string) *StatusCmd
  78. Sort(key string, sort *Sort) *StringSliceCmd
  79. SortStore(key, store string, sort *Sort) *IntCmd
  80. SortInterfaces(key string, sort *Sort) *SliceCmd
  81. Touch(keys ...string) *IntCmd
  82. TTL(key string) *DurationCmd
  83. Type(key string) *StatusCmd
  84. Scan(cursor uint64, match string, count int64) *ScanCmd
  85. SScan(key string, cursor uint64, match string, count int64) *ScanCmd
  86. HScan(key string, cursor uint64, match string, count int64) *ScanCmd
  87. ZScan(key string, cursor uint64, match string, count int64) *ScanCmd
  88. Append(key, value string) *IntCmd
  89. BitCount(key string, bitCount *BitCount) *IntCmd
  90. BitOpAnd(destKey string, keys ...string) *IntCmd
  91. BitOpOr(destKey string, keys ...string) *IntCmd
  92. BitOpXor(destKey string, keys ...string) *IntCmd
  93. BitOpNot(destKey string, key string) *IntCmd
  94. BitPos(key string, bit int64, pos ...int64) *IntCmd
  95. BitField(key string, args ...interface{}) *IntSliceCmd
  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(values ...interface{}) *StatusCmd
  107. MSetNX(values ...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, values ...interface{}) *IntCmd
  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, values ...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, values ...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. XAdd(a *XAddArgs) *StringCmd
  164. XDel(stream string, ids ...string) *IntCmd
  165. XLen(stream string) *IntCmd
  166. XRange(stream, start, stop string) *XMessageSliceCmd
  167. XRangeN(stream, start, stop string, count int64) *XMessageSliceCmd
  168. XRevRange(stream string, start, stop string) *XMessageSliceCmd
  169. XRevRangeN(stream string, start, stop string, count int64) *XMessageSliceCmd
  170. XRead(a *XReadArgs) *XStreamSliceCmd
  171. XReadStreams(streams ...string) *XStreamSliceCmd
  172. XGroupCreate(stream, group, start string) *StatusCmd
  173. XGroupCreateMkStream(stream, group, start string) *StatusCmd
  174. XGroupSetID(stream, group, start string) *StatusCmd
  175. XGroupDestroy(stream, group string) *IntCmd
  176. XGroupDelConsumer(stream, group, consumer string) *IntCmd
  177. XReadGroup(a *XReadGroupArgs) *XStreamSliceCmd
  178. XAck(stream, group string, ids ...string) *IntCmd
  179. XPending(stream, group string) *XPendingCmd
  180. XPendingExt(a *XPendingExtArgs) *XPendingExtCmd
  181. XClaim(a *XClaimArgs) *XMessageSliceCmd
  182. XClaimJustID(a *XClaimArgs) *StringSliceCmd
  183. XTrim(key string, maxLen int64) *IntCmd
  184. XTrimApprox(key string, maxLen int64) *IntCmd
  185. XInfoGroups(key string) *XInfoGroupsCmd
  186. BZPopMax(timeout time.Duration, keys ...string) *ZWithKeyCmd
  187. BZPopMin(timeout time.Duration, keys ...string) *ZWithKeyCmd
  188. ZAdd(key string, members ...*Z) *IntCmd
  189. ZAddNX(key string, members ...*Z) *IntCmd
  190. ZAddXX(key string, members ...*Z) *IntCmd
  191. ZAddCh(key string, members ...*Z) *IntCmd
  192. ZAddNXCh(key string, members ...*Z) *IntCmd
  193. ZAddXXCh(key string, members ...*Z) *IntCmd
  194. ZIncr(key string, member *Z) *FloatCmd
  195. ZIncrNX(key string, member *Z) *FloatCmd
  196. ZIncrXX(key string, member *Z) *FloatCmd
  197. ZCard(key string) *IntCmd
  198. ZCount(key, min, max string) *IntCmd
  199. ZLexCount(key, min, max string) *IntCmd
  200. ZIncrBy(key string, increment float64, member string) *FloatCmd
  201. ZInterStore(destination string, store *ZStore) *IntCmd
  202. ZPopMax(key string, count ...int64) *ZSliceCmd
  203. ZPopMin(key string, count ...int64) *ZSliceCmd
  204. ZRange(key string, start, stop int64) *StringSliceCmd
  205. ZRangeWithScores(key string, start, stop int64) *ZSliceCmd
  206. ZRangeByScore(key string, opt *ZRangeBy) *StringSliceCmd
  207. ZRangeByLex(key string, opt *ZRangeBy) *StringSliceCmd
  208. ZRangeByScoreWithScores(key string, opt *ZRangeBy) *ZSliceCmd
  209. ZRank(key, member string) *IntCmd
  210. ZRem(key string, members ...interface{}) *IntCmd
  211. ZRemRangeByRank(key string, start, stop int64) *IntCmd
  212. ZRemRangeByScore(key, min, max string) *IntCmd
  213. ZRemRangeByLex(key, min, max string) *IntCmd
  214. ZRevRange(key string, start, stop int64) *StringSliceCmd
  215. ZRevRangeWithScores(key string, start, stop int64) *ZSliceCmd
  216. ZRevRangeByScore(key string, opt *ZRangeBy) *StringSliceCmd
  217. ZRevRangeByLex(key string, opt *ZRangeBy) *StringSliceCmd
  218. ZRevRangeByScoreWithScores(key string, opt *ZRangeBy) *ZSliceCmd
  219. ZRevRank(key, member string) *IntCmd
  220. ZScore(key, member string) *FloatCmd
  221. ZUnionStore(dest string, store *ZStore) *IntCmd
  222. PFAdd(key string, els ...interface{}) *IntCmd
  223. PFCount(keys ...string) *IntCmd
  224. PFMerge(dest string, keys ...string) *StatusCmd
  225. BgRewriteAOF() *StatusCmd
  226. BgSave() *StatusCmd
  227. ClientKill(ipPort string) *StatusCmd
  228. ClientKillByFilter(keys ...string) *IntCmd
  229. ClientList() *StringCmd
  230. ClientPause(dur time.Duration) *BoolCmd
  231. ClientID() *IntCmd
  232. ConfigGet(parameter string) *SliceCmd
  233. ConfigResetStat() *StatusCmd
  234. ConfigSet(parameter, value string) *StatusCmd
  235. ConfigRewrite() *StatusCmd
  236. DBSize() *IntCmd
  237. FlushAll() *StatusCmd
  238. FlushAllAsync() *StatusCmd
  239. FlushDB() *StatusCmd
  240. FlushDBAsync() *StatusCmd
  241. Info(section ...string) *StringCmd
  242. LastSave() *IntCmd
  243. Save() *StatusCmd
  244. Shutdown() *StatusCmd
  245. ShutdownSave() *StatusCmd
  246. ShutdownNoSave() *StatusCmd
  247. SlaveOf(host, port string) *StatusCmd
  248. Time() *TimeCmd
  249. Eval(script string, keys []string, args ...interface{}) *Cmd
  250. EvalSha(sha1 string, keys []string, args ...interface{}) *Cmd
  251. ScriptExists(hashes ...string) *BoolSliceCmd
  252. ScriptFlush() *StatusCmd
  253. ScriptKill() *StatusCmd
  254. ScriptLoad(script string) *StringCmd
  255. DebugObject(key string) *StringCmd
  256. Publish(channel string, message interface{}) *IntCmd
  257. PubSubChannels(pattern string) *StringSliceCmd
  258. PubSubNumSub(channels ...string) *StringIntMapCmd
  259. PubSubNumPat() *IntCmd
  260. ClusterSlots() *ClusterSlotsCmd
  261. ClusterNodes() *StringCmd
  262. ClusterMeet(host, port string) *StatusCmd
  263. ClusterForget(nodeID string) *StatusCmd
  264. ClusterReplicate(nodeID string) *StatusCmd
  265. ClusterResetSoft() *StatusCmd
  266. ClusterResetHard() *StatusCmd
  267. ClusterInfo() *StringCmd
  268. ClusterKeySlot(key string) *IntCmd
  269. ClusterGetKeysInSlot(slot int, count int) *StringSliceCmd
  270. ClusterCountFailureReports(nodeID string) *IntCmd
  271. ClusterCountKeysInSlot(slot int) *IntCmd
  272. ClusterDelSlots(slots ...int) *StatusCmd
  273. ClusterDelSlotsRange(min, max int) *StatusCmd
  274. ClusterSaveConfig() *StatusCmd
  275. ClusterSlaves(nodeID string) *StringSliceCmd
  276. ClusterFailover() *StatusCmd
  277. ClusterAddSlots(slots ...int) *StatusCmd
  278. ClusterAddSlotsRange(min, max int) *StatusCmd
  279. GeoAdd(key string, geoLocation ...*GeoLocation) *IntCmd
  280. GeoPos(key string, members ...string) *GeoPosCmd
  281. GeoRadius(key string, longitude, latitude float64, query *GeoRadiusQuery) *GeoLocationCmd
  282. GeoRadiusStore(key string, longitude, latitude float64, query *GeoRadiusQuery) *IntCmd
  283. GeoRadiusByMember(key, member string, query *GeoRadiusQuery) *GeoLocationCmd
  284. GeoRadiusByMemberStore(key, member string, query *GeoRadiusQuery) *IntCmd
  285. GeoDist(key string, member1, member2, unit string) *FloatCmd
  286. GeoHash(key string, members ...string) *StringSliceCmd
  287. ReadOnly() *StatusCmd
  288. ReadWrite() *StatusCmd
  289. MemoryUsage(key string, samples ...int) *IntCmd
  290. }
  291. type StatefulCmdable interface {
  292. Cmdable
  293. Auth(password string) *StatusCmd
  294. Select(index int) *StatusCmd
  295. SwapDB(index1, index2 int) *StatusCmd
  296. ClientSetName(name string) *BoolCmd
  297. }
  298. var _ Cmdable = (*Client)(nil)
  299. var _ Cmdable = (*Tx)(nil)
  300. var _ Cmdable = (*Ring)(nil)
  301. var _ Cmdable = (*ClusterClient)(nil)
  302. type cmdable func(cmd Cmder) error
  303. type statefulCmdable func(cmd Cmder) error
  304. //------------------------------------------------------------------------------
  305. func (c statefulCmdable) Auth(password string) *StatusCmd {
  306. cmd := NewStatusCmd("auth", password)
  307. _ = c(cmd)
  308. return cmd
  309. }
  310. func (c cmdable) Echo(message interface{}) *StringCmd {
  311. cmd := NewStringCmd("echo", message)
  312. _ = c(cmd)
  313. return cmd
  314. }
  315. func (c cmdable) Ping() *StatusCmd {
  316. cmd := NewStatusCmd("ping")
  317. _ = c(cmd)
  318. return cmd
  319. }
  320. func (c cmdable) Wait(numSlaves int, timeout time.Duration) *IntCmd {
  321. cmd := NewIntCmd("wait", numSlaves, int(timeout/time.Millisecond))
  322. _ = c(cmd)
  323. return cmd
  324. }
  325. func (c cmdable) Quit() *StatusCmd {
  326. panic("not implemented")
  327. }
  328. func (c statefulCmdable) Select(index int) *StatusCmd {
  329. cmd := NewStatusCmd("select", index)
  330. _ = c(cmd)
  331. return cmd
  332. }
  333. func (c statefulCmdable) SwapDB(index1, index2 int) *StatusCmd {
  334. cmd := NewStatusCmd("swapdb", index1, index2)
  335. _ = c(cmd)
  336. return cmd
  337. }
  338. //------------------------------------------------------------------------------
  339. func (c cmdable) Command() *CommandsInfoCmd {
  340. cmd := NewCommandsInfoCmd("command")
  341. _ = c(cmd)
  342. return cmd
  343. }
  344. func (c cmdable) Del(keys ...string) *IntCmd {
  345. args := make([]interface{}, 1+len(keys))
  346. args[0] = "del"
  347. for i, key := range keys {
  348. args[1+i] = key
  349. }
  350. cmd := NewIntCmd(args...)
  351. _ = c(cmd)
  352. return cmd
  353. }
  354. func (c cmdable) Unlink(keys ...string) *IntCmd {
  355. args := make([]interface{}, 1+len(keys))
  356. args[0] = "unlink"
  357. for i, key := range keys {
  358. args[1+i] = key
  359. }
  360. cmd := NewIntCmd(args...)
  361. _ = c(cmd)
  362. return cmd
  363. }
  364. func (c cmdable) Dump(key string) *StringCmd {
  365. cmd := NewStringCmd("dump", key)
  366. _ = c(cmd)
  367. return cmd
  368. }
  369. func (c cmdable) Exists(keys ...string) *IntCmd {
  370. args := make([]interface{}, 1+len(keys))
  371. args[0] = "exists"
  372. for i, key := range keys {
  373. args[1+i] = key
  374. }
  375. cmd := NewIntCmd(args...)
  376. _ = c(cmd)
  377. return cmd
  378. }
  379. func (c cmdable) Expire(key string, expiration time.Duration) *BoolCmd {
  380. cmd := NewBoolCmd("expire", key, formatSec(expiration))
  381. _ = c(cmd)
  382. return cmd
  383. }
  384. func (c cmdable) ExpireAt(key string, tm time.Time) *BoolCmd {
  385. cmd := NewBoolCmd("expireat", key, tm.Unix())
  386. _ = c(cmd)
  387. return cmd
  388. }
  389. func (c cmdable) Keys(pattern string) *StringSliceCmd {
  390. cmd := NewStringSliceCmd("keys", pattern)
  391. _ = c(cmd)
  392. return cmd
  393. }
  394. func (c cmdable) Migrate(host, port, key string, db int, timeout time.Duration) *StatusCmd {
  395. cmd := NewStatusCmd(
  396. "migrate",
  397. host,
  398. port,
  399. key,
  400. db,
  401. formatMs(timeout),
  402. )
  403. cmd.setReadTimeout(timeout)
  404. _ = c(cmd)
  405. return cmd
  406. }
  407. func (c cmdable) Move(key string, db int) *BoolCmd {
  408. cmd := NewBoolCmd("move", key, db)
  409. _ = c(cmd)
  410. return cmd
  411. }
  412. func (c cmdable) ObjectRefCount(key string) *IntCmd {
  413. cmd := NewIntCmd("object", "refcount", key)
  414. _ = c(cmd)
  415. return cmd
  416. }
  417. func (c cmdable) ObjectEncoding(key string) *StringCmd {
  418. cmd := NewStringCmd("object", "encoding", key)
  419. _ = c(cmd)
  420. return cmd
  421. }
  422. func (c cmdable) ObjectIdleTime(key string) *DurationCmd {
  423. cmd := NewDurationCmd(time.Second, "object", "idletime", key)
  424. _ = c(cmd)
  425. return cmd
  426. }
  427. func (c cmdable) Persist(key string) *BoolCmd {
  428. cmd := NewBoolCmd("persist", key)
  429. _ = c(cmd)
  430. return cmd
  431. }
  432. func (c cmdable) PExpire(key string, expiration time.Duration) *BoolCmd {
  433. cmd := NewBoolCmd("pexpire", key, formatMs(expiration))
  434. _ = c(cmd)
  435. return cmd
  436. }
  437. func (c cmdable) PExpireAt(key string, tm time.Time) *BoolCmd {
  438. cmd := NewBoolCmd(
  439. "pexpireat",
  440. key,
  441. tm.UnixNano()/int64(time.Millisecond),
  442. )
  443. _ = c(cmd)
  444. return cmd
  445. }
  446. func (c cmdable) PTTL(key string) *DurationCmd {
  447. cmd := NewDurationCmd(time.Millisecond, "pttl", key)
  448. _ = c(cmd)
  449. return cmd
  450. }
  451. func (c cmdable) RandomKey() *StringCmd {
  452. cmd := NewStringCmd("randomkey")
  453. _ = c(cmd)
  454. return cmd
  455. }
  456. func (c cmdable) Rename(key, newkey string) *StatusCmd {
  457. cmd := NewStatusCmd("rename", key, newkey)
  458. _ = c(cmd)
  459. return cmd
  460. }
  461. func (c cmdable) RenameNX(key, newkey string) *BoolCmd {
  462. cmd := NewBoolCmd("renamenx", key, newkey)
  463. _ = c(cmd)
  464. return cmd
  465. }
  466. func (c cmdable) Restore(key string, ttl time.Duration, value string) *StatusCmd {
  467. cmd := NewStatusCmd(
  468. "restore",
  469. key,
  470. formatMs(ttl),
  471. value,
  472. )
  473. _ = c(cmd)
  474. return cmd
  475. }
  476. func (c cmdable) RestoreReplace(key string, ttl time.Duration, value string) *StatusCmd {
  477. cmd := NewStatusCmd(
  478. "restore",
  479. key,
  480. formatMs(ttl),
  481. value,
  482. "replace",
  483. )
  484. _ = c(cmd)
  485. return cmd
  486. }
  487. type Sort struct {
  488. By string
  489. Offset, Count int64
  490. Get []string
  491. Order string
  492. Alpha bool
  493. }
  494. func (sort *Sort) args(key string) []interface{} {
  495. args := []interface{}{"sort", key}
  496. if sort.By != "" {
  497. args = append(args, "by", sort.By)
  498. }
  499. if sort.Offset != 0 || sort.Count != 0 {
  500. args = append(args, "limit", sort.Offset, sort.Count)
  501. }
  502. for _, get := range sort.Get {
  503. args = append(args, "get", get)
  504. }
  505. if sort.Order != "" {
  506. args = append(args, sort.Order)
  507. }
  508. if sort.Alpha {
  509. args = append(args, "alpha")
  510. }
  511. return args
  512. }
  513. func (c cmdable) Sort(key string, sort *Sort) *StringSliceCmd {
  514. cmd := NewStringSliceCmd(sort.args(key)...)
  515. _ = c(cmd)
  516. return cmd
  517. }
  518. func (c cmdable) SortStore(key, store string, sort *Sort) *IntCmd {
  519. args := sort.args(key)
  520. if store != "" {
  521. args = append(args, "store", store)
  522. }
  523. cmd := NewIntCmd(args...)
  524. _ = c(cmd)
  525. return cmd
  526. }
  527. func (c cmdable) SortInterfaces(key string, sort *Sort) *SliceCmd {
  528. cmd := NewSliceCmd(sort.args(key)...)
  529. _ = c(cmd)
  530. return cmd
  531. }
  532. func (c cmdable) Touch(keys ...string) *IntCmd {
  533. args := make([]interface{}, len(keys)+1)
  534. args[0] = "touch"
  535. for i, key := range keys {
  536. args[i+1] = key
  537. }
  538. cmd := NewIntCmd(args...)
  539. _ = c(cmd)
  540. return cmd
  541. }
  542. func (c cmdable) TTL(key string) *DurationCmd {
  543. cmd := NewDurationCmd(time.Second, "ttl", key)
  544. _ = c(cmd)
  545. return cmd
  546. }
  547. func (c cmdable) Type(key string) *StatusCmd {
  548. cmd := NewStatusCmd("type", key)
  549. _ = c(cmd)
  550. return cmd
  551. }
  552. func (c cmdable) Scan(cursor uint64, match string, count int64) *ScanCmd {
  553. args := []interface{}{"scan", cursor}
  554. if match != "" {
  555. args = append(args, "match", match)
  556. }
  557. if count > 0 {
  558. args = append(args, "count", count)
  559. }
  560. cmd := NewScanCmd(c, args...)
  561. _ = c(cmd)
  562. return cmd
  563. }
  564. func (c cmdable) SScan(key string, cursor uint64, match string, count int64) *ScanCmd {
  565. args := []interface{}{"sscan", key, cursor}
  566. if match != "" {
  567. args = append(args, "match", match)
  568. }
  569. if count > 0 {
  570. args = append(args, "count", count)
  571. }
  572. cmd := NewScanCmd(c, args...)
  573. _ = c(cmd)
  574. return cmd
  575. }
  576. func (c cmdable) HScan(key string, cursor uint64, match string, count int64) *ScanCmd {
  577. args := []interface{}{"hscan", key, cursor}
  578. if match != "" {
  579. args = append(args, "match", match)
  580. }
  581. if count > 0 {
  582. args = append(args, "count", count)
  583. }
  584. cmd := NewScanCmd(c, args...)
  585. _ = c(cmd)
  586. return cmd
  587. }
  588. func (c cmdable) ZScan(key string, cursor uint64, match string, count int64) *ScanCmd {
  589. args := []interface{}{"zscan", key, cursor}
  590. if match != "" {
  591. args = append(args, "match", match)
  592. }
  593. if count > 0 {
  594. args = append(args, "count", count)
  595. }
  596. cmd := NewScanCmd(c, args...)
  597. _ = c(cmd)
  598. return cmd
  599. }
  600. //------------------------------------------------------------------------------
  601. func (c cmdable) Append(key, value string) *IntCmd {
  602. cmd := NewIntCmd("append", key, value)
  603. _ = c(cmd)
  604. return cmd
  605. }
  606. type BitCount struct {
  607. Start, End int64
  608. }
  609. func (c cmdable) BitCount(key string, bitCount *BitCount) *IntCmd {
  610. args := []interface{}{"bitcount", key}
  611. if bitCount != nil {
  612. args = append(
  613. args,
  614. bitCount.Start,
  615. bitCount.End,
  616. )
  617. }
  618. cmd := NewIntCmd(args...)
  619. _ = c(cmd)
  620. return cmd
  621. }
  622. func (c cmdable) bitOp(op, destKey string, keys ...string) *IntCmd {
  623. args := make([]interface{}, 3+len(keys))
  624. args[0] = "bitop"
  625. args[1] = op
  626. args[2] = destKey
  627. for i, key := range keys {
  628. args[3+i] = key
  629. }
  630. cmd := NewIntCmd(args...)
  631. _ = c(cmd)
  632. return cmd
  633. }
  634. func (c cmdable) BitOpAnd(destKey string, keys ...string) *IntCmd {
  635. return c.bitOp("and", destKey, keys...)
  636. }
  637. func (c cmdable) BitOpOr(destKey string, keys ...string) *IntCmd {
  638. return c.bitOp("or", destKey, keys...)
  639. }
  640. func (c cmdable) BitOpXor(destKey string, keys ...string) *IntCmd {
  641. return c.bitOp("xor", destKey, keys...)
  642. }
  643. func (c cmdable) BitOpNot(destKey string, key string) *IntCmd {
  644. return c.bitOp("not", destKey, key)
  645. }
  646. func (c cmdable) BitPos(key string, bit int64, pos ...int64) *IntCmd {
  647. args := make([]interface{}, 3+len(pos))
  648. args[0] = "bitpos"
  649. args[1] = key
  650. args[2] = bit
  651. switch len(pos) {
  652. case 0:
  653. case 1:
  654. args[3] = pos[0]
  655. case 2:
  656. args[3] = pos[0]
  657. args[4] = pos[1]
  658. default:
  659. panic("too many arguments")
  660. }
  661. cmd := NewIntCmd(args...)
  662. _ = c(cmd)
  663. return cmd
  664. }
  665. func (c cmdable) BitField(key string, args ...interface{}) *IntSliceCmd {
  666. a := make([]interface{}, 0, 2+len(args))
  667. a = append(a, "bitfield")
  668. a = append(a, key)
  669. a = append(a, args...)
  670. cmd := NewIntSliceCmd(a...)
  671. _ = c(cmd)
  672. return cmd
  673. }
  674. func (c cmdable) Decr(key string) *IntCmd {
  675. cmd := NewIntCmd("decr", key)
  676. _ = c(cmd)
  677. return cmd
  678. }
  679. func (c cmdable) DecrBy(key string, decrement int64) *IntCmd {
  680. cmd := NewIntCmd("decrby", key, decrement)
  681. _ = c(cmd)
  682. return cmd
  683. }
  684. // Redis `GET key` command. It returns redis.Nil error when key does not exist.
  685. func (c cmdable) Get(key string) *StringCmd {
  686. cmd := NewStringCmd("get", key)
  687. _ = c(cmd)
  688. return cmd
  689. }
  690. func (c cmdable) GetBit(key string, offset int64) *IntCmd {
  691. cmd := NewIntCmd("getbit", key, offset)
  692. _ = c(cmd)
  693. return cmd
  694. }
  695. func (c cmdable) GetRange(key string, start, end int64) *StringCmd {
  696. cmd := NewStringCmd("getrange", key, start, end)
  697. _ = c(cmd)
  698. return cmd
  699. }
  700. func (c cmdable) GetSet(key string, value interface{}) *StringCmd {
  701. cmd := NewStringCmd("getset", key, value)
  702. _ = c(cmd)
  703. return cmd
  704. }
  705. func (c cmdable) Incr(key string) *IntCmd {
  706. cmd := NewIntCmd("incr", key)
  707. _ = c(cmd)
  708. return cmd
  709. }
  710. func (c cmdable) IncrBy(key string, value int64) *IntCmd {
  711. cmd := NewIntCmd("incrby", key, value)
  712. _ = c(cmd)
  713. return cmd
  714. }
  715. func (c cmdable) IncrByFloat(key string, value float64) *FloatCmd {
  716. cmd := NewFloatCmd("incrbyfloat", key, value)
  717. _ = c(cmd)
  718. return cmd
  719. }
  720. func (c cmdable) MGet(keys ...string) *SliceCmd {
  721. args := make([]interface{}, 1+len(keys))
  722. args[0] = "mget"
  723. for i, key := range keys {
  724. args[1+i] = key
  725. }
  726. cmd := NewSliceCmd(args...)
  727. _ = c(cmd)
  728. return cmd
  729. }
  730. // MSet is like Set but accepts multiple values:
  731. // - MSet("key1", "value1", "key2", "value2")
  732. // - MSet([]string{"key1", "value1", "key2", "value2"})
  733. // - MSet(map[string]interface{}{"key1": "value1", "key2": "value2"})
  734. func (c cmdable) MSet(values ...interface{}) *StatusCmd {
  735. args := make([]interface{}, 1, 1+len(values))
  736. args[0] = "mset"
  737. args = appendArgs(args, values)
  738. cmd := NewStatusCmd(args...)
  739. _ = c(cmd)
  740. return cmd
  741. }
  742. // MSetNX is like SetNX but accepts multiple values:
  743. // - MSetNX("key1", "value1", "key2", "value2")
  744. // - MSetNX([]string{"key1", "value1", "key2", "value2"})
  745. // - MSetNX(map[string]interface{}{"key1": "value1", "key2": "value2"})
  746. func (c cmdable) MSetNX(values ...interface{}) *BoolCmd {
  747. args := make([]interface{}, 1, 1+len(values))
  748. args[0] = "msetnx"
  749. args = appendArgs(args, values)
  750. cmd := NewBoolCmd(args...)
  751. _ = c(cmd)
  752. return cmd
  753. }
  754. // Redis `SET key value [expiration]` command.
  755. //
  756. // Use expiration for `SETEX`-like behavior.
  757. // Zero expiration means the key has no expiration time.
  758. func (c cmdable) Set(key string, value interface{}, expiration time.Duration) *StatusCmd {
  759. args := make([]interface{}, 3, 5)
  760. args[0] = "set"
  761. args[1] = key
  762. args[2] = value
  763. if expiration > 0 {
  764. if usePrecise(expiration) {
  765. args = append(args, "px", formatMs(expiration))
  766. } else {
  767. args = append(args, "ex", formatSec(expiration))
  768. }
  769. }
  770. cmd := NewStatusCmd(args...)
  771. _ = c(cmd)
  772. return cmd
  773. }
  774. func (c cmdable) SetBit(key string, offset int64, value int) *IntCmd {
  775. cmd := NewIntCmd(
  776. "setbit",
  777. key,
  778. offset,
  779. value,
  780. )
  781. _ = c(cmd)
  782. return cmd
  783. }
  784. // Redis `SET key value [expiration] NX` command.
  785. //
  786. // Zero expiration means the key has no expiration time.
  787. func (c cmdable) SetNX(key string, value interface{}, expiration time.Duration) *BoolCmd {
  788. var cmd *BoolCmd
  789. if expiration == 0 {
  790. // Use old `SETNX` to support old Redis versions.
  791. cmd = NewBoolCmd("setnx", key, value)
  792. } else {
  793. if usePrecise(expiration) {
  794. cmd = NewBoolCmd("set", key, value, "px", formatMs(expiration), "nx")
  795. } else {
  796. cmd = NewBoolCmd("set", key, value, "ex", formatSec(expiration), "nx")
  797. }
  798. }
  799. _ = c(cmd)
  800. return cmd
  801. }
  802. // Redis `SET key value [expiration] XX` command.
  803. //
  804. // Zero expiration means the key has no expiration time.
  805. func (c cmdable) SetXX(key string, value interface{}, expiration time.Duration) *BoolCmd {
  806. var cmd *BoolCmd
  807. if expiration == 0 {
  808. cmd = NewBoolCmd("set", key, value, "xx")
  809. } else {
  810. if usePrecise(expiration) {
  811. cmd = NewBoolCmd("set", key, value, "px", formatMs(expiration), "xx")
  812. } else {
  813. cmd = NewBoolCmd("set", key, value, "ex", formatSec(expiration), "xx")
  814. }
  815. }
  816. _ = c(cmd)
  817. return cmd
  818. }
  819. func (c cmdable) SetRange(key string, offset int64, value string) *IntCmd {
  820. cmd := NewIntCmd("setrange", key, offset, value)
  821. _ = c(cmd)
  822. return cmd
  823. }
  824. func (c cmdable) StrLen(key string) *IntCmd {
  825. cmd := NewIntCmd("strlen", key)
  826. _ = c(cmd)
  827. return cmd
  828. }
  829. //------------------------------------------------------------------------------
  830. func (c cmdable) HDel(key string, fields ...string) *IntCmd {
  831. args := make([]interface{}, 2+len(fields))
  832. args[0] = "hdel"
  833. args[1] = key
  834. for i, field := range fields {
  835. args[2+i] = field
  836. }
  837. cmd := NewIntCmd(args...)
  838. _ = c(cmd)
  839. return cmd
  840. }
  841. func (c cmdable) HExists(key, field string) *BoolCmd {
  842. cmd := NewBoolCmd("hexists", key, field)
  843. _ = c(cmd)
  844. return cmd
  845. }
  846. func (c cmdable) HGet(key, field string) *StringCmd {
  847. cmd := NewStringCmd("hget", key, field)
  848. _ = c(cmd)
  849. return cmd
  850. }
  851. func (c cmdable) HGetAll(key string) *StringStringMapCmd {
  852. cmd := NewStringStringMapCmd("hgetall", key)
  853. _ = c(cmd)
  854. return cmd
  855. }
  856. func (c cmdable) HIncrBy(key, field string, incr int64) *IntCmd {
  857. cmd := NewIntCmd("hincrby", key, field, incr)
  858. _ = c(cmd)
  859. return cmd
  860. }
  861. func (c cmdable) HIncrByFloat(key, field string, incr float64) *FloatCmd {
  862. cmd := NewFloatCmd("hincrbyfloat", key, field, incr)
  863. _ = c(cmd)
  864. return cmd
  865. }
  866. func (c cmdable) HKeys(key string) *StringSliceCmd {
  867. cmd := NewStringSliceCmd("hkeys", key)
  868. _ = c(cmd)
  869. return cmd
  870. }
  871. func (c cmdable) HLen(key string) *IntCmd {
  872. cmd := NewIntCmd("hlen", key)
  873. _ = c(cmd)
  874. return cmd
  875. }
  876. // HMGet returns the values for the specified fields in the hash stored at key.
  877. // It returns an interface{} to distinguish between empty string and nil value.
  878. func (c cmdable) HMGet(key string, fields ...string) *SliceCmd {
  879. args := make([]interface{}, 2+len(fields))
  880. args[0] = "hmget"
  881. args[1] = key
  882. for i, field := range fields {
  883. args[2+i] = field
  884. }
  885. cmd := NewSliceCmd(args...)
  886. _ = c(cmd)
  887. return cmd
  888. }
  889. // HMSet is like HSet, but accepts multiple values:
  890. // - HMSet("myhash", "key1", "value1", "key2", "value2")
  891. // - HMSet("myhash", []string{"key1", "value1", "key2", "value2"})
  892. // - HMSet("myhash", map[string]interface{}{"key1": "value1", "key2": "value2"})
  893. //
  894. // Note that it uses HSET Redis command underneath because HMSET is deprecated.
  895. func (c cmdable) HMSet(key string, values ...interface{}) *IntCmd {
  896. args := make([]interface{}, 2, 2+len(values))
  897. args[0] = "hset"
  898. args[1] = key
  899. args = appendArgs(args, values)
  900. cmd := NewIntCmd(args...)
  901. _ = c(cmd)
  902. return cmd
  903. }
  904. func (c cmdable) HSet(key, field string, value interface{}) *BoolCmd {
  905. cmd := NewBoolCmd("hset", key, field, value)
  906. _ = c(cmd)
  907. return cmd
  908. }
  909. func (c cmdable) HSetNX(key, field string, value interface{}) *BoolCmd {
  910. cmd := NewBoolCmd("hsetnx", key, field, value)
  911. _ = c(cmd)
  912. return cmd
  913. }
  914. func (c cmdable) HVals(key string) *StringSliceCmd {
  915. cmd := NewStringSliceCmd("hvals", key)
  916. _ = c(cmd)
  917. return cmd
  918. }
  919. //------------------------------------------------------------------------------
  920. func (c cmdable) BLPop(timeout time.Duration, keys ...string) *StringSliceCmd {
  921. args := make([]interface{}, 1+len(keys)+1)
  922. args[0] = "blpop"
  923. for i, key := range keys {
  924. args[1+i] = key
  925. }
  926. args[len(args)-1] = formatSec(timeout)
  927. cmd := NewStringSliceCmd(args...)
  928. cmd.setReadTimeout(timeout)
  929. _ = c(cmd)
  930. return cmd
  931. }
  932. func (c cmdable) BRPop(timeout time.Duration, keys ...string) *StringSliceCmd {
  933. args := make([]interface{}, 1+len(keys)+1)
  934. args[0] = "brpop"
  935. for i, key := range keys {
  936. args[1+i] = key
  937. }
  938. args[len(keys)+1] = formatSec(timeout)
  939. cmd := NewStringSliceCmd(args...)
  940. cmd.setReadTimeout(timeout)
  941. _ = c(cmd)
  942. return cmd
  943. }
  944. func (c cmdable) BRPopLPush(source, destination string, timeout time.Duration) *StringCmd {
  945. cmd := NewStringCmd(
  946. "brpoplpush",
  947. source,
  948. destination,
  949. formatSec(timeout),
  950. )
  951. cmd.setReadTimeout(timeout)
  952. _ = c(cmd)
  953. return cmd
  954. }
  955. func (c cmdable) LIndex(key string, index int64) *StringCmd {
  956. cmd := NewStringCmd("lindex", key, index)
  957. _ = c(cmd)
  958. return cmd
  959. }
  960. func (c cmdable) LInsert(key, op string, pivot, value interface{}) *IntCmd {
  961. cmd := NewIntCmd("linsert", key, op, pivot, value)
  962. _ = c(cmd)
  963. return cmd
  964. }
  965. func (c cmdable) LInsertBefore(key string, pivot, value interface{}) *IntCmd {
  966. cmd := NewIntCmd("linsert", key, "before", pivot, value)
  967. _ = c(cmd)
  968. return cmd
  969. }
  970. func (c cmdable) LInsertAfter(key string, pivot, value interface{}) *IntCmd {
  971. cmd := NewIntCmd("linsert", key, "after", pivot, value)
  972. _ = c(cmd)
  973. return cmd
  974. }
  975. func (c cmdable) LLen(key string) *IntCmd {
  976. cmd := NewIntCmd("llen", key)
  977. _ = c(cmd)
  978. return cmd
  979. }
  980. func (c cmdable) LPop(key string) *StringCmd {
  981. cmd := NewStringCmd("lpop", key)
  982. _ = c(cmd)
  983. return cmd
  984. }
  985. func (c cmdable) LPush(key string, values ...interface{}) *IntCmd {
  986. args := make([]interface{}, 2, 2+len(values))
  987. args[0] = "lpush"
  988. args[1] = key
  989. args = appendArgs(args, values)
  990. cmd := NewIntCmd(args...)
  991. _ = c(cmd)
  992. return cmd
  993. }
  994. func (c cmdable) LPushX(key string, values ...interface{}) *IntCmd {
  995. args := make([]interface{}, 2, 2+len(values))
  996. args[0] = "lpushx"
  997. args[1] = key
  998. args = appendArgs(args, values)
  999. cmd := NewIntCmd(args...)
  1000. _ = c(cmd)
  1001. return cmd
  1002. }
  1003. func (c cmdable) LRange(key string, start, stop int64) *StringSliceCmd {
  1004. cmd := NewStringSliceCmd(
  1005. "lrange",
  1006. key,
  1007. start,
  1008. stop,
  1009. )
  1010. _ = c(cmd)
  1011. return cmd
  1012. }
  1013. func (c cmdable) LRem(key string, count int64, value interface{}) *IntCmd {
  1014. cmd := NewIntCmd("lrem", key, count, value)
  1015. _ = c(cmd)
  1016. return cmd
  1017. }
  1018. func (c cmdable) LSet(key string, index int64, value interface{}) *StatusCmd {
  1019. cmd := NewStatusCmd("lset", key, index, value)
  1020. _ = c(cmd)
  1021. return cmd
  1022. }
  1023. func (c cmdable) LTrim(key string, start, stop int64) *StatusCmd {
  1024. cmd := NewStatusCmd(
  1025. "ltrim",
  1026. key,
  1027. start,
  1028. stop,
  1029. )
  1030. _ = c(cmd)
  1031. return cmd
  1032. }
  1033. func (c cmdable) RPop(key string) *StringCmd {
  1034. cmd := NewStringCmd("rpop", key)
  1035. _ = c(cmd)
  1036. return cmd
  1037. }
  1038. func (c cmdable) RPopLPush(source, destination string) *StringCmd {
  1039. cmd := NewStringCmd("rpoplpush", source, destination)
  1040. _ = c(cmd)
  1041. return cmd
  1042. }
  1043. func (c cmdable) RPush(key string, values ...interface{}) *IntCmd {
  1044. args := make([]interface{}, 2, 2+len(values))
  1045. args[0] = "rpush"
  1046. args[1] = key
  1047. args = appendArgs(args, values)
  1048. cmd := NewIntCmd(args...)
  1049. _ = c(cmd)
  1050. return cmd
  1051. }
  1052. func (c cmdable) RPushX(key string, values ...interface{}) *IntCmd {
  1053. args := make([]interface{}, 2, 2+len(values))
  1054. args[0] = "rpushx"
  1055. args[1] = key
  1056. args = appendArgs(args, values)
  1057. cmd := NewIntCmd(args...)
  1058. _ = c(cmd)
  1059. return cmd
  1060. }
  1061. //------------------------------------------------------------------------------
  1062. func (c cmdable) SAdd(key string, members ...interface{}) *IntCmd {
  1063. args := make([]interface{}, 2, 2+len(members))
  1064. args[0] = "sadd"
  1065. args[1] = key
  1066. args = appendArgs(args, members)
  1067. cmd := NewIntCmd(args...)
  1068. _ = c(cmd)
  1069. return cmd
  1070. }
  1071. func (c cmdable) SCard(key string) *IntCmd {
  1072. cmd := NewIntCmd("scard", key)
  1073. _ = c(cmd)
  1074. return cmd
  1075. }
  1076. func (c cmdable) SDiff(keys ...string) *StringSliceCmd {
  1077. args := make([]interface{}, 1+len(keys))
  1078. args[0] = "sdiff"
  1079. for i, key := range keys {
  1080. args[1+i] = key
  1081. }
  1082. cmd := NewStringSliceCmd(args...)
  1083. _ = c(cmd)
  1084. return cmd
  1085. }
  1086. func (c cmdable) SDiffStore(destination string, keys ...string) *IntCmd {
  1087. args := make([]interface{}, 2+len(keys))
  1088. args[0] = "sdiffstore"
  1089. args[1] = destination
  1090. for i, key := range keys {
  1091. args[2+i] = key
  1092. }
  1093. cmd := NewIntCmd(args...)
  1094. _ = c(cmd)
  1095. return cmd
  1096. }
  1097. func (c cmdable) SInter(keys ...string) *StringSliceCmd {
  1098. args := make([]interface{}, 1+len(keys))
  1099. args[0] = "sinter"
  1100. for i, key := range keys {
  1101. args[1+i] = key
  1102. }
  1103. cmd := NewStringSliceCmd(args...)
  1104. _ = c(cmd)
  1105. return cmd
  1106. }
  1107. func (c cmdable) SInterStore(destination string, keys ...string) *IntCmd {
  1108. args := make([]interface{}, 2+len(keys))
  1109. args[0] = "sinterstore"
  1110. args[1] = destination
  1111. for i, key := range keys {
  1112. args[2+i] = key
  1113. }
  1114. cmd := NewIntCmd(args...)
  1115. _ = c(cmd)
  1116. return cmd
  1117. }
  1118. func (c cmdable) SIsMember(key string, member interface{}) *BoolCmd {
  1119. cmd := NewBoolCmd("sismember", key, member)
  1120. _ = c(cmd)
  1121. return cmd
  1122. }
  1123. // Redis `SMEMBERS key` command output as a slice
  1124. func (c cmdable) SMembers(key string) *StringSliceCmd {
  1125. cmd := NewStringSliceCmd("smembers", key)
  1126. _ = c(cmd)
  1127. return cmd
  1128. }
  1129. // Redis `SMEMBERS key` command output as a map
  1130. func (c cmdable) SMembersMap(key string) *StringStructMapCmd {
  1131. cmd := NewStringStructMapCmd("smembers", key)
  1132. _ = c(cmd)
  1133. return cmd
  1134. }
  1135. func (c cmdable) SMove(source, destination string, member interface{}) *BoolCmd {
  1136. cmd := NewBoolCmd("smove", source, destination, member)
  1137. _ = c(cmd)
  1138. return cmd
  1139. }
  1140. // Redis `SPOP key` command.
  1141. func (c cmdable) SPop(key string) *StringCmd {
  1142. cmd := NewStringCmd("spop", key)
  1143. _ = c(cmd)
  1144. return cmd
  1145. }
  1146. // Redis `SPOP key count` command.
  1147. func (c cmdable) SPopN(key string, count int64) *StringSliceCmd {
  1148. cmd := NewStringSliceCmd("spop", key, count)
  1149. _ = c(cmd)
  1150. return cmd
  1151. }
  1152. // Redis `SRANDMEMBER key` command.
  1153. func (c cmdable) SRandMember(key string) *StringCmd {
  1154. cmd := NewStringCmd("srandmember", key)
  1155. _ = c(cmd)
  1156. return cmd
  1157. }
  1158. // Redis `SRANDMEMBER key count` command.
  1159. func (c cmdable) SRandMemberN(key string, count int64) *StringSliceCmd {
  1160. cmd := NewStringSliceCmd("srandmember", key, count)
  1161. _ = c(cmd)
  1162. return cmd
  1163. }
  1164. func (c cmdable) SRem(key string, members ...interface{}) *IntCmd {
  1165. args := make([]interface{}, 2, 2+len(members))
  1166. args[0] = "srem"
  1167. args[1] = key
  1168. args = appendArgs(args, members)
  1169. cmd := NewIntCmd(args...)
  1170. _ = c(cmd)
  1171. return cmd
  1172. }
  1173. func (c cmdable) SUnion(keys ...string) *StringSliceCmd {
  1174. args := make([]interface{}, 1+len(keys))
  1175. args[0] = "sunion"
  1176. for i, key := range keys {
  1177. args[1+i] = key
  1178. }
  1179. cmd := NewStringSliceCmd(args...)
  1180. _ = c(cmd)
  1181. return cmd
  1182. }
  1183. func (c cmdable) SUnionStore(destination string, keys ...string) *IntCmd {
  1184. args := make([]interface{}, 2+len(keys))
  1185. args[0] = "sunionstore"
  1186. args[1] = destination
  1187. for i, key := range keys {
  1188. args[2+i] = key
  1189. }
  1190. cmd := NewIntCmd(args...)
  1191. _ = c(cmd)
  1192. return cmd
  1193. }
  1194. //------------------------------------------------------------------------------
  1195. type XAddArgs struct {
  1196. Stream string
  1197. MaxLen int64 // MAXLEN N
  1198. MaxLenApprox int64 // MAXLEN ~ N
  1199. ID string
  1200. Values map[string]interface{}
  1201. }
  1202. func (c cmdable) XAdd(a *XAddArgs) *StringCmd {
  1203. args := make([]interface{}, 0, 6+len(a.Values)*2)
  1204. args = append(args, "xadd")
  1205. args = append(args, a.Stream)
  1206. if a.MaxLen > 0 {
  1207. args = append(args, "maxlen", a.MaxLen)
  1208. } else if a.MaxLenApprox > 0 {
  1209. args = append(args, "maxlen", "~", a.MaxLenApprox)
  1210. }
  1211. if a.ID != "" {
  1212. args = append(args, a.ID)
  1213. } else {
  1214. args = append(args, "*")
  1215. }
  1216. for k, v := range a.Values {
  1217. args = append(args, k)
  1218. args = append(args, v)
  1219. }
  1220. cmd := NewStringCmd(args...)
  1221. _ = c(cmd)
  1222. return cmd
  1223. }
  1224. func (c cmdable) XDel(stream string, ids ...string) *IntCmd {
  1225. args := []interface{}{"xdel", stream}
  1226. for _, id := range ids {
  1227. args = append(args, id)
  1228. }
  1229. cmd := NewIntCmd(args...)
  1230. _ = c(cmd)
  1231. return cmd
  1232. }
  1233. func (c cmdable) XLen(stream string) *IntCmd {
  1234. cmd := NewIntCmd("xlen", stream)
  1235. _ = c(cmd)
  1236. return cmd
  1237. }
  1238. func (c cmdable) XRange(stream, start, stop string) *XMessageSliceCmd {
  1239. cmd := NewXMessageSliceCmd("xrange", stream, start, stop)
  1240. _ = c(cmd)
  1241. return cmd
  1242. }
  1243. func (c cmdable) XRangeN(stream, start, stop string, count int64) *XMessageSliceCmd {
  1244. cmd := NewXMessageSliceCmd("xrange", stream, start, stop, "count", count)
  1245. _ = c(cmd)
  1246. return cmd
  1247. }
  1248. func (c cmdable) XRevRange(stream, start, stop string) *XMessageSliceCmd {
  1249. cmd := NewXMessageSliceCmd("xrevrange", stream, start, stop)
  1250. _ = c(cmd)
  1251. return cmd
  1252. }
  1253. func (c cmdable) XRevRangeN(stream, start, stop string, count int64) *XMessageSliceCmd {
  1254. cmd := NewXMessageSliceCmd("xrevrange", stream, start, stop, "count", count)
  1255. _ = c(cmd)
  1256. return cmd
  1257. }
  1258. type XReadArgs struct {
  1259. Streams []string // list of streams and ids, e.g. stream1 stream2 id1 id2
  1260. Count int64
  1261. Block time.Duration
  1262. }
  1263. func (c cmdable) XRead(a *XReadArgs) *XStreamSliceCmd {
  1264. args := make([]interface{}, 0, 5+len(a.Streams))
  1265. args = append(args, "xread")
  1266. if a.Count > 0 {
  1267. args = append(args, "count")
  1268. args = append(args, a.Count)
  1269. }
  1270. if a.Block >= 0 {
  1271. args = append(args, "block")
  1272. args = append(args, int64(a.Block/time.Millisecond))
  1273. }
  1274. args = append(args, "streams")
  1275. for _, s := range a.Streams {
  1276. args = append(args, s)
  1277. }
  1278. cmd := NewXStreamSliceCmd(args...)
  1279. if a.Block >= 0 {
  1280. cmd.setReadTimeout(a.Block)
  1281. }
  1282. _ = c(cmd)
  1283. return cmd
  1284. }
  1285. func (c cmdable) XReadStreams(streams ...string) *XStreamSliceCmd {
  1286. return c.XRead(&XReadArgs{
  1287. Streams: streams,
  1288. Block: -1,
  1289. })
  1290. }
  1291. func (c cmdable) XGroupCreate(stream, group, start string) *StatusCmd {
  1292. cmd := NewStatusCmd("xgroup", "create", stream, group, start)
  1293. _ = c(cmd)
  1294. return cmd
  1295. }
  1296. func (c cmdable) XGroupCreateMkStream(stream, group, start string) *StatusCmd {
  1297. cmd := NewStatusCmd("xgroup", "create", stream, group, start, "mkstream")
  1298. _ = c(cmd)
  1299. return cmd
  1300. }
  1301. func (c cmdable) XGroupSetID(stream, group, start string) *StatusCmd {
  1302. cmd := NewStatusCmd("xgroup", "setid", stream, group, start)
  1303. _ = c(cmd)
  1304. return cmd
  1305. }
  1306. func (c cmdable) XGroupDestroy(stream, group string) *IntCmd {
  1307. cmd := NewIntCmd("xgroup", "destroy", stream, group)
  1308. _ = c(cmd)
  1309. return cmd
  1310. }
  1311. func (c cmdable) XGroupDelConsumer(stream, group, consumer string) *IntCmd {
  1312. cmd := NewIntCmd("xgroup", "delconsumer", stream, group, consumer)
  1313. _ = c(cmd)
  1314. return cmd
  1315. }
  1316. type XReadGroupArgs struct {
  1317. Group string
  1318. Consumer string
  1319. Streams []string // list of streams and ids, e.g. stream1 stream2 id1 id2
  1320. Count int64
  1321. Block time.Duration
  1322. NoAck bool
  1323. }
  1324. func (c cmdable) XReadGroup(a *XReadGroupArgs) *XStreamSliceCmd {
  1325. args := make([]interface{}, 0, 8+len(a.Streams))
  1326. args = append(args, "xreadgroup", "group", a.Group, a.Consumer)
  1327. if a.Count > 0 {
  1328. args = append(args, "count", a.Count)
  1329. }
  1330. if a.Block >= 0 {
  1331. args = append(args, "block", int64(a.Block/time.Millisecond))
  1332. }
  1333. if a.NoAck {
  1334. args = append(args, "noack")
  1335. }
  1336. args = append(args, "streams")
  1337. for _, s := range a.Streams {
  1338. args = append(args, s)
  1339. }
  1340. cmd := NewXStreamSliceCmd(args...)
  1341. if a.Block >= 0 {
  1342. cmd.setReadTimeout(a.Block)
  1343. }
  1344. _ = c(cmd)
  1345. return cmd
  1346. }
  1347. func (c cmdable) XAck(stream, group string, ids ...string) *IntCmd {
  1348. args := []interface{}{"xack", stream, group}
  1349. for _, id := range ids {
  1350. args = append(args, id)
  1351. }
  1352. cmd := NewIntCmd(args...)
  1353. _ = c(cmd)
  1354. return cmd
  1355. }
  1356. func (c cmdable) XPending(stream, group string) *XPendingCmd {
  1357. cmd := NewXPendingCmd("xpending", stream, group)
  1358. _ = c(cmd)
  1359. return cmd
  1360. }
  1361. type XPendingExtArgs struct {
  1362. Stream string
  1363. Group string
  1364. Start string
  1365. End string
  1366. Count int64
  1367. Consumer string
  1368. }
  1369. func (c cmdable) XPendingExt(a *XPendingExtArgs) *XPendingExtCmd {
  1370. args := make([]interface{}, 0, 7)
  1371. args = append(args, "xpending", a.Stream, a.Group, a.Start, a.End, a.Count)
  1372. if a.Consumer != "" {
  1373. args = append(args, a.Consumer)
  1374. }
  1375. cmd := NewXPendingExtCmd(args...)
  1376. _ = c(cmd)
  1377. return cmd
  1378. }
  1379. type XClaimArgs struct {
  1380. Stream string
  1381. Group string
  1382. Consumer string
  1383. MinIdle time.Duration
  1384. Messages []string
  1385. }
  1386. func (c cmdable) XClaim(a *XClaimArgs) *XMessageSliceCmd {
  1387. args := xClaimArgs(a)
  1388. cmd := NewXMessageSliceCmd(args...)
  1389. _ = c(cmd)
  1390. return cmd
  1391. }
  1392. func (c cmdable) XClaimJustID(a *XClaimArgs) *StringSliceCmd {
  1393. args := xClaimArgs(a)
  1394. args = append(args, "justid")
  1395. cmd := NewStringSliceCmd(args...)
  1396. _ = c(cmd)
  1397. return cmd
  1398. }
  1399. func xClaimArgs(a *XClaimArgs) []interface{} {
  1400. args := make([]interface{}, 0, 4+len(a.Messages))
  1401. args = append(args,
  1402. "xclaim",
  1403. a.Stream,
  1404. a.Group, a.Consumer,
  1405. int64(a.MinIdle/time.Millisecond))
  1406. for _, id := range a.Messages {
  1407. args = append(args, id)
  1408. }
  1409. return args
  1410. }
  1411. func (c cmdable) XTrim(key string, maxLen int64) *IntCmd {
  1412. cmd := NewIntCmd("xtrim", key, "maxlen", maxLen)
  1413. _ = c(cmd)
  1414. return cmd
  1415. }
  1416. func (c cmdable) XTrimApprox(key string, maxLen int64) *IntCmd {
  1417. cmd := NewIntCmd("xtrim", key, "maxlen", "~", maxLen)
  1418. _ = c(cmd)
  1419. return cmd
  1420. }
  1421. func (c cmdable) XInfoGroups(key string) *XInfoGroupsCmd {
  1422. cmd := NewXInfoGroupsCmd(key)
  1423. _ = c(cmd)
  1424. return cmd
  1425. }
  1426. //------------------------------------------------------------------------------
  1427. // Z represents sorted set member.
  1428. type Z struct {
  1429. Score float64
  1430. Member interface{}
  1431. }
  1432. // ZWithKey represents sorted set member including the name of the key where it was popped.
  1433. type ZWithKey struct {
  1434. Z
  1435. Key string
  1436. }
  1437. // ZStore is used as an arg to ZInterStore and ZUnionStore.
  1438. type ZStore struct {
  1439. Keys []string
  1440. Weights []float64
  1441. // Can be SUM, MIN or MAX.
  1442. Aggregate string
  1443. }
  1444. // Redis `BZPOPMAX key [key ...] timeout` command.
  1445. func (c cmdable) BZPopMax(timeout time.Duration, keys ...string) *ZWithKeyCmd {
  1446. args := make([]interface{}, 1+len(keys)+1)
  1447. args[0] = "bzpopmax"
  1448. for i, key := range keys {
  1449. args[1+i] = key
  1450. }
  1451. args[len(args)-1] = formatSec(timeout)
  1452. cmd := NewZWithKeyCmd(args...)
  1453. cmd.setReadTimeout(timeout)
  1454. _ = c(cmd)
  1455. return cmd
  1456. }
  1457. // Redis `BZPOPMIN key [key ...] timeout` command.
  1458. func (c cmdable) BZPopMin(timeout time.Duration, keys ...string) *ZWithKeyCmd {
  1459. args := make([]interface{}, 1+len(keys)+1)
  1460. args[0] = "bzpopmin"
  1461. for i, key := range keys {
  1462. args[1+i] = key
  1463. }
  1464. args[len(args)-1] = formatSec(timeout)
  1465. cmd := NewZWithKeyCmd(args...)
  1466. cmd.setReadTimeout(timeout)
  1467. _ = c(cmd)
  1468. return cmd
  1469. }
  1470. func (c cmdable) zAdd(a []interface{}, n int, members ...*Z) *IntCmd {
  1471. for i, m := range members {
  1472. a[n+2*i] = m.Score
  1473. a[n+2*i+1] = m.Member
  1474. }
  1475. cmd := NewIntCmd(a...)
  1476. _ = c(cmd)
  1477. return cmd
  1478. }
  1479. // Redis `ZADD key score member [score member ...]` command.
  1480. func (c cmdable) ZAdd(key string, members ...*Z) *IntCmd {
  1481. const n = 2
  1482. a := make([]interface{}, n+2*len(members))
  1483. a[0], a[1] = "zadd", key
  1484. return c.zAdd(a, n, members...)
  1485. }
  1486. // Redis `ZADD key NX score member [score member ...]` command.
  1487. func (c cmdable) ZAddNX(key string, members ...*Z) *IntCmd {
  1488. const n = 3
  1489. a := make([]interface{}, n+2*len(members))
  1490. a[0], a[1], a[2] = "zadd", key, "nx"
  1491. return c.zAdd(a, n, members...)
  1492. }
  1493. // Redis `ZADD key XX score member [score member ...]` command.
  1494. func (c cmdable) ZAddXX(key string, members ...*Z) *IntCmd {
  1495. const n = 3
  1496. a := make([]interface{}, n+2*len(members))
  1497. a[0], a[1], a[2] = "zadd", key, "xx"
  1498. return c.zAdd(a, n, members...)
  1499. }
  1500. // Redis `ZADD key CH score member [score member ...]` command.
  1501. func (c cmdable) ZAddCh(key string, members ...*Z) *IntCmd {
  1502. const n = 3
  1503. a := make([]interface{}, n+2*len(members))
  1504. a[0], a[1], a[2] = "zadd", key, "ch"
  1505. return c.zAdd(a, n, members...)
  1506. }
  1507. // Redis `ZADD key NX CH score member [score member ...]` command.
  1508. func (c cmdable) ZAddNXCh(key string, members ...*Z) *IntCmd {
  1509. const n = 4
  1510. a := make([]interface{}, n+2*len(members))
  1511. a[0], a[1], a[2], a[3] = "zadd", key, "nx", "ch"
  1512. return c.zAdd(a, n, members...)
  1513. }
  1514. // Redis `ZADD key XX CH score member [score member ...]` command.
  1515. func (c cmdable) ZAddXXCh(key string, members ...*Z) *IntCmd {
  1516. const n = 4
  1517. a := make([]interface{}, n+2*len(members))
  1518. a[0], a[1], a[2], a[3] = "zadd", key, "xx", "ch"
  1519. return c.zAdd(a, n, members...)
  1520. }
  1521. func (c cmdable) zIncr(a []interface{}, n int, members ...*Z) *FloatCmd {
  1522. for i, m := range members {
  1523. a[n+2*i] = m.Score
  1524. a[n+2*i+1] = m.Member
  1525. }
  1526. cmd := NewFloatCmd(a...)
  1527. _ = c(cmd)
  1528. return cmd
  1529. }
  1530. // Redis `ZADD key INCR score member` command.
  1531. func (c cmdable) ZIncr(key string, member *Z) *FloatCmd {
  1532. const n = 3
  1533. a := make([]interface{}, n+2)
  1534. a[0], a[1], a[2] = "zadd", key, "incr"
  1535. return c.zIncr(a, n, member)
  1536. }
  1537. // Redis `ZADD key NX INCR score member` command.
  1538. func (c cmdable) ZIncrNX(key string, member *Z) *FloatCmd {
  1539. const n = 4
  1540. a := make([]interface{}, n+2)
  1541. a[0], a[1], a[2], a[3] = "zadd", key, "incr", "nx"
  1542. return c.zIncr(a, n, member)
  1543. }
  1544. // Redis `ZADD key XX INCR score member` command.
  1545. func (c cmdable) ZIncrXX(key string, member *Z) *FloatCmd {
  1546. const n = 4
  1547. a := make([]interface{}, n+2)
  1548. a[0], a[1], a[2], a[3] = "zadd", key, "incr", "xx"
  1549. return c.zIncr(a, n, member)
  1550. }
  1551. func (c cmdable) ZCard(key string) *IntCmd {
  1552. cmd := NewIntCmd("zcard", key)
  1553. _ = c(cmd)
  1554. return cmd
  1555. }
  1556. func (c cmdable) ZCount(key, min, max string) *IntCmd {
  1557. cmd := NewIntCmd("zcount", key, min, max)
  1558. _ = c(cmd)
  1559. return cmd
  1560. }
  1561. func (c cmdable) ZLexCount(key, min, max string) *IntCmd {
  1562. cmd := NewIntCmd("zlexcount", key, min, max)
  1563. _ = c(cmd)
  1564. return cmd
  1565. }
  1566. func (c cmdable) ZIncrBy(key string, increment float64, member string) *FloatCmd {
  1567. cmd := NewFloatCmd("zincrby", key, increment, member)
  1568. _ = c(cmd)
  1569. return cmd
  1570. }
  1571. func (c cmdable) ZInterStore(destination string, store *ZStore) *IntCmd {
  1572. args := make([]interface{}, 3+len(store.Keys))
  1573. args[0] = "zinterstore"
  1574. args[1] = destination
  1575. args[2] = len(store.Keys)
  1576. for i, key := range store.Keys {
  1577. args[3+i] = key
  1578. }
  1579. if len(store.Weights) > 0 {
  1580. args = append(args, "weights")
  1581. for _, weight := range store.Weights {
  1582. args = append(args, weight)
  1583. }
  1584. }
  1585. if store.Aggregate != "" {
  1586. args = append(args, "aggregate", store.Aggregate)
  1587. }
  1588. cmd := NewIntCmd(args...)
  1589. _ = c(cmd)
  1590. return cmd
  1591. }
  1592. func (c cmdable) ZPopMax(key string, count ...int64) *ZSliceCmd {
  1593. args := []interface{}{
  1594. "zpopmax",
  1595. key,
  1596. }
  1597. switch len(count) {
  1598. case 0:
  1599. break
  1600. case 1:
  1601. args = append(args, count[0])
  1602. default:
  1603. panic("too many arguments")
  1604. }
  1605. cmd := NewZSliceCmd(args...)
  1606. _ = c(cmd)
  1607. return cmd
  1608. }
  1609. func (c cmdable) ZPopMin(key string, count ...int64) *ZSliceCmd {
  1610. args := []interface{}{
  1611. "zpopmin",
  1612. key,
  1613. }
  1614. switch len(count) {
  1615. case 0:
  1616. break
  1617. case 1:
  1618. args = append(args, count[0])
  1619. default:
  1620. panic("too many arguments")
  1621. }
  1622. cmd := NewZSliceCmd(args...)
  1623. _ = c(cmd)
  1624. return cmd
  1625. }
  1626. func (c cmdable) zRange(key string, start, stop int64, withScores bool) *StringSliceCmd {
  1627. args := []interface{}{
  1628. "zrange",
  1629. key,
  1630. start,
  1631. stop,
  1632. }
  1633. if withScores {
  1634. args = append(args, "withscores")
  1635. }
  1636. cmd := NewStringSliceCmd(args...)
  1637. _ = c(cmd)
  1638. return cmd
  1639. }
  1640. func (c cmdable) ZRange(key string, start, stop int64) *StringSliceCmd {
  1641. return c.zRange(key, start, stop, false)
  1642. }
  1643. func (c cmdable) ZRangeWithScores(key string, start, stop int64) *ZSliceCmd {
  1644. cmd := NewZSliceCmd("zrange", key, start, stop, "withscores")
  1645. _ = c(cmd)
  1646. return cmd
  1647. }
  1648. type ZRangeBy struct {
  1649. Min, Max string
  1650. Offset, Count int64
  1651. }
  1652. func (c cmdable) zRangeBy(zcmd, key string, opt *ZRangeBy, withScores bool) *StringSliceCmd {
  1653. args := []interface{}{zcmd, key, opt.Min, opt.Max}
  1654. if withScores {
  1655. args = append(args, "withscores")
  1656. }
  1657. if opt.Offset != 0 || opt.Count != 0 {
  1658. args = append(
  1659. args,
  1660. "limit",
  1661. opt.Offset,
  1662. opt.Count,
  1663. )
  1664. }
  1665. cmd := NewStringSliceCmd(args...)
  1666. _ = c(cmd)
  1667. return cmd
  1668. }
  1669. func (c cmdable) ZRangeByScore(key string, opt *ZRangeBy) *StringSliceCmd {
  1670. return c.zRangeBy("zrangebyscore", key, opt, false)
  1671. }
  1672. func (c cmdable) ZRangeByLex(key string, opt *ZRangeBy) *StringSliceCmd {
  1673. return c.zRangeBy("zrangebylex", key, opt, false)
  1674. }
  1675. func (c cmdable) ZRangeByScoreWithScores(key string, opt *ZRangeBy) *ZSliceCmd {
  1676. args := []interface{}{"zrangebyscore", key, opt.Min, opt.Max, "withscores"}
  1677. if opt.Offset != 0 || opt.Count != 0 {
  1678. args = append(
  1679. args,
  1680. "limit",
  1681. opt.Offset,
  1682. opt.Count,
  1683. )
  1684. }
  1685. cmd := NewZSliceCmd(args...)
  1686. _ = c(cmd)
  1687. return cmd
  1688. }
  1689. func (c cmdable) ZRank(key, member string) *IntCmd {
  1690. cmd := NewIntCmd("zrank", key, member)
  1691. _ = c(cmd)
  1692. return cmd
  1693. }
  1694. func (c cmdable) ZRem(key string, members ...interface{}) *IntCmd {
  1695. args := make([]interface{}, 2, 2+len(members))
  1696. args[0] = "zrem"
  1697. args[1] = key
  1698. args = appendArgs(args, members)
  1699. cmd := NewIntCmd(args...)
  1700. _ = c(cmd)
  1701. return cmd
  1702. }
  1703. func (c cmdable) ZRemRangeByRank(key string, start, stop int64) *IntCmd {
  1704. cmd := NewIntCmd(
  1705. "zremrangebyrank",
  1706. key,
  1707. start,
  1708. stop,
  1709. )
  1710. _ = c(cmd)
  1711. return cmd
  1712. }
  1713. func (c cmdable) ZRemRangeByScore(key, min, max string) *IntCmd {
  1714. cmd := NewIntCmd("zremrangebyscore", key, min, max)
  1715. _ = c(cmd)
  1716. return cmd
  1717. }
  1718. func (c cmdable) ZRemRangeByLex(key, min, max string) *IntCmd {
  1719. cmd := NewIntCmd("zremrangebylex", key, min, max)
  1720. _ = c(cmd)
  1721. return cmd
  1722. }
  1723. func (c cmdable) ZRevRange(key string, start, stop int64) *StringSliceCmd {
  1724. cmd := NewStringSliceCmd("zrevrange", key, start, stop)
  1725. _ = c(cmd)
  1726. return cmd
  1727. }
  1728. func (c cmdable) ZRevRangeWithScores(key string, start, stop int64) *ZSliceCmd {
  1729. cmd := NewZSliceCmd("zrevrange", key, start, stop, "withscores")
  1730. _ = c(cmd)
  1731. return cmd
  1732. }
  1733. func (c cmdable) zRevRangeBy(zcmd, key string, opt *ZRangeBy) *StringSliceCmd {
  1734. args := []interface{}{zcmd, key, opt.Max, opt.Min}
  1735. if opt.Offset != 0 || opt.Count != 0 {
  1736. args = append(
  1737. args,
  1738. "limit",
  1739. opt.Offset,
  1740. opt.Count,
  1741. )
  1742. }
  1743. cmd := NewStringSliceCmd(args...)
  1744. _ = c(cmd)
  1745. return cmd
  1746. }
  1747. func (c cmdable) ZRevRangeByScore(key string, opt *ZRangeBy) *StringSliceCmd {
  1748. return c.zRevRangeBy("zrevrangebyscore", key, opt)
  1749. }
  1750. func (c cmdable) ZRevRangeByLex(key string, opt *ZRangeBy) *StringSliceCmd {
  1751. return c.zRevRangeBy("zrevrangebylex", key, opt)
  1752. }
  1753. func (c cmdable) ZRevRangeByScoreWithScores(key string, opt *ZRangeBy) *ZSliceCmd {
  1754. args := []interface{}{"zrevrangebyscore", key, opt.Max, opt.Min, "withscores"}
  1755. if opt.Offset != 0 || opt.Count != 0 {
  1756. args = append(
  1757. args,
  1758. "limit",
  1759. opt.Offset,
  1760. opt.Count,
  1761. )
  1762. }
  1763. cmd := NewZSliceCmd(args...)
  1764. _ = c(cmd)
  1765. return cmd
  1766. }
  1767. func (c cmdable) ZRevRank(key, member string) *IntCmd {
  1768. cmd := NewIntCmd("zrevrank", key, member)
  1769. _ = c(cmd)
  1770. return cmd
  1771. }
  1772. func (c cmdable) ZScore(key, member string) *FloatCmd {
  1773. cmd := NewFloatCmd("zscore", key, member)
  1774. _ = c(cmd)
  1775. return cmd
  1776. }
  1777. func (c cmdable) ZUnionStore(dest string, store *ZStore) *IntCmd {
  1778. args := make([]interface{}, 3+len(store.Keys))
  1779. args[0] = "zunionstore"
  1780. args[1] = dest
  1781. args[2] = len(store.Keys)
  1782. for i, key := range store.Keys {
  1783. args[3+i] = key
  1784. }
  1785. if len(store.Weights) > 0 {
  1786. args = append(args, "weights")
  1787. for _, weight := range store.Weights {
  1788. args = append(args, weight)
  1789. }
  1790. }
  1791. if store.Aggregate != "" {
  1792. args = append(args, "aggregate", store.Aggregate)
  1793. }
  1794. cmd := NewIntCmd(args...)
  1795. _ = c(cmd)
  1796. return cmd
  1797. }
  1798. //------------------------------------------------------------------------------
  1799. func (c cmdable) PFAdd(key string, els ...interface{}) *IntCmd {
  1800. args := make([]interface{}, 2, 2+len(els))
  1801. args[0] = "pfadd"
  1802. args[1] = key
  1803. args = appendArgs(args, els)
  1804. cmd := NewIntCmd(args...)
  1805. _ = c(cmd)
  1806. return cmd
  1807. }
  1808. func (c cmdable) PFCount(keys ...string) *IntCmd {
  1809. args := make([]interface{}, 1+len(keys))
  1810. args[0] = "pfcount"
  1811. for i, key := range keys {
  1812. args[1+i] = key
  1813. }
  1814. cmd := NewIntCmd(args...)
  1815. _ = c(cmd)
  1816. return cmd
  1817. }
  1818. func (c cmdable) PFMerge(dest string, keys ...string) *StatusCmd {
  1819. args := make([]interface{}, 2+len(keys))
  1820. args[0] = "pfmerge"
  1821. args[1] = dest
  1822. for i, key := range keys {
  1823. args[2+i] = key
  1824. }
  1825. cmd := NewStatusCmd(args...)
  1826. _ = c(cmd)
  1827. return cmd
  1828. }
  1829. //------------------------------------------------------------------------------
  1830. func (c cmdable) BgRewriteAOF() *StatusCmd {
  1831. cmd := NewStatusCmd("bgrewriteaof")
  1832. _ = c(cmd)
  1833. return cmd
  1834. }
  1835. func (c cmdable) BgSave() *StatusCmd {
  1836. cmd := NewStatusCmd("bgsave")
  1837. _ = c(cmd)
  1838. return cmd
  1839. }
  1840. func (c cmdable) ClientKill(ipPort string) *StatusCmd {
  1841. cmd := NewStatusCmd("client", "kill", ipPort)
  1842. _ = c(cmd)
  1843. return cmd
  1844. }
  1845. // ClientKillByFilter is new style synx, while the ClientKill is old
  1846. // CLIENT KILL <option> [value] ... <option> [value]
  1847. func (c cmdable) ClientKillByFilter(keys ...string) *IntCmd {
  1848. args := make([]interface{}, 2+len(keys))
  1849. args[0] = "client"
  1850. args[1] = "kill"
  1851. for i, key := range keys {
  1852. args[2+i] = key
  1853. }
  1854. cmd := NewIntCmd(args...)
  1855. _ = c(cmd)
  1856. return cmd
  1857. }
  1858. func (c cmdable) ClientList() *StringCmd {
  1859. cmd := NewStringCmd("client", "list")
  1860. _ = c(cmd)
  1861. return cmd
  1862. }
  1863. func (c cmdable) ClientPause(dur time.Duration) *BoolCmd {
  1864. cmd := NewBoolCmd("client", "pause", formatMs(dur))
  1865. _ = c(cmd)
  1866. return cmd
  1867. }
  1868. func (c cmdable) ClientID() *IntCmd {
  1869. cmd := NewIntCmd("client", "id")
  1870. _ = c(cmd)
  1871. return cmd
  1872. }
  1873. func (c cmdable) ClientUnblock(id int64) *IntCmd {
  1874. cmd := NewIntCmd("client", "unblock", id)
  1875. _ = c(cmd)
  1876. return cmd
  1877. }
  1878. func (c cmdable) ClientUnblockWithError(id int64) *IntCmd {
  1879. cmd := NewIntCmd("client", "unblock", id, "error")
  1880. _ = c(cmd)
  1881. return cmd
  1882. }
  1883. // ClientSetName assigns a name to the connection.
  1884. func (c statefulCmdable) ClientSetName(name string) *BoolCmd {
  1885. cmd := NewBoolCmd("client", "setname", name)
  1886. _ = c(cmd)
  1887. return cmd
  1888. }
  1889. // ClientGetName returns the name of the connection.
  1890. func (c cmdable) ClientGetName() *StringCmd {
  1891. cmd := NewStringCmd("client", "getname")
  1892. _ = c(cmd)
  1893. return cmd
  1894. }
  1895. func (c cmdable) ConfigGet(parameter string) *SliceCmd {
  1896. cmd := NewSliceCmd("config", "get", parameter)
  1897. _ = c(cmd)
  1898. return cmd
  1899. }
  1900. func (c cmdable) ConfigResetStat() *StatusCmd {
  1901. cmd := NewStatusCmd("config", "resetstat")
  1902. _ = c(cmd)
  1903. return cmd
  1904. }
  1905. func (c cmdable) ConfigSet(parameter, value string) *StatusCmd {
  1906. cmd := NewStatusCmd("config", "set", parameter, value)
  1907. _ = c(cmd)
  1908. return cmd
  1909. }
  1910. func (c cmdable) ConfigRewrite() *StatusCmd {
  1911. cmd := NewStatusCmd("config", "rewrite")
  1912. _ = c(cmd)
  1913. return cmd
  1914. }
  1915. // Deperecated. Use DBSize instead.
  1916. func (c cmdable) DbSize() *IntCmd {
  1917. return c.DBSize()
  1918. }
  1919. func (c cmdable) DBSize() *IntCmd {
  1920. cmd := NewIntCmd("dbsize")
  1921. _ = c(cmd)
  1922. return cmd
  1923. }
  1924. func (c cmdable) FlushAll() *StatusCmd {
  1925. cmd := NewStatusCmd("flushall")
  1926. _ = c(cmd)
  1927. return cmd
  1928. }
  1929. func (c cmdable) FlushAllAsync() *StatusCmd {
  1930. cmd := NewStatusCmd("flushall", "async")
  1931. _ = c(cmd)
  1932. return cmd
  1933. }
  1934. func (c cmdable) FlushDB() *StatusCmd {
  1935. cmd := NewStatusCmd("flushdb")
  1936. _ = c(cmd)
  1937. return cmd
  1938. }
  1939. func (c cmdable) FlushDBAsync() *StatusCmd {
  1940. cmd := NewStatusCmd("flushdb", "async")
  1941. _ = c(cmd)
  1942. return cmd
  1943. }
  1944. func (c cmdable) Info(section ...string) *StringCmd {
  1945. args := []interface{}{"info"}
  1946. if len(section) > 0 {
  1947. args = append(args, section[0])
  1948. }
  1949. cmd := NewStringCmd(args...)
  1950. _ = c(cmd)
  1951. return cmd
  1952. }
  1953. func (c cmdable) LastSave() *IntCmd {
  1954. cmd := NewIntCmd("lastsave")
  1955. _ = c(cmd)
  1956. return cmd
  1957. }
  1958. func (c cmdable) Save() *StatusCmd {
  1959. cmd := NewStatusCmd("save")
  1960. _ = c(cmd)
  1961. return cmd
  1962. }
  1963. func (c cmdable) shutdown(modifier string) *StatusCmd {
  1964. var args []interface{}
  1965. if modifier == "" {
  1966. args = []interface{}{"shutdown"}
  1967. } else {
  1968. args = []interface{}{"shutdown", modifier}
  1969. }
  1970. cmd := NewStatusCmd(args...)
  1971. _ = c(cmd)
  1972. if err := cmd.Err(); err != nil {
  1973. if err == io.EOF {
  1974. // Server quit as expected.
  1975. cmd.err = nil
  1976. }
  1977. } else {
  1978. // Server did not quit. String reply contains the reason.
  1979. cmd.err = errors.New(cmd.val)
  1980. cmd.val = ""
  1981. }
  1982. return cmd
  1983. }
  1984. func (c cmdable) Shutdown() *StatusCmd {
  1985. return c.shutdown("")
  1986. }
  1987. func (c cmdable) ShutdownSave() *StatusCmd {
  1988. return c.shutdown("save")
  1989. }
  1990. func (c cmdable) ShutdownNoSave() *StatusCmd {
  1991. return c.shutdown("nosave")
  1992. }
  1993. func (c cmdable) SlaveOf(host, port string) *StatusCmd {
  1994. cmd := NewStatusCmd("slaveof", host, port)
  1995. _ = c(cmd)
  1996. return cmd
  1997. }
  1998. func (c cmdable) SlowLog() {
  1999. panic("not implemented")
  2000. }
  2001. func (c cmdable) Sync() {
  2002. panic("not implemented")
  2003. }
  2004. func (c cmdable) Time() *TimeCmd {
  2005. cmd := NewTimeCmd("time")
  2006. _ = c(cmd)
  2007. return cmd
  2008. }
  2009. //------------------------------------------------------------------------------
  2010. func (c cmdable) Eval(script string, keys []string, args ...interface{}) *Cmd {
  2011. cmdArgs := make([]interface{}, 3+len(keys), 3+len(keys)+len(args))
  2012. cmdArgs[0] = "eval"
  2013. cmdArgs[1] = script
  2014. cmdArgs[2] = len(keys)
  2015. for i, key := range keys {
  2016. cmdArgs[3+i] = key
  2017. }
  2018. cmdArgs = appendArgs(cmdArgs, args)
  2019. cmd := NewCmd(cmdArgs...)
  2020. _ = c(cmd)
  2021. return cmd
  2022. }
  2023. func (c cmdable) EvalSha(sha1 string, keys []string, args ...interface{}) *Cmd {
  2024. cmdArgs := make([]interface{}, 3+len(keys), 3+len(keys)+len(args))
  2025. cmdArgs[0] = "evalsha"
  2026. cmdArgs[1] = sha1
  2027. cmdArgs[2] = len(keys)
  2028. for i, key := range keys {
  2029. cmdArgs[3+i] = key
  2030. }
  2031. cmdArgs = appendArgs(cmdArgs, args)
  2032. cmd := NewCmd(cmdArgs...)
  2033. _ = c(cmd)
  2034. return cmd
  2035. }
  2036. func (c cmdable) ScriptExists(hashes ...string) *BoolSliceCmd {
  2037. args := make([]interface{}, 2+len(hashes))
  2038. args[0] = "script"
  2039. args[1] = "exists"
  2040. for i, hash := range hashes {
  2041. args[2+i] = hash
  2042. }
  2043. cmd := NewBoolSliceCmd(args...)
  2044. _ = c(cmd)
  2045. return cmd
  2046. }
  2047. func (c cmdable) ScriptFlush() *StatusCmd {
  2048. cmd := NewStatusCmd("script", "flush")
  2049. _ = c(cmd)
  2050. return cmd
  2051. }
  2052. func (c cmdable) ScriptKill() *StatusCmd {
  2053. cmd := NewStatusCmd("script", "kill")
  2054. _ = c(cmd)
  2055. return cmd
  2056. }
  2057. func (c cmdable) ScriptLoad(script string) *StringCmd {
  2058. cmd := NewStringCmd("script", "load", script)
  2059. _ = c(cmd)
  2060. return cmd
  2061. }
  2062. //------------------------------------------------------------------------------
  2063. func (c cmdable) DebugObject(key string) *StringCmd {
  2064. cmd := NewStringCmd("debug", "object", key)
  2065. _ = c(cmd)
  2066. return cmd
  2067. }
  2068. //------------------------------------------------------------------------------
  2069. // Publish posts the message to the channel.
  2070. func (c cmdable) Publish(channel string, message interface{}) *IntCmd {
  2071. cmd := NewIntCmd("publish", channel, message)
  2072. _ = c(cmd)
  2073. return cmd
  2074. }
  2075. func (c cmdable) PubSubChannels(pattern string) *StringSliceCmd {
  2076. args := []interface{}{"pubsub", "channels"}
  2077. if pattern != "*" {
  2078. args = append(args, pattern)
  2079. }
  2080. cmd := NewStringSliceCmd(args...)
  2081. _ = c(cmd)
  2082. return cmd
  2083. }
  2084. func (c cmdable) PubSubNumSub(channels ...string) *StringIntMapCmd {
  2085. args := make([]interface{}, 2+len(channels))
  2086. args[0] = "pubsub"
  2087. args[1] = "numsub"
  2088. for i, channel := range channels {
  2089. args[2+i] = channel
  2090. }
  2091. cmd := NewStringIntMapCmd(args...)
  2092. _ = c(cmd)
  2093. return cmd
  2094. }
  2095. func (c cmdable) PubSubNumPat() *IntCmd {
  2096. cmd := NewIntCmd("pubsub", "numpat")
  2097. _ = c(cmd)
  2098. return cmd
  2099. }
  2100. //------------------------------------------------------------------------------
  2101. func (c cmdable) ClusterSlots() *ClusterSlotsCmd {
  2102. cmd := NewClusterSlotsCmd("cluster", "slots")
  2103. _ = c(cmd)
  2104. return cmd
  2105. }
  2106. func (c cmdable) ClusterNodes() *StringCmd {
  2107. cmd := NewStringCmd("cluster", "nodes")
  2108. _ = c(cmd)
  2109. return cmd
  2110. }
  2111. func (c cmdable) ClusterMeet(host, port string) *StatusCmd {
  2112. cmd := NewStatusCmd("cluster", "meet", host, port)
  2113. _ = c(cmd)
  2114. return cmd
  2115. }
  2116. func (c cmdable) ClusterForget(nodeID string) *StatusCmd {
  2117. cmd := NewStatusCmd("cluster", "forget", nodeID)
  2118. _ = c(cmd)
  2119. return cmd
  2120. }
  2121. func (c cmdable) ClusterReplicate(nodeID string) *StatusCmd {
  2122. cmd := NewStatusCmd("cluster", "replicate", nodeID)
  2123. _ = c(cmd)
  2124. return cmd
  2125. }
  2126. func (c cmdable) ClusterResetSoft() *StatusCmd {
  2127. cmd := NewStatusCmd("cluster", "reset", "soft")
  2128. _ = c(cmd)
  2129. return cmd
  2130. }
  2131. func (c cmdable) ClusterResetHard() *StatusCmd {
  2132. cmd := NewStatusCmd("cluster", "reset", "hard")
  2133. _ = c(cmd)
  2134. return cmd
  2135. }
  2136. func (c cmdable) ClusterInfo() *StringCmd {
  2137. cmd := NewStringCmd("cluster", "info")
  2138. _ = c(cmd)
  2139. return cmd
  2140. }
  2141. func (c cmdable) ClusterKeySlot(key string) *IntCmd {
  2142. cmd := NewIntCmd("cluster", "keyslot", key)
  2143. _ = c(cmd)
  2144. return cmd
  2145. }
  2146. func (c cmdable) ClusterGetKeysInSlot(slot int, count int) *StringSliceCmd {
  2147. cmd := NewStringSliceCmd("cluster", "getkeysinslot", slot, count)
  2148. _ = c(cmd)
  2149. return cmd
  2150. }
  2151. func (c cmdable) ClusterCountFailureReports(nodeID string) *IntCmd {
  2152. cmd := NewIntCmd("cluster", "count-failure-reports", nodeID)
  2153. _ = c(cmd)
  2154. return cmd
  2155. }
  2156. func (c cmdable) ClusterCountKeysInSlot(slot int) *IntCmd {
  2157. cmd := NewIntCmd("cluster", "countkeysinslot", slot)
  2158. _ = c(cmd)
  2159. return cmd
  2160. }
  2161. func (c cmdable) ClusterDelSlots(slots ...int) *StatusCmd {
  2162. args := make([]interface{}, 2+len(slots))
  2163. args[0] = "cluster"
  2164. args[1] = "delslots"
  2165. for i, slot := range slots {
  2166. args[2+i] = slot
  2167. }
  2168. cmd := NewStatusCmd(args...)
  2169. _ = c(cmd)
  2170. return cmd
  2171. }
  2172. func (c cmdable) ClusterDelSlotsRange(min, max int) *StatusCmd {
  2173. size := max - min + 1
  2174. slots := make([]int, size)
  2175. for i := 0; i < size; i++ {
  2176. slots[i] = min + i
  2177. }
  2178. return c.ClusterDelSlots(slots...)
  2179. }
  2180. func (c cmdable) ClusterSaveConfig() *StatusCmd {
  2181. cmd := NewStatusCmd("cluster", "saveconfig")
  2182. _ = c(cmd)
  2183. return cmd
  2184. }
  2185. func (c cmdable) ClusterSlaves(nodeID string) *StringSliceCmd {
  2186. cmd := NewStringSliceCmd("cluster", "slaves", nodeID)
  2187. _ = c(cmd)
  2188. return cmd
  2189. }
  2190. func (c cmdable) ReadOnly() *StatusCmd {
  2191. cmd := NewStatusCmd("readonly")
  2192. _ = c(cmd)
  2193. return cmd
  2194. }
  2195. func (c cmdable) ReadWrite() *StatusCmd {
  2196. cmd := NewStatusCmd("readwrite")
  2197. _ = c(cmd)
  2198. return cmd
  2199. }
  2200. func (c cmdable) ClusterFailover() *StatusCmd {
  2201. cmd := NewStatusCmd("cluster", "failover")
  2202. _ = c(cmd)
  2203. return cmd
  2204. }
  2205. func (c cmdable) ClusterAddSlots(slots ...int) *StatusCmd {
  2206. args := make([]interface{}, 2+len(slots))
  2207. args[0] = "cluster"
  2208. args[1] = "addslots"
  2209. for i, num := range slots {
  2210. args[2+i] = num
  2211. }
  2212. cmd := NewStatusCmd(args...)
  2213. _ = c(cmd)
  2214. return cmd
  2215. }
  2216. func (c cmdable) ClusterAddSlotsRange(min, max int) *StatusCmd {
  2217. size := max - min + 1
  2218. slots := make([]int, size)
  2219. for i := 0; i < size; i++ {
  2220. slots[i] = min + i
  2221. }
  2222. return c.ClusterAddSlots(slots...)
  2223. }
  2224. //------------------------------------------------------------------------------
  2225. func (c cmdable) GeoAdd(key string, geoLocation ...*GeoLocation) *IntCmd {
  2226. args := make([]interface{}, 2+3*len(geoLocation))
  2227. args[0] = "geoadd"
  2228. args[1] = key
  2229. for i, eachLoc := range geoLocation {
  2230. args[2+3*i] = eachLoc.Longitude
  2231. args[2+3*i+1] = eachLoc.Latitude
  2232. args[2+3*i+2] = eachLoc.Name
  2233. }
  2234. cmd := NewIntCmd(args...)
  2235. _ = c(cmd)
  2236. return cmd
  2237. }
  2238. // GeoRadius is a read-only GEORADIUS_RO command.
  2239. func (c cmdable) GeoRadius(key string, longitude, latitude float64, query *GeoRadiusQuery) *GeoLocationCmd {
  2240. cmd := NewGeoLocationCmd(query, "georadius_ro", key, longitude, latitude)
  2241. if query.Store != "" || query.StoreDist != "" {
  2242. cmd.SetErr(errors.New("GeoRadius does not support Store or StoreDist"))
  2243. return cmd
  2244. }
  2245. _ = c(cmd)
  2246. return cmd
  2247. }
  2248. // GeoRadiusStore is a writing GEORADIUS command.
  2249. func (c cmdable) GeoRadiusStore(key string, longitude, latitude float64, query *GeoRadiusQuery) *IntCmd {
  2250. args := geoLocationArgs(query, "georadius", key, longitude, latitude)
  2251. cmd := NewIntCmd(args...)
  2252. if query.Store == "" && query.StoreDist == "" {
  2253. cmd.SetErr(errors.New("GeoRadiusStore requires Store or StoreDist"))
  2254. return cmd
  2255. }
  2256. _ = c(cmd)
  2257. return cmd
  2258. }
  2259. // GeoRadius is a read-only GEORADIUSBYMEMBER_RO command.
  2260. func (c cmdable) GeoRadiusByMember(key, member string, query *GeoRadiusQuery) *GeoLocationCmd {
  2261. cmd := NewGeoLocationCmd(query, "georadiusbymember_ro", key, member)
  2262. if query.Store != "" || query.StoreDist != "" {
  2263. cmd.SetErr(errors.New("GeoRadiusByMember does not support Store or StoreDist"))
  2264. return cmd
  2265. }
  2266. _ = c(cmd)
  2267. return cmd
  2268. }
  2269. // GeoRadiusByMemberStore is a writing GEORADIUSBYMEMBER command.
  2270. func (c cmdable) GeoRadiusByMemberStore(key, member string, query *GeoRadiusQuery) *IntCmd {
  2271. args := geoLocationArgs(query, "georadiusbymember", key, member)
  2272. cmd := NewIntCmd(args...)
  2273. if query.Store == "" && query.StoreDist == "" {
  2274. cmd.SetErr(errors.New("GeoRadiusByMemberStore requires Store or StoreDist"))
  2275. return cmd
  2276. }
  2277. _ = c(cmd)
  2278. return cmd
  2279. }
  2280. func (c cmdable) GeoDist(key string, member1, member2, unit string) *FloatCmd {
  2281. if unit == "" {
  2282. unit = "km"
  2283. }
  2284. cmd := NewFloatCmd("geodist", key, member1, member2, unit)
  2285. _ = c(cmd)
  2286. return cmd
  2287. }
  2288. func (c cmdable) GeoHash(key string, members ...string) *StringSliceCmd {
  2289. args := make([]interface{}, 2+len(members))
  2290. args[0] = "geohash"
  2291. args[1] = key
  2292. for i, member := range members {
  2293. args[2+i] = member
  2294. }
  2295. cmd := NewStringSliceCmd(args...)
  2296. _ = c(cmd)
  2297. return cmd
  2298. }
  2299. func (c cmdable) GeoPos(key string, members ...string) *GeoPosCmd {
  2300. args := make([]interface{}, 2+len(members))
  2301. args[0] = "geopos"
  2302. args[1] = key
  2303. for i, member := range members {
  2304. args[2+i] = member
  2305. }
  2306. cmd := NewGeoPosCmd(args...)
  2307. _ = c(cmd)
  2308. return cmd
  2309. }
  2310. //------------------------------------------------------------------------------
  2311. func (c cmdable) MemoryUsage(key string, samples ...int) *IntCmd {
  2312. args := []interface{}{"memory", "usage", key}
  2313. if len(samples) > 0 {
  2314. if len(samples) != 1 {
  2315. panic("MemoryUsage expects single sample count")
  2316. }
  2317. args = append(args, "SAMPLES", samples[0])
  2318. }
  2319. cmd := NewIntCmd(args...)
  2320. _ = c(cmd)
  2321. return cmd
  2322. }