commands.go 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625
  1. package redis
  2. import (
  3. "errors"
  4. "io"
  5. "time"
  6. "github.com/2637309949/dolphin/packages/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. func (c cmdable) HMGet(key string, fields ...string) *SliceCmd {
  877. args := make([]interface{}, 2+len(fields))
  878. args[0] = "hmget"
  879. args[1] = key
  880. for i, field := range fields {
  881. args[2+i] = field
  882. }
  883. cmd := NewSliceCmd(args...)
  884. _ = c(cmd)
  885. return cmd
  886. }
  887. // HMSet is like HSet, but accepts multiple values:
  888. // - HMSet("key1", "value1", "key2", "value2")
  889. // - HMSet([]string{"key1", "value1", "key2", "value2"})
  890. // - HMSet(map[string]interface{}{"key1": "value1", "key2": "value2"})
  891. //
  892. // Note that it uses HSET Redis command underneath because HMSET is deprecated.
  893. func (c cmdable) HMSet(key string, values ...interface{}) *IntCmd {
  894. args := make([]interface{}, 2+2*len(values))
  895. args[0] = "hset"
  896. args[1] = key
  897. args = appendArgs(args, values)
  898. cmd := NewIntCmd(args...)
  899. _ = c(cmd)
  900. return cmd
  901. }
  902. func (c cmdable) HSet(key, field string, value interface{}) *BoolCmd {
  903. cmd := NewBoolCmd("hset", key, field, value)
  904. _ = c(cmd)
  905. return cmd
  906. }
  907. func (c cmdable) HSetNX(key, field string, value interface{}) *BoolCmd {
  908. cmd := NewBoolCmd("hsetnx", key, field, value)
  909. _ = c(cmd)
  910. return cmd
  911. }
  912. func (c cmdable) HVals(key string) *StringSliceCmd {
  913. cmd := NewStringSliceCmd("hvals", key)
  914. _ = c(cmd)
  915. return cmd
  916. }
  917. //------------------------------------------------------------------------------
  918. func (c cmdable) BLPop(timeout time.Duration, keys ...string) *StringSliceCmd {
  919. args := make([]interface{}, 1+len(keys)+1)
  920. args[0] = "blpop"
  921. for i, key := range keys {
  922. args[1+i] = key
  923. }
  924. args[len(args)-1] = formatSec(timeout)
  925. cmd := NewStringSliceCmd(args...)
  926. cmd.setReadTimeout(timeout)
  927. _ = c(cmd)
  928. return cmd
  929. }
  930. func (c cmdable) BRPop(timeout time.Duration, keys ...string) *StringSliceCmd {
  931. args := make([]interface{}, 1+len(keys)+1)
  932. args[0] = "brpop"
  933. for i, key := range keys {
  934. args[1+i] = key
  935. }
  936. args[len(keys)+1] = formatSec(timeout)
  937. cmd := NewStringSliceCmd(args...)
  938. cmd.setReadTimeout(timeout)
  939. _ = c(cmd)
  940. return cmd
  941. }
  942. func (c cmdable) BRPopLPush(source, destination string, timeout time.Duration) *StringCmd {
  943. cmd := NewStringCmd(
  944. "brpoplpush",
  945. source,
  946. destination,
  947. formatSec(timeout),
  948. )
  949. cmd.setReadTimeout(timeout)
  950. _ = c(cmd)
  951. return cmd
  952. }
  953. func (c cmdable) LIndex(key string, index int64) *StringCmd {
  954. cmd := NewStringCmd("lindex", key, index)
  955. _ = c(cmd)
  956. return cmd
  957. }
  958. func (c cmdable) LInsert(key, op string, pivot, value interface{}) *IntCmd {
  959. cmd := NewIntCmd("linsert", key, op, pivot, value)
  960. _ = c(cmd)
  961. return cmd
  962. }
  963. func (c cmdable) LInsertBefore(key string, pivot, value interface{}) *IntCmd {
  964. cmd := NewIntCmd("linsert", key, "before", pivot, value)
  965. _ = c(cmd)
  966. return cmd
  967. }
  968. func (c cmdable) LInsertAfter(key string, pivot, value interface{}) *IntCmd {
  969. cmd := NewIntCmd("linsert", key, "after", pivot, value)
  970. _ = c(cmd)
  971. return cmd
  972. }
  973. func (c cmdable) LLen(key string) *IntCmd {
  974. cmd := NewIntCmd("llen", key)
  975. _ = c(cmd)
  976. return cmd
  977. }
  978. func (c cmdable) LPop(key string) *StringCmd {
  979. cmd := NewStringCmd("lpop", key)
  980. _ = c(cmd)
  981. return cmd
  982. }
  983. func (c cmdable) LPush(key string, values ...interface{}) *IntCmd {
  984. args := make([]interface{}, 2, 2+len(values))
  985. args[0] = "lpush"
  986. args[1] = key
  987. args = appendArgs(args, values)
  988. cmd := NewIntCmd(args...)
  989. _ = c(cmd)
  990. return cmd
  991. }
  992. func (c cmdable) LPushX(key string, values ...interface{}) *IntCmd {
  993. args := make([]interface{}, 2, 2+len(values))
  994. args[0] = "lpushx"
  995. args[1] = key
  996. args = appendArgs(args, values)
  997. cmd := NewIntCmd(args...)
  998. _ = c(cmd)
  999. return cmd
  1000. }
  1001. func (c cmdable) LRange(key string, start, stop int64) *StringSliceCmd {
  1002. cmd := NewStringSliceCmd(
  1003. "lrange",
  1004. key,
  1005. start,
  1006. stop,
  1007. )
  1008. _ = c(cmd)
  1009. return cmd
  1010. }
  1011. func (c cmdable) LRem(key string, count int64, value interface{}) *IntCmd {
  1012. cmd := NewIntCmd("lrem", key, count, value)
  1013. _ = c(cmd)
  1014. return cmd
  1015. }
  1016. func (c cmdable) LSet(key string, index int64, value interface{}) *StatusCmd {
  1017. cmd := NewStatusCmd("lset", key, index, value)
  1018. _ = c(cmd)
  1019. return cmd
  1020. }
  1021. func (c cmdable) LTrim(key string, start, stop int64) *StatusCmd {
  1022. cmd := NewStatusCmd(
  1023. "ltrim",
  1024. key,
  1025. start,
  1026. stop,
  1027. )
  1028. _ = c(cmd)
  1029. return cmd
  1030. }
  1031. func (c cmdable) RPop(key string) *StringCmd {
  1032. cmd := NewStringCmd("rpop", key)
  1033. _ = c(cmd)
  1034. return cmd
  1035. }
  1036. func (c cmdable) RPopLPush(source, destination string) *StringCmd {
  1037. cmd := NewStringCmd("rpoplpush", source, destination)
  1038. _ = c(cmd)
  1039. return cmd
  1040. }
  1041. func (c cmdable) RPush(key string, values ...interface{}) *IntCmd {
  1042. args := make([]interface{}, 2, 2+len(values))
  1043. args[0] = "rpush"
  1044. args[1] = key
  1045. args = appendArgs(args, values)
  1046. cmd := NewIntCmd(args...)
  1047. _ = c(cmd)
  1048. return cmd
  1049. }
  1050. func (c cmdable) RPushX(key string, values ...interface{}) *IntCmd {
  1051. args := make([]interface{}, 2, 2+len(values))
  1052. args[0] = "rpushx"
  1053. args[1] = key
  1054. args = appendArgs(args, values)
  1055. cmd := NewIntCmd(args...)
  1056. _ = c(cmd)
  1057. return cmd
  1058. }
  1059. //------------------------------------------------------------------------------
  1060. func (c cmdable) SAdd(key string, members ...interface{}) *IntCmd {
  1061. args := make([]interface{}, 2, 2+len(members))
  1062. args[0] = "sadd"
  1063. args[1] = key
  1064. args = appendArgs(args, members)
  1065. cmd := NewIntCmd(args...)
  1066. _ = c(cmd)
  1067. return cmd
  1068. }
  1069. func (c cmdable) SCard(key string) *IntCmd {
  1070. cmd := NewIntCmd("scard", key)
  1071. _ = c(cmd)
  1072. return cmd
  1073. }
  1074. func (c cmdable) SDiff(keys ...string) *StringSliceCmd {
  1075. args := make([]interface{}, 1+len(keys))
  1076. args[0] = "sdiff"
  1077. for i, key := range keys {
  1078. args[1+i] = key
  1079. }
  1080. cmd := NewStringSliceCmd(args...)
  1081. _ = c(cmd)
  1082. return cmd
  1083. }
  1084. func (c cmdable) SDiffStore(destination string, keys ...string) *IntCmd {
  1085. args := make([]interface{}, 2+len(keys))
  1086. args[0] = "sdiffstore"
  1087. args[1] = destination
  1088. for i, key := range keys {
  1089. args[2+i] = key
  1090. }
  1091. cmd := NewIntCmd(args...)
  1092. _ = c(cmd)
  1093. return cmd
  1094. }
  1095. func (c cmdable) SInter(keys ...string) *StringSliceCmd {
  1096. args := make([]interface{}, 1+len(keys))
  1097. args[0] = "sinter"
  1098. for i, key := range keys {
  1099. args[1+i] = key
  1100. }
  1101. cmd := NewStringSliceCmd(args...)
  1102. _ = c(cmd)
  1103. return cmd
  1104. }
  1105. func (c cmdable) SInterStore(destination string, keys ...string) *IntCmd {
  1106. args := make([]interface{}, 2+len(keys))
  1107. args[0] = "sinterstore"
  1108. args[1] = destination
  1109. for i, key := range keys {
  1110. args[2+i] = key
  1111. }
  1112. cmd := NewIntCmd(args...)
  1113. _ = c(cmd)
  1114. return cmd
  1115. }
  1116. func (c cmdable) SIsMember(key string, member interface{}) *BoolCmd {
  1117. cmd := NewBoolCmd("sismember", key, member)
  1118. _ = c(cmd)
  1119. return cmd
  1120. }
  1121. // Redis `SMEMBERS key` command output as a slice
  1122. func (c cmdable) SMembers(key string) *StringSliceCmd {
  1123. cmd := NewStringSliceCmd("smembers", key)
  1124. _ = c(cmd)
  1125. return cmd
  1126. }
  1127. // Redis `SMEMBERS key` command output as a map
  1128. func (c cmdable) SMembersMap(key string) *StringStructMapCmd {
  1129. cmd := NewStringStructMapCmd("smembers", key)
  1130. _ = c(cmd)
  1131. return cmd
  1132. }
  1133. func (c cmdable) SMove(source, destination string, member interface{}) *BoolCmd {
  1134. cmd := NewBoolCmd("smove", source, destination, member)
  1135. _ = c(cmd)
  1136. return cmd
  1137. }
  1138. // Redis `SPOP key` command.
  1139. func (c cmdable) SPop(key string) *StringCmd {
  1140. cmd := NewStringCmd("spop", key)
  1141. _ = c(cmd)
  1142. return cmd
  1143. }
  1144. // Redis `SPOP key count` command.
  1145. func (c cmdable) SPopN(key string, count int64) *StringSliceCmd {
  1146. cmd := NewStringSliceCmd("spop", key, count)
  1147. _ = c(cmd)
  1148. return cmd
  1149. }
  1150. // Redis `SRANDMEMBER key` command.
  1151. func (c cmdable) SRandMember(key string) *StringCmd {
  1152. cmd := NewStringCmd("srandmember", key)
  1153. _ = c(cmd)
  1154. return cmd
  1155. }
  1156. // Redis `SRANDMEMBER key count` command.
  1157. func (c cmdable) SRandMemberN(key string, count int64) *StringSliceCmd {
  1158. cmd := NewStringSliceCmd("srandmember", key, count)
  1159. _ = c(cmd)
  1160. return cmd
  1161. }
  1162. func (c cmdable) SRem(key string, members ...interface{}) *IntCmd {
  1163. args := make([]interface{}, 2, 2+len(members))
  1164. args[0] = "srem"
  1165. args[1] = key
  1166. args = appendArgs(args, members)
  1167. cmd := NewIntCmd(args...)
  1168. _ = c(cmd)
  1169. return cmd
  1170. }
  1171. func (c cmdable) SUnion(keys ...string) *StringSliceCmd {
  1172. args := make([]interface{}, 1+len(keys))
  1173. args[0] = "sunion"
  1174. for i, key := range keys {
  1175. args[1+i] = key
  1176. }
  1177. cmd := NewStringSliceCmd(args...)
  1178. _ = c(cmd)
  1179. return cmd
  1180. }
  1181. func (c cmdable) SUnionStore(destination string, keys ...string) *IntCmd {
  1182. args := make([]interface{}, 2+len(keys))
  1183. args[0] = "sunionstore"
  1184. args[1] = destination
  1185. for i, key := range keys {
  1186. args[2+i] = key
  1187. }
  1188. cmd := NewIntCmd(args...)
  1189. _ = c(cmd)
  1190. return cmd
  1191. }
  1192. //------------------------------------------------------------------------------
  1193. type XAddArgs struct {
  1194. Stream string
  1195. MaxLen int64 // MAXLEN N
  1196. MaxLenApprox int64 // MAXLEN ~ N
  1197. ID string
  1198. Values map[string]interface{}
  1199. }
  1200. func (c cmdable) XAdd(a *XAddArgs) *StringCmd {
  1201. args := make([]interface{}, 0, 6+len(a.Values)*2)
  1202. args = append(args, "xadd")
  1203. args = append(args, a.Stream)
  1204. if a.MaxLen > 0 {
  1205. args = append(args, "maxlen", a.MaxLen)
  1206. } else if a.MaxLenApprox > 0 {
  1207. args = append(args, "maxlen", "~", a.MaxLenApprox)
  1208. }
  1209. if a.ID != "" {
  1210. args = append(args, a.ID)
  1211. } else {
  1212. args = append(args, "*")
  1213. }
  1214. for k, v := range a.Values {
  1215. args = append(args, k)
  1216. args = append(args, v)
  1217. }
  1218. cmd := NewStringCmd(args...)
  1219. _ = c(cmd)
  1220. return cmd
  1221. }
  1222. func (c cmdable) XDel(stream string, ids ...string) *IntCmd {
  1223. args := []interface{}{"xdel", stream}
  1224. for _, id := range ids {
  1225. args = append(args, id)
  1226. }
  1227. cmd := NewIntCmd(args...)
  1228. _ = c(cmd)
  1229. return cmd
  1230. }
  1231. func (c cmdable) XLen(stream string) *IntCmd {
  1232. cmd := NewIntCmd("xlen", stream)
  1233. _ = c(cmd)
  1234. return cmd
  1235. }
  1236. func (c cmdable) XRange(stream, start, stop string) *XMessageSliceCmd {
  1237. cmd := NewXMessageSliceCmd("xrange", stream, start, stop)
  1238. _ = c(cmd)
  1239. return cmd
  1240. }
  1241. func (c cmdable) XRangeN(stream, start, stop string, count int64) *XMessageSliceCmd {
  1242. cmd := NewXMessageSliceCmd("xrange", stream, start, stop, "count", count)
  1243. _ = c(cmd)
  1244. return cmd
  1245. }
  1246. func (c cmdable) XRevRange(stream, start, stop string) *XMessageSliceCmd {
  1247. cmd := NewXMessageSliceCmd("xrevrange", stream, start, stop)
  1248. _ = c(cmd)
  1249. return cmd
  1250. }
  1251. func (c cmdable) XRevRangeN(stream, start, stop string, count int64) *XMessageSliceCmd {
  1252. cmd := NewXMessageSliceCmd("xrevrange", stream, start, stop, "count", count)
  1253. _ = c(cmd)
  1254. return cmd
  1255. }
  1256. type XReadArgs struct {
  1257. Streams []string
  1258. Count int64
  1259. Block time.Duration
  1260. }
  1261. func (c cmdable) XRead(a *XReadArgs) *XStreamSliceCmd {
  1262. args := make([]interface{}, 0, 5+len(a.Streams))
  1263. args = append(args, "xread")
  1264. if a.Count > 0 {
  1265. args = append(args, "count")
  1266. args = append(args, a.Count)
  1267. }
  1268. if a.Block >= 0 {
  1269. args = append(args, "block")
  1270. args = append(args, int64(a.Block/time.Millisecond))
  1271. }
  1272. args = append(args, "streams")
  1273. for _, s := range a.Streams {
  1274. args = append(args, s)
  1275. }
  1276. cmd := NewXStreamSliceCmd(args...)
  1277. if a.Block >= 0 {
  1278. cmd.setReadTimeout(a.Block)
  1279. }
  1280. _ = c(cmd)
  1281. return cmd
  1282. }
  1283. func (c cmdable) XReadStreams(streams ...string) *XStreamSliceCmd {
  1284. return c.XRead(&XReadArgs{
  1285. Streams: streams,
  1286. Block: -1,
  1287. })
  1288. }
  1289. func (c cmdable) XGroupCreate(stream, group, start string) *StatusCmd {
  1290. cmd := NewStatusCmd("xgroup", "create", stream, group, start)
  1291. _ = c(cmd)
  1292. return cmd
  1293. }
  1294. func (c cmdable) XGroupCreateMkStream(stream, group, start string) *StatusCmd {
  1295. cmd := NewStatusCmd("xgroup", "create", stream, group, start, "mkstream")
  1296. _ = c(cmd)
  1297. return cmd
  1298. }
  1299. func (c cmdable) XGroupSetID(stream, group, start string) *StatusCmd {
  1300. cmd := NewStatusCmd("xgroup", "setid", stream, group, start)
  1301. _ = c(cmd)
  1302. return cmd
  1303. }
  1304. func (c cmdable) XGroupDestroy(stream, group string) *IntCmd {
  1305. cmd := NewIntCmd("xgroup", "destroy", stream, group)
  1306. _ = c(cmd)
  1307. return cmd
  1308. }
  1309. func (c cmdable) XGroupDelConsumer(stream, group, consumer string) *IntCmd {
  1310. cmd := NewIntCmd("xgroup", "delconsumer", stream, group, consumer)
  1311. _ = c(cmd)
  1312. return cmd
  1313. }
  1314. type XReadGroupArgs struct {
  1315. Group string
  1316. Consumer string
  1317. Streams []string // list of streams and ids, e.g. stream1 stream2 id1 id2
  1318. Count int64
  1319. Block time.Duration
  1320. NoAck bool
  1321. }
  1322. func (c cmdable) XReadGroup(a *XReadGroupArgs) *XStreamSliceCmd {
  1323. args := make([]interface{}, 0, 8+len(a.Streams))
  1324. args = append(args, "xreadgroup", "group", a.Group, a.Consumer)
  1325. if a.Count > 0 {
  1326. args = append(args, "count", a.Count)
  1327. }
  1328. if a.Block >= 0 {
  1329. args = append(args, "block", int64(a.Block/time.Millisecond))
  1330. }
  1331. if a.NoAck {
  1332. args = append(args, "noack")
  1333. }
  1334. args = append(args, "streams")
  1335. for _, s := range a.Streams {
  1336. args = append(args, s)
  1337. }
  1338. cmd := NewXStreamSliceCmd(args...)
  1339. if a.Block >= 0 {
  1340. cmd.setReadTimeout(a.Block)
  1341. }
  1342. _ = c(cmd)
  1343. return cmd
  1344. }
  1345. func (c cmdable) XAck(stream, group string, ids ...string) *IntCmd {
  1346. args := []interface{}{"xack", stream, group}
  1347. for _, id := range ids {
  1348. args = append(args, id)
  1349. }
  1350. cmd := NewIntCmd(args...)
  1351. _ = c(cmd)
  1352. return cmd
  1353. }
  1354. func (c cmdable) XPending(stream, group string) *XPendingCmd {
  1355. cmd := NewXPendingCmd("xpending", stream, group)
  1356. _ = c(cmd)
  1357. return cmd
  1358. }
  1359. type XPendingExtArgs struct {
  1360. Stream string
  1361. Group string
  1362. Start string
  1363. End string
  1364. Count int64
  1365. Consumer string
  1366. }
  1367. func (c cmdable) XPendingExt(a *XPendingExtArgs) *XPendingExtCmd {
  1368. args := make([]interface{}, 0, 7)
  1369. args = append(args, "xpending", a.Stream, a.Group, a.Start, a.End, a.Count)
  1370. if a.Consumer != "" {
  1371. args = append(args, a.Consumer)
  1372. }
  1373. cmd := NewXPendingExtCmd(args...)
  1374. _ = c(cmd)
  1375. return cmd
  1376. }
  1377. type XClaimArgs struct {
  1378. Stream string
  1379. Group string
  1380. Consumer string
  1381. MinIdle time.Duration
  1382. Messages []string
  1383. }
  1384. func (c cmdable) XClaim(a *XClaimArgs) *XMessageSliceCmd {
  1385. args := xClaimArgs(a)
  1386. cmd := NewXMessageSliceCmd(args...)
  1387. _ = c(cmd)
  1388. return cmd
  1389. }
  1390. func (c cmdable) XClaimJustID(a *XClaimArgs) *StringSliceCmd {
  1391. args := xClaimArgs(a)
  1392. args = append(args, "justid")
  1393. cmd := NewStringSliceCmd(args...)
  1394. _ = c(cmd)
  1395. return cmd
  1396. }
  1397. func xClaimArgs(a *XClaimArgs) []interface{} {
  1398. args := make([]interface{}, 0, 4+len(a.Messages))
  1399. args = append(args,
  1400. "xclaim",
  1401. a.Stream,
  1402. a.Group, a.Consumer,
  1403. int64(a.MinIdle/time.Millisecond))
  1404. for _, id := range a.Messages {
  1405. args = append(args, id)
  1406. }
  1407. return args
  1408. }
  1409. func (c cmdable) XTrim(key string, maxLen int64) *IntCmd {
  1410. cmd := NewIntCmd("xtrim", key, "maxlen", maxLen)
  1411. _ = c(cmd)
  1412. return cmd
  1413. }
  1414. func (c cmdable) XTrimApprox(key string, maxLen int64) *IntCmd {
  1415. cmd := NewIntCmd("xtrim", key, "maxlen", "~", maxLen)
  1416. _ = c(cmd)
  1417. return cmd
  1418. }
  1419. func (c cmdable) XInfoGroups(key string) *XInfoGroupsCmd {
  1420. cmd := NewXInfoGroupsCmd(key)
  1421. _ = c(cmd)
  1422. return cmd
  1423. }
  1424. //------------------------------------------------------------------------------
  1425. // Z represents sorted set member.
  1426. type Z struct {
  1427. Score float64
  1428. Member interface{}
  1429. }
  1430. // ZWithKey represents sorted set member including the name of the key where it was popped.
  1431. type ZWithKey struct {
  1432. Z
  1433. Key string
  1434. }
  1435. // ZStore is used as an arg to ZInterStore and ZUnionStore.
  1436. type ZStore struct {
  1437. Keys []string
  1438. Weights []float64
  1439. // Can be SUM, MIN or MAX.
  1440. Aggregate string
  1441. }
  1442. // Redis `BZPOPMAX key [key ...] timeout` command.
  1443. func (c cmdable) BZPopMax(timeout time.Duration, keys ...string) *ZWithKeyCmd {
  1444. args := make([]interface{}, 1+len(keys)+1)
  1445. args[0] = "bzpopmax"
  1446. for i, key := range keys {
  1447. args[1+i] = key
  1448. }
  1449. args[len(args)-1] = formatSec(timeout)
  1450. cmd := NewZWithKeyCmd(args...)
  1451. cmd.setReadTimeout(timeout)
  1452. _ = c(cmd)
  1453. return cmd
  1454. }
  1455. // Redis `BZPOPMIN key [key ...] timeout` command.
  1456. func (c cmdable) BZPopMin(timeout time.Duration, keys ...string) *ZWithKeyCmd {
  1457. args := make([]interface{}, 1+len(keys)+1)
  1458. args[0] = "bzpopmin"
  1459. for i, key := range keys {
  1460. args[1+i] = key
  1461. }
  1462. args[len(args)-1] = formatSec(timeout)
  1463. cmd := NewZWithKeyCmd(args...)
  1464. cmd.setReadTimeout(timeout)
  1465. _ = c(cmd)
  1466. return cmd
  1467. }
  1468. func (c cmdable) zAdd(a []interface{}, n int, members ...*Z) *IntCmd {
  1469. for i, m := range members {
  1470. a[n+2*i] = m.Score
  1471. a[n+2*i+1] = m.Member
  1472. }
  1473. cmd := NewIntCmd(a...)
  1474. _ = c(cmd)
  1475. return cmd
  1476. }
  1477. // Redis `ZADD key score member [score member ...]` command.
  1478. func (c cmdable) ZAdd(key string, members ...*Z) *IntCmd {
  1479. const n = 2
  1480. a := make([]interface{}, n+2*len(members))
  1481. a[0], a[1] = "zadd", key
  1482. return c.zAdd(a, n, members...)
  1483. }
  1484. // Redis `ZADD key NX score member [score member ...]` command.
  1485. func (c cmdable) ZAddNX(key string, members ...*Z) *IntCmd {
  1486. const n = 3
  1487. a := make([]interface{}, n+2*len(members))
  1488. a[0], a[1], a[2] = "zadd", key, "nx"
  1489. return c.zAdd(a, n, members...)
  1490. }
  1491. // Redis `ZADD key XX score member [score member ...]` command.
  1492. func (c cmdable) ZAddXX(key string, members ...*Z) *IntCmd {
  1493. const n = 3
  1494. a := make([]interface{}, n+2*len(members))
  1495. a[0], a[1], a[2] = "zadd", key, "xx"
  1496. return c.zAdd(a, n, members...)
  1497. }
  1498. // Redis `ZADD key CH score member [score member ...]` command.
  1499. func (c cmdable) ZAddCh(key string, members ...*Z) *IntCmd {
  1500. const n = 3
  1501. a := make([]interface{}, n+2*len(members))
  1502. a[0], a[1], a[2] = "zadd", key, "ch"
  1503. return c.zAdd(a, n, members...)
  1504. }
  1505. // Redis `ZADD key NX CH score member [score member ...]` command.
  1506. func (c cmdable) ZAddNXCh(key string, members ...*Z) *IntCmd {
  1507. const n = 4
  1508. a := make([]interface{}, n+2*len(members))
  1509. a[0], a[1], a[2], a[3] = "zadd", key, "nx", "ch"
  1510. return c.zAdd(a, n, members...)
  1511. }
  1512. // Redis `ZADD key XX CH score member [score member ...]` command.
  1513. func (c cmdable) ZAddXXCh(key string, members ...*Z) *IntCmd {
  1514. const n = 4
  1515. a := make([]interface{}, n+2*len(members))
  1516. a[0], a[1], a[2], a[3] = "zadd", key, "xx", "ch"
  1517. return c.zAdd(a, n, members...)
  1518. }
  1519. func (c cmdable) zIncr(a []interface{}, n int, members ...*Z) *FloatCmd {
  1520. for i, m := range members {
  1521. a[n+2*i] = m.Score
  1522. a[n+2*i+1] = m.Member
  1523. }
  1524. cmd := NewFloatCmd(a...)
  1525. _ = c(cmd)
  1526. return cmd
  1527. }
  1528. // Redis `ZADD key INCR score member` command.
  1529. func (c cmdable) ZIncr(key string, member *Z) *FloatCmd {
  1530. const n = 3
  1531. a := make([]interface{}, n+2)
  1532. a[0], a[1], a[2] = "zadd", key, "incr"
  1533. return c.zIncr(a, n, member)
  1534. }
  1535. // Redis `ZADD key NX INCR score member` command.
  1536. func (c cmdable) ZIncrNX(key string, member *Z) *FloatCmd {
  1537. const n = 4
  1538. a := make([]interface{}, n+2)
  1539. a[0], a[1], a[2], a[3] = "zadd", key, "incr", "nx"
  1540. return c.zIncr(a, n, member)
  1541. }
  1542. // Redis `ZADD key XX INCR score member` command.
  1543. func (c cmdable) ZIncrXX(key string, member *Z) *FloatCmd {
  1544. const n = 4
  1545. a := make([]interface{}, n+2)
  1546. a[0], a[1], a[2], a[3] = "zadd", key, "incr", "xx"
  1547. return c.zIncr(a, n, member)
  1548. }
  1549. func (c cmdable) ZCard(key string) *IntCmd {
  1550. cmd := NewIntCmd("zcard", key)
  1551. _ = c(cmd)
  1552. return cmd
  1553. }
  1554. func (c cmdable) ZCount(key, min, max string) *IntCmd {
  1555. cmd := NewIntCmd("zcount", key, min, max)
  1556. _ = c(cmd)
  1557. return cmd
  1558. }
  1559. func (c cmdable) ZLexCount(key, min, max string) *IntCmd {
  1560. cmd := NewIntCmd("zlexcount", key, min, max)
  1561. _ = c(cmd)
  1562. return cmd
  1563. }
  1564. func (c cmdable) ZIncrBy(key string, increment float64, member string) *FloatCmd {
  1565. cmd := NewFloatCmd("zincrby", key, increment, member)
  1566. _ = c(cmd)
  1567. return cmd
  1568. }
  1569. func (c cmdable) ZInterStore(destination string, store *ZStore) *IntCmd {
  1570. args := make([]interface{}, 3+len(store.Keys))
  1571. args[0] = "zinterstore"
  1572. args[1] = destination
  1573. args[2] = len(store.Keys)
  1574. for i, key := range store.Keys {
  1575. args[3+i] = key
  1576. }
  1577. if len(store.Weights) > 0 {
  1578. args = append(args, "weights")
  1579. for _, weight := range store.Weights {
  1580. args = append(args, weight)
  1581. }
  1582. }
  1583. if store.Aggregate != "" {
  1584. args = append(args, "aggregate", store.Aggregate)
  1585. }
  1586. cmd := NewIntCmd(args...)
  1587. _ = c(cmd)
  1588. return cmd
  1589. }
  1590. func (c cmdable) ZPopMax(key string, count ...int64) *ZSliceCmd {
  1591. args := []interface{}{
  1592. "zpopmax",
  1593. key,
  1594. }
  1595. switch len(count) {
  1596. case 0:
  1597. break
  1598. case 1:
  1599. args = append(args, count[0])
  1600. default:
  1601. panic("too many arguments")
  1602. }
  1603. cmd := NewZSliceCmd(args...)
  1604. _ = c(cmd)
  1605. return cmd
  1606. }
  1607. func (c cmdable) ZPopMin(key string, count ...int64) *ZSliceCmd {
  1608. args := []interface{}{
  1609. "zpopmin",
  1610. key,
  1611. }
  1612. switch len(count) {
  1613. case 0:
  1614. break
  1615. case 1:
  1616. args = append(args, count[0])
  1617. default:
  1618. panic("too many arguments")
  1619. }
  1620. cmd := NewZSliceCmd(args...)
  1621. _ = c(cmd)
  1622. return cmd
  1623. }
  1624. func (c cmdable) zRange(key string, start, stop int64, withScores bool) *StringSliceCmd {
  1625. args := []interface{}{
  1626. "zrange",
  1627. key,
  1628. start,
  1629. stop,
  1630. }
  1631. if withScores {
  1632. args = append(args, "withscores")
  1633. }
  1634. cmd := NewStringSliceCmd(args...)
  1635. _ = c(cmd)
  1636. return cmd
  1637. }
  1638. func (c cmdable) ZRange(key string, start, stop int64) *StringSliceCmd {
  1639. return c.zRange(key, start, stop, false)
  1640. }
  1641. func (c cmdable) ZRangeWithScores(key string, start, stop int64) *ZSliceCmd {
  1642. cmd := NewZSliceCmd("zrange", key, start, stop, "withscores")
  1643. _ = c(cmd)
  1644. return cmd
  1645. }
  1646. type ZRangeBy struct {
  1647. Min, Max string
  1648. Offset, Count int64
  1649. }
  1650. func (c cmdable) zRangeBy(zcmd, key string, opt *ZRangeBy, withScores bool) *StringSliceCmd {
  1651. args := []interface{}{zcmd, key, opt.Min, opt.Max}
  1652. if withScores {
  1653. args = append(args, "withscores")
  1654. }
  1655. if opt.Offset != 0 || opt.Count != 0 {
  1656. args = append(
  1657. args,
  1658. "limit",
  1659. opt.Offset,
  1660. opt.Count,
  1661. )
  1662. }
  1663. cmd := NewStringSliceCmd(args...)
  1664. _ = c(cmd)
  1665. return cmd
  1666. }
  1667. func (c cmdable) ZRangeByScore(key string, opt *ZRangeBy) *StringSliceCmd {
  1668. return c.zRangeBy("zrangebyscore", key, opt, false)
  1669. }
  1670. func (c cmdable) ZRangeByLex(key string, opt *ZRangeBy) *StringSliceCmd {
  1671. return c.zRangeBy("zrangebylex", key, opt, false)
  1672. }
  1673. func (c cmdable) ZRangeByScoreWithScores(key string, opt *ZRangeBy) *ZSliceCmd {
  1674. args := []interface{}{"zrangebyscore", key, opt.Min, opt.Max, "withscores"}
  1675. if opt.Offset != 0 || opt.Count != 0 {
  1676. args = append(
  1677. args,
  1678. "limit",
  1679. opt.Offset,
  1680. opt.Count,
  1681. )
  1682. }
  1683. cmd := NewZSliceCmd(args...)
  1684. _ = c(cmd)
  1685. return cmd
  1686. }
  1687. func (c cmdable) ZRank(key, member string) *IntCmd {
  1688. cmd := NewIntCmd("zrank", key, member)
  1689. _ = c(cmd)
  1690. return cmd
  1691. }
  1692. func (c cmdable) ZRem(key string, members ...interface{}) *IntCmd {
  1693. args := make([]interface{}, 2, 2+len(members))
  1694. args[0] = "zrem"
  1695. args[1] = key
  1696. args = appendArgs(args, members)
  1697. cmd := NewIntCmd(args...)
  1698. _ = c(cmd)
  1699. return cmd
  1700. }
  1701. func (c cmdable) ZRemRangeByRank(key string, start, stop int64) *IntCmd {
  1702. cmd := NewIntCmd(
  1703. "zremrangebyrank",
  1704. key,
  1705. start,
  1706. stop,
  1707. )
  1708. _ = c(cmd)
  1709. return cmd
  1710. }
  1711. func (c cmdable) ZRemRangeByScore(key, min, max string) *IntCmd {
  1712. cmd := NewIntCmd("zremrangebyscore", key, min, max)
  1713. _ = c(cmd)
  1714. return cmd
  1715. }
  1716. func (c cmdable) ZRemRangeByLex(key, min, max string) *IntCmd {
  1717. cmd := NewIntCmd("zremrangebylex", key, min, max)
  1718. _ = c(cmd)
  1719. return cmd
  1720. }
  1721. func (c cmdable) ZRevRange(key string, start, stop int64) *StringSliceCmd {
  1722. cmd := NewStringSliceCmd("zrevrange", key, start, stop)
  1723. _ = c(cmd)
  1724. return cmd
  1725. }
  1726. func (c cmdable) ZRevRangeWithScores(key string, start, stop int64) *ZSliceCmd {
  1727. cmd := NewZSliceCmd("zrevrange", key, start, stop, "withscores")
  1728. _ = c(cmd)
  1729. return cmd
  1730. }
  1731. func (c cmdable) zRevRangeBy(zcmd, key string, opt *ZRangeBy) *StringSliceCmd {
  1732. args := []interface{}{zcmd, key, opt.Max, opt.Min}
  1733. if opt.Offset != 0 || opt.Count != 0 {
  1734. args = append(
  1735. args,
  1736. "limit",
  1737. opt.Offset,
  1738. opt.Count,
  1739. )
  1740. }
  1741. cmd := NewStringSliceCmd(args...)
  1742. _ = c(cmd)
  1743. return cmd
  1744. }
  1745. func (c cmdable) ZRevRangeByScore(key string, opt *ZRangeBy) *StringSliceCmd {
  1746. return c.zRevRangeBy("zrevrangebyscore", key, opt)
  1747. }
  1748. func (c cmdable) ZRevRangeByLex(key string, opt *ZRangeBy) *StringSliceCmd {
  1749. return c.zRevRangeBy("zrevrangebylex", key, opt)
  1750. }
  1751. func (c cmdable) ZRevRangeByScoreWithScores(key string, opt *ZRangeBy) *ZSliceCmd {
  1752. args := []interface{}{"zrevrangebyscore", key, opt.Max, opt.Min, "withscores"}
  1753. if opt.Offset != 0 || opt.Count != 0 {
  1754. args = append(
  1755. args,
  1756. "limit",
  1757. opt.Offset,
  1758. opt.Count,
  1759. )
  1760. }
  1761. cmd := NewZSliceCmd(args...)
  1762. _ = c(cmd)
  1763. return cmd
  1764. }
  1765. func (c cmdable) ZRevRank(key, member string) *IntCmd {
  1766. cmd := NewIntCmd("zrevrank", key, member)
  1767. _ = c(cmd)
  1768. return cmd
  1769. }
  1770. func (c cmdable) ZScore(key, member string) *FloatCmd {
  1771. cmd := NewFloatCmd("zscore", key, member)
  1772. _ = c(cmd)
  1773. return cmd
  1774. }
  1775. func (c cmdable) ZUnionStore(dest string, store *ZStore) *IntCmd {
  1776. args := make([]interface{}, 3+len(store.Keys))
  1777. args[0] = "zunionstore"
  1778. args[1] = dest
  1779. args[2] = len(store.Keys)
  1780. for i, key := range store.Keys {
  1781. args[3+i] = key
  1782. }
  1783. if len(store.Weights) > 0 {
  1784. args = append(args, "weights")
  1785. for _, weight := range store.Weights {
  1786. args = append(args, weight)
  1787. }
  1788. }
  1789. if store.Aggregate != "" {
  1790. args = append(args, "aggregate", store.Aggregate)
  1791. }
  1792. cmd := NewIntCmd(args...)
  1793. _ = c(cmd)
  1794. return cmd
  1795. }
  1796. //------------------------------------------------------------------------------
  1797. func (c cmdable) PFAdd(key string, els ...interface{}) *IntCmd {
  1798. args := make([]interface{}, 2, 2+len(els))
  1799. args[0] = "pfadd"
  1800. args[1] = key
  1801. args = appendArgs(args, els)
  1802. cmd := NewIntCmd(args...)
  1803. _ = c(cmd)
  1804. return cmd
  1805. }
  1806. func (c cmdable) PFCount(keys ...string) *IntCmd {
  1807. args := make([]interface{}, 1+len(keys))
  1808. args[0] = "pfcount"
  1809. for i, key := range keys {
  1810. args[1+i] = key
  1811. }
  1812. cmd := NewIntCmd(args...)
  1813. _ = c(cmd)
  1814. return cmd
  1815. }
  1816. func (c cmdable) PFMerge(dest string, keys ...string) *StatusCmd {
  1817. args := make([]interface{}, 2+len(keys))
  1818. args[0] = "pfmerge"
  1819. args[1] = dest
  1820. for i, key := range keys {
  1821. args[2+i] = key
  1822. }
  1823. cmd := NewStatusCmd(args...)
  1824. _ = c(cmd)
  1825. return cmd
  1826. }
  1827. //------------------------------------------------------------------------------
  1828. func (c cmdable) BgRewriteAOF() *StatusCmd {
  1829. cmd := NewStatusCmd("bgrewriteaof")
  1830. _ = c(cmd)
  1831. return cmd
  1832. }
  1833. func (c cmdable) BgSave() *StatusCmd {
  1834. cmd := NewStatusCmd("bgsave")
  1835. _ = c(cmd)
  1836. return cmd
  1837. }
  1838. func (c cmdable) ClientKill(ipPort string) *StatusCmd {
  1839. cmd := NewStatusCmd("client", "kill", ipPort)
  1840. _ = c(cmd)
  1841. return cmd
  1842. }
  1843. // ClientKillByFilter is new style synx, while the ClientKill is old
  1844. // CLIENT KILL <option> [value] ... <option> [value]
  1845. func (c cmdable) ClientKillByFilter(keys ...string) *IntCmd {
  1846. args := make([]interface{}, 2+len(keys))
  1847. args[0] = "client"
  1848. args[1] = "kill"
  1849. for i, key := range keys {
  1850. args[2+i] = key
  1851. }
  1852. cmd := NewIntCmd(args...)
  1853. _ = c(cmd)
  1854. return cmd
  1855. }
  1856. func (c cmdable) ClientList() *StringCmd {
  1857. cmd := NewStringCmd("client", "list")
  1858. _ = c(cmd)
  1859. return cmd
  1860. }
  1861. func (c cmdable) ClientPause(dur time.Duration) *BoolCmd {
  1862. cmd := NewBoolCmd("client", "pause", formatMs(dur))
  1863. _ = c(cmd)
  1864. return cmd
  1865. }
  1866. func (c cmdable) ClientID() *IntCmd {
  1867. cmd := NewIntCmd("client", "id")
  1868. _ = c(cmd)
  1869. return cmd
  1870. }
  1871. func (c cmdable) ClientUnblock(id int64) *IntCmd {
  1872. cmd := NewIntCmd("client", "unblock", id)
  1873. _ = c(cmd)
  1874. return cmd
  1875. }
  1876. func (c cmdable) ClientUnblockWithError(id int64) *IntCmd {
  1877. cmd := NewIntCmd("client", "unblock", id, "error")
  1878. _ = c(cmd)
  1879. return cmd
  1880. }
  1881. // ClientSetName assigns a name to the connection.
  1882. func (c statefulCmdable) ClientSetName(name string) *BoolCmd {
  1883. cmd := NewBoolCmd("client", "setname", name)
  1884. _ = c(cmd)
  1885. return cmd
  1886. }
  1887. // ClientGetName returns the name of the connection.
  1888. func (c cmdable) ClientGetName() *StringCmd {
  1889. cmd := NewStringCmd("client", "getname")
  1890. _ = c(cmd)
  1891. return cmd
  1892. }
  1893. func (c cmdable) ConfigGet(parameter string) *SliceCmd {
  1894. cmd := NewSliceCmd("config", "get", parameter)
  1895. _ = c(cmd)
  1896. return cmd
  1897. }
  1898. func (c cmdable) ConfigResetStat() *StatusCmd {
  1899. cmd := NewStatusCmd("config", "resetstat")
  1900. _ = c(cmd)
  1901. return cmd
  1902. }
  1903. func (c cmdable) ConfigSet(parameter, value string) *StatusCmd {
  1904. cmd := NewStatusCmd("config", "set", parameter, value)
  1905. _ = c(cmd)
  1906. return cmd
  1907. }
  1908. func (c cmdable) ConfigRewrite() *StatusCmd {
  1909. cmd := NewStatusCmd("config", "rewrite")
  1910. _ = c(cmd)
  1911. return cmd
  1912. }
  1913. // Deperecated. Use DBSize instead.
  1914. func (c cmdable) DbSize() *IntCmd {
  1915. return c.DBSize()
  1916. }
  1917. func (c cmdable) DBSize() *IntCmd {
  1918. cmd := NewIntCmd("dbsize")
  1919. _ = c(cmd)
  1920. return cmd
  1921. }
  1922. func (c cmdable) FlushAll() *StatusCmd {
  1923. cmd := NewStatusCmd("flushall")
  1924. _ = c(cmd)
  1925. return cmd
  1926. }
  1927. func (c cmdable) FlushAllAsync() *StatusCmd {
  1928. cmd := NewStatusCmd("flushall", "async")
  1929. _ = c(cmd)
  1930. return cmd
  1931. }
  1932. func (c cmdable) FlushDB() *StatusCmd {
  1933. cmd := NewStatusCmd("flushdb")
  1934. _ = c(cmd)
  1935. return cmd
  1936. }
  1937. func (c cmdable) FlushDBAsync() *StatusCmd {
  1938. cmd := NewStatusCmd("flushdb", "async")
  1939. _ = c(cmd)
  1940. return cmd
  1941. }
  1942. func (c cmdable) Info(section ...string) *StringCmd {
  1943. args := []interface{}{"info"}
  1944. if len(section) > 0 {
  1945. args = append(args, section[0])
  1946. }
  1947. cmd := NewStringCmd(args...)
  1948. _ = c(cmd)
  1949. return cmd
  1950. }
  1951. func (c cmdable) LastSave() *IntCmd {
  1952. cmd := NewIntCmd("lastsave")
  1953. _ = c(cmd)
  1954. return cmd
  1955. }
  1956. func (c cmdable) Save() *StatusCmd {
  1957. cmd := NewStatusCmd("save")
  1958. _ = c(cmd)
  1959. return cmd
  1960. }
  1961. func (c cmdable) shutdown(modifier string) *StatusCmd {
  1962. var args []interface{}
  1963. if modifier == "" {
  1964. args = []interface{}{"shutdown"}
  1965. } else {
  1966. args = []interface{}{"shutdown", modifier}
  1967. }
  1968. cmd := NewStatusCmd(args...)
  1969. _ = c(cmd)
  1970. if err := cmd.Err(); err != nil {
  1971. if err == io.EOF {
  1972. // Server quit as expected.
  1973. cmd.err = nil
  1974. }
  1975. } else {
  1976. // Server did not quit. String reply contains the reason.
  1977. cmd.err = errors.New(cmd.val)
  1978. cmd.val = ""
  1979. }
  1980. return cmd
  1981. }
  1982. func (c cmdable) Shutdown() *StatusCmd {
  1983. return c.shutdown("")
  1984. }
  1985. func (c cmdable) ShutdownSave() *StatusCmd {
  1986. return c.shutdown("save")
  1987. }
  1988. func (c cmdable) ShutdownNoSave() *StatusCmd {
  1989. return c.shutdown("nosave")
  1990. }
  1991. func (c cmdable) SlaveOf(host, port string) *StatusCmd {
  1992. cmd := NewStatusCmd("slaveof", host, port)
  1993. _ = c(cmd)
  1994. return cmd
  1995. }
  1996. func (c cmdable) SlowLog() {
  1997. panic("not implemented")
  1998. }
  1999. func (c cmdable) Sync() {
  2000. panic("not implemented")
  2001. }
  2002. func (c cmdable) Time() *TimeCmd {
  2003. cmd := NewTimeCmd("time")
  2004. _ = c(cmd)
  2005. return cmd
  2006. }
  2007. //------------------------------------------------------------------------------
  2008. func (c cmdable) Eval(script string, keys []string, args ...interface{}) *Cmd {
  2009. cmdArgs := make([]interface{}, 3+len(keys), 3+len(keys)+len(args))
  2010. cmdArgs[0] = "eval"
  2011. cmdArgs[1] = script
  2012. cmdArgs[2] = len(keys)
  2013. for i, key := range keys {
  2014. cmdArgs[3+i] = key
  2015. }
  2016. cmdArgs = appendArgs(cmdArgs, args)
  2017. cmd := NewCmd(cmdArgs...)
  2018. _ = c(cmd)
  2019. return cmd
  2020. }
  2021. func (c cmdable) EvalSha(sha1 string, keys []string, args ...interface{}) *Cmd {
  2022. cmdArgs := make([]interface{}, 3+len(keys), 3+len(keys)+len(args))
  2023. cmdArgs[0] = "evalsha"
  2024. cmdArgs[1] = sha1
  2025. cmdArgs[2] = len(keys)
  2026. for i, key := range keys {
  2027. cmdArgs[3+i] = key
  2028. }
  2029. cmdArgs = appendArgs(cmdArgs, args)
  2030. cmd := NewCmd(cmdArgs...)
  2031. _ = c(cmd)
  2032. return cmd
  2033. }
  2034. func (c cmdable) ScriptExists(hashes ...string) *BoolSliceCmd {
  2035. args := make([]interface{}, 2+len(hashes))
  2036. args[0] = "script"
  2037. args[1] = "exists"
  2038. for i, hash := range hashes {
  2039. args[2+i] = hash
  2040. }
  2041. cmd := NewBoolSliceCmd(args...)
  2042. _ = c(cmd)
  2043. return cmd
  2044. }
  2045. func (c cmdable) ScriptFlush() *StatusCmd {
  2046. cmd := NewStatusCmd("script", "flush")
  2047. _ = c(cmd)
  2048. return cmd
  2049. }
  2050. func (c cmdable) ScriptKill() *StatusCmd {
  2051. cmd := NewStatusCmd("script", "kill")
  2052. _ = c(cmd)
  2053. return cmd
  2054. }
  2055. func (c cmdable) ScriptLoad(script string) *StringCmd {
  2056. cmd := NewStringCmd("script", "load", script)
  2057. _ = c(cmd)
  2058. return cmd
  2059. }
  2060. //------------------------------------------------------------------------------
  2061. func (c cmdable) DebugObject(key string) *StringCmd {
  2062. cmd := NewStringCmd("debug", "object", key)
  2063. _ = c(cmd)
  2064. return cmd
  2065. }
  2066. //------------------------------------------------------------------------------
  2067. // Publish posts the message to the channel.
  2068. func (c cmdable) Publish(channel string, message interface{}) *IntCmd {
  2069. cmd := NewIntCmd("publish", channel, message)
  2070. _ = c(cmd)
  2071. return cmd
  2072. }
  2073. func (c cmdable) PubSubChannels(pattern string) *StringSliceCmd {
  2074. args := []interface{}{"pubsub", "channels"}
  2075. if pattern != "*" {
  2076. args = append(args, pattern)
  2077. }
  2078. cmd := NewStringSliceCmd(args...)
  2079. _ = c(cmd)
  2080. return cmd
  2081. }
  2082. func (c cmdable) PubSubNumSub(channels ...string) *StringIntMapCmd {
  2083. args := make([]interface{}, 2+len(channels))
  2084. args[0] = "pubsub"
  2085. args[1] = "numsub"
  2086. for i, channel := range channels {
  2087. args[2+i] = channel
  2088. }
  2089. cmd := NewStringIntMapCmd(args...)
  2090. _ = c(cmd)
  2091. return cmd
  2092. }
  2093. func (c cmdable) PubSubNumPat() *IntCmd {
  2094. cmd := NewIntCmd("pubsub", "numpat")
  2095. _ = c(cmd)
  2096. return cmd
  2097. }
  2098. //------------------------------------------------------------------------------
  2099. func (c cmdable) ClusterSlots() *ClusterSlotsCmd {
  2100. cmd := NewClusterSlotsCmd("cluster", "slots")
  2101. _ = c(cmd)
  2102. return cmd
  2103. }
  2104. func (c cmdable) ClusterNodes() *StringCmd {
  2105. cmd := NewStringCmd("cluster", "nodes")
  2106. _ = c(cmd)
  2107. return cmd
  2108. }
  2109. func (c cmdable) ClusterMeet(host, port string) *StatusCmd {
  2110. cmd := NewStatusCmd("cluster", "meet", host, port)
  2111. _ = c(cmd)
  2112. return cmd
  2113. }
  2114. func (c cmdable) ClusterForget(nodeID string) *StatusCmd {
  2115. cmd := NewStatusCmd("cluster", "forget", nodeID)
  2116. _ = c(cmd)
  2117. return cmd
  2118. }
  2119. func (c cmdable) ClusterReplicate(nodeID string) *StatusCmd {
  2120. cmd := NewStatusCmd("cluster", "replicate", nodeID)
  2121. _ = c(cmd)
  2122. return cmd
  2123. }
  2124. func (c cmdable) ClusterResetSoft() *StatusCmd {
  2125. cmd := NewStatusCmd("cluster", "reset", "soft")
  2126. _ = c(cmd)
  2127. return cmd
  2128. }
  2129. func (c cmdable) ClusterResetHard() *StatusCmd {
  2130. cmd := NewStatusCmd("cluster", "reset", "hard")
  2131. _ = c(cmd)
  2132. return cmd
  2133. }
  2134. func (c cmdable) ClusterInfo() *StringCmd {
  2135. cmd := NewStringCmd("cluster", "info")
  2136. _ = c(cmd)
  2137. return cmd
  2138. }
  2139. func (c cmdable) ClusterKeySlot(key string) *IntCmd {
  2140. cmd := NewIntCmd("cluster", "keyslot", key)
  2141. _ = c(cmd)
  2142. return cmd
  2143. }
  2144. func (c cmdable) ClusterGetKeysInSlot(slot int, count int) *StringSliceCmd {
  2145. cmd := NewStringSliceCmd("cluster", "getkeysinslot", slot, count)
  2146. _ = c(cmd)
  2147. return cmd
  2148. }
  2149. func (c cmdable) ClusterCountFailureReports(nodeID string) *IntCmd {
  2150. cmd := NewIntCmd("cluster", "count-failure-reports", nodeID)
  2151. _ = c(cmd)
  2152. return cmd
  2153. }
  2154. func (c cmdable) ClusterCountKeysInSlot(slot int) *IntCmd {
  2155. cmd := NewIntCmd("cluster", "countkeysinslot", slot)
  2156. _ = c(cmd)
  2157. return cmd
  2158. }
  2159. func (c cmdable) ClusterDelSlots(slots ...int) *StatusCmd {
  2160. args := make([]interface{}, 2+len(slots))
  2161. args[0] = "cluster"
  2162. args[1] = "delslots"
  2163. for i, slot := range slots {
  2164. args[2+i] = slot
  2165. }
  2166. cmd := NewStatusCmd(args...)
  2167. _ = c(cmd)
  2168. return cmd
  2169. }
  2170. func (c cmdable) ClusterDelSlotsRange(min, max int) *StatusCmd {
  2171. size := max - min + 1
  2172. slots := make([]int, size)
  2173. for i := 0; i < size; i++ {
  2174. slots[i] = min + i
  2175. }
  2176. return c.ClusterDelSlots(slots...)
  2177. }
  2178. func (c cmdable) ClusterSaveConfig() *StatusCmd {
  2179. cmd := NewStatusCmd("cluster", "saveconfig")
  2180. _ = c(cmd)
  2181. return cmd
  2182. }
  2183. func (c cmdable) ClusterSlaves(nodeID string) *StringSliceCmd {
  2184. cmd := NewStringSliceCmd("cluster", "slaves", nodeID)
  2185. _ = c(cmd)
  2186. return cmd
  2187. }
  2188. func (c cmdable) ReadOnly() *StatusCmd {
  2189. cmd := NewStatusCmd("readonly")
  2190. _ = c(cmd)
  2191. return cmd
  2192. }
  2193. func (c cmdable) ReadWrite() *StatusCmd {
  2194. cmd := NewStatusCmd("readwrite")
  2195. _ = c(cmd)
  2196. return cmd
  2197. }
  2198. func (c cmdable) ClusterFailover() *StatusCmd {
  2199. cmd := NewStatusCmd("cluster", "failover")
  2200. _ = c(cmd)
  2201. return cmd
  2202. }
  2203. func (c cmdable) ClusterAddSlots(slots ...int) *StatusCmd {
  2204. args := make([]interface{}, 2+len(slots))
  2205. args[0] = "cluster"
  2206. args[1] = "addslots"
  2207. for i, num := range slots {
  2208. args[2+i] = num
  2209. }
  2210. cmd := NewStatusCmd(args...)
  2211. _ = c(cmd)
  2212. return cmd
  2213. }
  2214. func (c cmdable) ClusterAddSlotsRange(min, max int) *StatusCmd {
  2215. size := max - min + 1
  2216. slots := make([]int, size)
  2217. for i := 0; i < size; i++ {
  2218. slots[i] = min + i
  2219. }
  2220. return c.ClusterAddSlots(slots...)
  2221. }
  2222. //------------------------------------------------------------------------------
  2223. func (c cmdable) GeoAdd(key string, geoLocation ...*GeoLocation) *IntCmd {
  2224. args := make([]interface{}, 2+3*len(geoLocation))
  2225. args[0] = "geoadd"
  2226. args[1] = key
  2227. for i, eachLoc := range geoLocation {
  2228. args[2+3*i] = eachLoc.Longitude
  2229. args[2+3*i+1] = eachLoc.Latitude
  2230. args[2+3*i+2] = eachLoc.Name
  2231. }
  2232. cmd := NewIntCmd(args...)
  2233. _ = c(cmd)
  2234. return cmd
  2235. }
  2236. // GeoRadius is a read-only GEORADIUS_RO command.
  2237. func (c cmdable) GeoRadius(key string, longitude, latitude float64, query *GeoRadiusQuery) *GeoLocationCmd {
  2238. cmd := NewGeoLocationCmd(query, "georadius_ro", key, longitude, latitude)
  2239. if query.Store != "" || query.StoreDist != "" {
  2240. cmd.setErr(errors.New("GeoRadius does not support Store or StoreDist"))
  2241. return cmd
  2242. }
  2243. _ = c(cmd)
  2244. return cmd
  2245. }
  2246. // GeoRadiusStore is a writing GEORADIUS command.
  2247. func (c cmdable) GeoRadiusStore(key string, longitude, latitude float64, query *GeoRadiusQuery) *IntCmd {
  2248. args := geoLocationArgs(query, "georadius", key, longitude, latitude)
  2249. cmd := NewIntCmd(args...)
  2250. if query.Store == "" && query.StoreDist == "" {
  2251. cmd.setErr(errors.New("GeoRadiusStore requires Store or StoreDist"))
  2252. return cmd
  2253. }
  2254. _ = c(cmd)
  2255. return cmd
  2256. }
  2257. // GeoRadius is a read-only GEORADIUSBYMEMBER_RO command.
  2258. func (c cmdable) GeoRadiusByMember(key, member string, query *GeoRadiusQuery) *GeoLocationCmd {
  2259. cmd := NewGeoLocationCmd(query, "georadiusbymember_ro", key, member)
  2260. if query.Store != "" || query.StoreDist != "" {
  2261. cmd.setErr(errors.New("GeoRadiusByMember does not support Store or StoreDist"))
  2262. return cmd
  2263. }
  2264. _ = c(cmd)
  2265. return cmd
  2266. }
  2267. // GeoRadiusByMemberStore is a writing GEORADIUSBYMEMBER command.
  2268. func (c cmdable) GeoRadiusByMemberStore(key, member string, query *GeoRadiusQuery) *IntCmd {
  2269. args := geoLocationArgs(query, "georadiusbymember", key, member)
  2270. cmd := NewIntCmd(args...)
  2271. if query.Store == "" && query.StoreDist == "" {
  2272. cmd.setErr(errors.New("GeoRadiusByMemberStore requires Store or StoreDist"))
  2273. return cmd
  2274. }
  2275. _ = c(cmd)
  2276. return cmd
  2277. }
  2278. func (c cmdable) GeoDist(key string, member1, member2, unit string) *FloatCmd {
  2279. if unit == "" {
  2280. unit = "km"
  2281. }
  2282. cmd := NewFloatCmd("geodist", key, member1, member2, unit)
  2283. _ = c(cmd)
  2284. return cmd
  2285. }
  2286. func (c cmdable) GeoHash(key string, members ...string) *StringSliceCmd {
  2287. args := make([]interface{}, 2+len(members))
  2288. args[0] = "geohash"
  2289. args[1] = key
  2290. for i, member := range members {
  2291. args[2+i] = member
  2292. }
  2293. cmd := NewStringSliceCmd(args...)
  2294. _ = c(cmd)
  2295. return cmd
  2296. }
  2297. func (c cmdable) GeoPos(key string, members ...string) *GeoPosCmd {
  2298. args := make([]interface{}, 2+len(members))
  2299. args[0] = "geopos"
  2300. args[1] = key
  2301. for i, member := range members {
  2302. args[2+i] = member
  2303. }
  2304. cmd := NewGeoPosCmd(args...)
  2305. _ = c(cmd)
  2306. return cmd
  2307. }
  2308. //------------------------------------------------------------------------------
  2309. func (c cmdable) MemoryUsage(key string, samples ...int) *IntCmd {
  2310. args := []interface{}{"memory", "usage", key}
  2311. if len(samples) > 0 {
  2312. if len(samples) != 1 {
  2313. panic("MemoryUsage expects single sample count")
  2314. }
  2315. args = append(args, "SAMPLES", samples[0])
  2316. }
  2317. cmd := NewIntCmd(args...)
  2318. _ = c(cmd)
  2319. return cmd
  2320. }