12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078 |
- package redis
- import (
- "io"
- "strconv"
- "time"
- "gopkg.in/redis.v5/internal"
- )
- func readTimeout(timeout time.Duration) time.Duration {
- if timeout == 0 {
- return 0
- }
- return timeout + time.Second
- }
- func usePrecise(dur time.Duration) bool {
- return dur < time.Second || dur%time.Second != 0
- }
- func formatMs(dur time.Duration) string {
- if dur > 0 && dur < time.Millisecond {
- internal.Logf(
- "specified duration is %s, but minimal supported value is %s",
- dur, time.Millisecond,
- )
- }
- return strconv.FormatInt(int64(dur/time.Millisecond), 10)
- }
- func formatSec(dur time.Duration) string {
- if dur > 0 && dur < time.Second {
- internal.Logf(
- "specified duration is %s, but minimal supported value is %s",
- dur, time.Second,
- )
- }
- return strconv.FormatInt(int64(dur/time.Second), 10)
- }
- type Cmdable interface {
- Pipeline() *Pipeline
- Pipelined(fn func(*Pipeline) error) ([]Cmder, error)
- Echo(message interface{}) *StringCmd
- Ping() *StatusCmd
- Quit() *StatusCmd
- Del(keys ...string) *IntCmd
- Unlink(keys ...string) *IntCmd
- Dump(key string) *StringCmd
- Exists(key string) *BoolCmd
- // TODO: merge with Exists in v6
- ExistsMulti(keys ...string) *IntCmd
- Expire(key string, expiration time.Duration) *BoolCmd
- ExpireAt(key string, tm time.Time) *BoolCmd
- Keys(pattern string) *StringSliceCmd
- Migrate(host, port, key string, db int64, timeout time.Duration) *StatusCmd
- Move(key string, db int64) *BoolCmd
- ObjectRefCount(key string) *IntCmd
- ObjectEncoding(key string) *StringCmd
- ObjectIdleTime(key string) *DurationCmd
- Persist(key string) *BoolCmd
- PExpire(key string, expiration time.Duration) *BoolCmd
- PExpireAt(key string, tm time.Time) *BoolCmd
- PTTL(key string) *DurationCmd
- RandomKey() *StringCmd
- Rename(key, newkey string) *StatusCmd
- RenameNX(key, newkey string) *BoolCmd
- Restore(key string, ttl time.Duration, value string) *StatusCmd
- RestoreReplace(key string, ttl time.Duration, value string) *StatusCmd
- Sort(key string, sort Sort) *StringSliceCmd
- SortInterfaces(key string, sort Sort) *SliceCmd
- TTL(key string) *DurationCmd
- Type(key string) *StatusCmd
- Scan(cursor uint64, match string, count int64) *ScanCmd
- SScan(key string, cursor uint64, match string, count int64) *ScanCmd
- HScan(key string, cursor uint64, match string, count int64) *ScanCmd
- ZScan(key string, cursor uint64, match string, count int64) *ScanCmd
- Append(key, value string) *IntCmd
- BitCount(key string, bitCount *BitCount) *IntCmd
- BitOpAnd(destKey string, keys ...string) *IntCmd
- BitOpOr(destKey string, keys ...string) *IntCmd
- BitOpXor(destKey string, keys ...string) *IntCmd
- BitOpNot(destKey string, key string) *IntCmd
- BitPos(key string, bit int64, pos ...int64) *IntCmd
- Decr(key string) *IntCmd
- DecrBy(key string, decrement int64) *IntCmd
- Get(key string) *StringCmd
- GetBit(key string, offset int64) *IntCmd
- GetRange(key string, start, end int64) *StringCmd
- GetSet(key string, value interface{}) *StringCmd
- Incr(key string) *IntCmd
- IncrBy(key string, value int64) *IntCmd
- IncrByFloat(key string, value float64) *FloatCmd
- MGet(keys ...string) *SliceCmd
- MSet(pairs ...interface{}) *StatusCmd
- MSetNX(pairs ...interface{}) *BoolCmd
- Set(key string, value interface{}, expiration time.Duration) *StatusCmd
- SetBit(key string, offset int64, value int) *IntCmd
- SetNX(key string, value interface{}, expiration time.Duration) *BoolCmd
- SetXX(key string, value interface{}, expiration time.Duration) *BoolCmd
- SetRange(key string, offset int64, value string) *IntCmd
- StrLen(key string) *IntCmd
- HDel(key string, fields ...string) *IntCmd
- HExists(key, field string) *BoolCmd
- HGet(key, field string) *StringCmd
- HGetAll(key string) *StringStringMapCmd
- HIncrBy(key, field string, incr int64) *IntCmd
- HIncrByFloat(key, field string, incr float64) *FloatCmd
- HKeys(key string) *StringSliceCmd
- HLen(key string) *IntCmd
- HMGet(key string, fields ...string) *SliceCmd
- HMSet(key string, fields map[string]string) *StatusCmd
- HSet(key, field string, value interface{}) *BoolCmd
- HSetNX(key, field string, value interface{}) *BoolCmd
- HVals(key string) *StringSliceCmd
- BLPop(timeout time.Duration, keys ...string) *StringSliceCmd
- BRPop(timeout time.Duration, keys ...string) *StringSliceCmd
- BRPopLPush(source, destination string, timeout time.Duration) *StringCmd
- LIndex(key string, index int64) *StringCmd
- LInsert(key, op string, pivot, value interface{}) *IntCmd
- LInsertBefore(key string, pivot, value interface{}) *IntCmd
- LInsertAfter(key string, pivot, value interface{}) *IntCmd
- LLen(key string) *IntCmd
- LPop(key string) *StringCmd
- LPush(key string, values ...interface{}) *IntCmd
- LPushX(key string, value interface{}) *IntCmd
- LRange(key string, start, stop int64) *StringSliceCmd
- LRem(key string, count int64, value interface{}) *IntCmd
- LSet(key string, index int64, value interface{}) *StatusCmd
- LTrim(key string, start, stop int64) *StatusCmd
- RPop(key string) *StringCmd
- RPopLPush(source, destination string) *StringCmd
- RPush(key string, values ...interface{}) *IntCmd
- RPushX(key string, value interface{}) *IntCmd
- SAdd(key string, members ...interface{}) *IntCmd
- SCard(key string) *IntCmd
- SDiff(keys ...string) *StringSliceCmd
- SDiffStore(destination string, keys ...string) *IntCmd
- SInter(keys ...string) *StringSliceCmd
- SInterStore(destination string, keys ...string) *IntCmd
- SIsMember(key string, member interface{}) *BoolCmd
- SMembers(key string) *StringSliceCmd
- SMove(source, destination string, member interface{}) *BoolCmd
- SPop(key string) *StringCmd
- SPopN(key string, count int64) *StringSliceCmd
- SRandMember(key string) *StringCmd
- SRandMemberN(key string, count int64) *StringSliceCmd
- SRem(key string, members ...interface{}) *IntCmd
- SUnion(keys ...string) *StringSliceCmd
- SUnionStore(destination string, keys ...string) *IntCmd
- ZAdd(key string, members ...Z) *IntCmd
- ZAddNX(key string, members ...Z) *IntCmd
- ZAddXX(key string, members ...Z) *IntCmd
- ZAddCh(key string, members ...Z) *IntCmd
- ZAddNXCh(key string, members ...Z) *IntCmd
- ZAddXXCh(key string, members ...Z) *IntCmd
- ZIncr(key string, member Z) *FloatCmd
- ZIncrNX(key string, member Z) *FloatCmd
- ZIncrXX(key string, member Z) *FloatCmd
- ZCard(key string) *IntCmd
- ZCount(key, min, max string) *IntCmd
- ZIncrBy(key string, increment float64, member string) *FloatCmd
- ZInterStore(destination string, store ZStore, keys ...string) *IntCmd
- ZRange(key string, start, stop int64) *StringSliceCmd
- ZRangeWithScores(key string, start, stop int64) *ZSliceCmd
- ZRangeByScore(key string, opt ZRangeBy) *StringSliceCmd
- ZRangeByLex(key string, opt ZRangeBy) *StringSliceCmd
- ZRangeByScoreWithScores(key string, opt ZRangeBy) *ZSliceCmd
- ZRank(key, member string) *IntCmd
- ZRem(key string, members ...interface{}) *IntCmd
- ZRemRangeByRank(key string, start, stop int64) *IntCmd
- ZRemRangeByScore(key, min, max string) *IntCmd
- ZRemRangeByLex(key, min, max string) *IntCmd
- ZRevRange(key string, start, stop int64) *StringSliceCmd
- ZRevRangeWithScores(key string, start, stop int64) *ZSliceCmd
- ZRevRangeByScore(key string, opt ZRangeBy) *StringSliceCmd
- ZRevRangeByLex(key string, opt ZRangeBy) *StringSliceCmd
- ZRevRangeByScoreWithScores(key string, opt ZRangeBy) *ZSliceCmd
- ZRevRank(key, member string) *IntCmd
- ZScore(key, member string) *FloatCmd
- ZUnionStore(dest string, store ZStore, keys ...string) *IntCmd
- PFAdd(key string, els ...interface{}) *IntCmd
- PFCount(keys ...string) *IntCmd
- PFMerge(dest string, keys ...string) *StatusCmd
- BgRewriteAOF() *StatusCmd
- BgSave() *StatusCmd
- ClientKill(ipPort string) *StatusCmd
- ClientList() *StringCmd
- ClientPause(dur time.Duration) *BoolCmd
- ConfigGet(parameter string) *SliceCmd
- ConfigResetStat() *StatusCmd
- ConfigSet(parameter, value string) *StatusCmd
- DbSize() *IntCmd
- FlushAll() *StatusCmd
- FlushDb() *StatusCmd
- Info(section ...string) *StringCmd
- LastSave() *IntCmd
- Save() *StatusCmd
- Shutdown() *StatusCmd
- ShutdownSave() *StatusCmd
- ShutdownNoSave() *StatusCmd
- SlaveOf(host, port string) *StatusCmd
- Time() *TimeCmd
- Eval(script string, keys []string, args ...interface{}) *Cmd
- EvalSha(sha1 string, keys []string, args ...interface{}) *Cmd
- ScriptExists(scripts ...string) *BoolSliceCmd
- ScriptFlush() *StatusCmd
- ScriptKill() *StatusCmd
- ScriptLoad(script string) *StringCmd
- DebugObject(key string) *StringCmd
- PubSubChannels(pattern string) *StringSliceCmd
- PubSubNumSub(channels ...string) *StringIntMapCmd
- PubSubNumPat() *IntCmd
- ClusterSlots() *ClusterSlotsCmd
- ClusterNodes() *StringCmd
- ClusterMeet(host, port string) *StatusCmd
- ClusterForget(nodeID string) *StatusCmd
- ClusterReplicate(nodeID string) *StatusCmd
- ClusterResetSoft() *StatusCmd
- ClusterResetHard() *StatusCmd
- ClusterInfo() *StringCmd
- ClusterKeySlot(key string) *IntCmd
- ClusterCountFailureReports(nodeID string) *IntCmd
- ClusterCountKeysInSlot(slot int) *IntCmd
- ClusterDelSlots(slots ...int) *StatusCmd
- ClusterDelSlotsRange(min, max int) *StatusCmd
- ClusterSaveConfig() *StatusCmd
- ClusterSlaves(nodeID string) *StringSliceCmd
- ClusterFailover() *StatusCmd
- ClusterAddSlots(slots ...int) *StatusCmd
- ClusterAddSlotsRange(min, max int) *StatusCmd
- GeoAdd(key string, geoLocation ...*GeoLocation) *IntCmd
- GeoPos(key string, members ...string) *GeoPosCmd
- GeoRadius(key string, longitude, latitude float64, query *GeoRadiusQuery) *GeoLocationCmd
- GeoRadiusByMember(key, member string, query *GeoRadiusQuery) *GeoLocationCmd
- GeoDist(key string, member1, member2, unit string) *FloatCmd
- GeoHash(key string, members ...string) *StringSliceCmd
- Command() *CommandsInfoCmd
- }
- var _ Cmdable = (*Client)(nil)
- var _ Cmdable = (*Tx)(nil)
- var _ Cmdable = (*Ring)(nil)
- var _ Cmdable = (*ClusterClient)(nil)
- type cmdable struct {
- process func(cmd Cmder) error
- }
- type statefulCmdable struct {
- process func(cmd Cmder) error
- }
- //------------------------------------------------------------------------------
- func (c *statefulCmdable) Auth(password string) *StatusCmd {
- cmd := NewStatusCmd("auth", password)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) Echo(message interface{}) *StringCmd {
- cmd := NewStringCmd("echo", message)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) Ping() *StatusCmd {
- cmd := NewStatusCmd("ping")
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) Wait(numSlaves int, timeout time.Duration) *IntCmd {
- cmd := NewIntCmd("wait", numSlaves, int(timeout/time.Millisecond))
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) Quit() *StatusCmd {
- panic("not implemented")
- }
- func (c *statefulCmdable) Select(index int) *StatusCmd {
- cmd := NewStatusCmd("select", index)
- c.process(cmd)
- return cmd
- }
- //------------------------------------------------------------------------------
- func (c *cmdable) Del(keys ...string) *IntCmd {
- args := make([]interface{}, 1+len(keys))
- args[0] = "del"
- for i, key := range keys {
- args[1+i] = key
- }
- cmd := NewIntCmd(args...)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) Unlink(keys ...string) *IntCmd {
- args := make([]interface{}, 1+len(keys))
- args[0] = "unlink"
- for i, key := range keys {
- args[1+i] = key
- }
- cmd := NewIntCmd(args...)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) Dump(key string) *StringCmd {
- cmd := NewStringCmd("dump", key)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) Exists(key string) *BoolCmd {
- cmd := NewBoolCmd("exists", key)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ExistsMulti(keys ...string) *IntCmd {
- args := make([]interface{}, 1+len(keys))
- args[0] = "exists"
- for i, key := range keys {
- args[1+i] = key
- }
- cmd := NewIntCmd(args...)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) Expire(key string, expiration time.Duration) *BoolCmd {
- cmd := NewBoolCmd("expire", key, formatSec(expiration))
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ExpireAt(key string, tm time.Time) *BoolCmd {
- cmd := NewBoolCmd("expireat", key, tm.Unix())
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) Keys(pattern string) *StringSliceCmd {
- cmd := NewStringSliceCmd("keys", pattern)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) Migrate(host, port, key string, db int64, timeout time.Duration) *StatusCmd {
- cmd := NewStatusCmd(
- "migrate",
- host,
- port,
- key,
- db,
- formatMs(timeout),
- )
- cmd.setReadTimeout(readTimeout(timeout))
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) Move(key string, db int64) *BoolCmd {
- cmd := NewBoolCmd("move", key, db)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ObjectRefCount(key string) *IntCmd {
- cmd := NewIntCmd("object", "refcount", key)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ObjectEncoding(key string) *StringCmd {
- cmd := NewStringCmd("object", "encoding", key)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ObjectIdleTime(key string) *DurationCmd {
- cmd := NewDurationCmd(time.Second, "object", "idletime", key)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) Persist(key string) *BoolCmd {
- cmd := NewBoolCmd("persist", key)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) PExpire(key string, expiration time.Duration) *BoolCmd {
- cmd := NewBoolCmd("pexpire", key, formatMs(expiration))
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) PExpireAt(key string, tm time.Time) *BoolCmd {
- cmd := NewBoolCmd(
- "pexpireat",
- key,
- tm.UnixNano()/int64(time.Millisecond),
- )
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) PTTL(key string) *DurationCmd {
- cmd := NewDurationCmd(time.Millisecond, "pttl", key)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) RandomKey() *StringCmd {
- cmd := NewStringCmd("randomkey")
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) Rename(key, newkey string) *StatusCmd {
- cmd := NewStatusCmd("rename", key, newkey)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) RenameNX(key, newkey string) *BoolCmd {
- cmd := NewBoolCmd("renamenx", key, newkey)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) Restore(key string, ttl time.Duration, value string) *StatusCmd {
- cmd := NewStatusCmd(
- "restore",
- key,
- formatMs(ttl),
- value,
- )
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) RestoreReplace(key string, ttl time.Duration, value string) *StatusCmd {
- cmd := NewStatusCmd(
- "restore",
- key,
- formatMs(ttl),
- value,
- "replace",
- )
- c.process(cmd)
- return cmd
- }
- type Sort struct {
- By string
- Offset, Count float64
- Get []string
- Order string
- IsAlpha bool
- Store string
- }
- func (sort *Sort) args(key string) []interface{} {
- args := []interface{}{"sort", key}
- if sort.By != "" {
- args = append(args, "by", sort.By)
- }
- if sort.Offset != 0 || sort.Count != 0 {
- args = append(args, "limit", sort.Offset, sort.Count)
- }
- for _, get := range sort.Get {
- args = append(args, "get", get)
- }
- if sort.Order != "" {
- args = append(args, sort.Order)
- }
- if sort.IsAlpha {
- args = append(args, "alpha")
- }
- if sort.Store != "" {
- args = append(args, "store", sort.Store)
- }
- return args
- }
- func (c *cmdable) Sort(key string, sort Sort) *StringSliceCmd {
- cmd := NewStringSliceCmd(sort.args(key)...)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) SortInterfaces(key string, sort Sort) *SliceCmd {
- cmd := NewSliceCmd(sort.args(key)...)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) TTL(key string) *DurationCmd {
- cmd := NewDurationCmd(time.Second, "ttl", key)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) Type(key string) *StatusCmd {
- cmd := NewStatusCmd("type", key)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) Scan(cursor uint64, match string, count int64) *ScanCmd {
- args := []interface{}{"scan", cursor}
- if match != "" {
- args = append(args, "match", match)
- }
- if count > 0 {
- args = append(args, "count", count)
- }
- cmd := NewScanCmd(c.process, args...)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) SScan(key string, cursor uint64, match string, count int64) *ScanCmd {
- args := []interface{}{"sscan", key, cursor}
- if match != "" {
- args = append(args, "match", match)
- }
- if count > 0 {
- args = append(args, "count", count)
- }
- cmd := NewScanCmd(c.process, args...)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) HScan(key string, cursor uint64, match string, count int64) *ScanCmd {
- args := []interface{}{"hscan", key, cursor}
- if match != "" {
- args = append(args, "match", match)
- }
- if count > 0 {
- args = append(args, "count", count)
- }
- cmd := NewScanCmd(c.process, args...)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ZScan(key string, cursor uint64, match string, count int64) *ScanCmd {
- args := []interface{}{"zscan", key, cursor}
- if match != "" {
- args = append(args, "match", match)
- }
- if count > 0 {
- args = append(args, "count", count)
- }
- cmd := NewScanCmd(c.process, args...)
- c.process(cmd)
- return cmd
- }
- //------------------------------------------------------------------------------
- func (c *cmdable) Append(key, value string) *IntCmd {
- cmd := NewIntCmd("append", key, value)
- c.process(cmd)
- return cmd
- }
- type BitCount struct {
- Start, End int64
- }
- func (c *cmdable) BitCount(key string, bitCount *BitCount) *IntCmd {
- args := []interface{}{"bitcount", key}
- if bitCount != nil {
- args = append(
- args,
- bitCount.Start,
- bitCount.End,
- )
- }
- cmd := NewIntCmd(args...)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) bitOp(op, destKey string, keys ...string) *IntCmd {
- args := make([]interface{}, 3+len(keys))
- args[0] = "bitop"
- args[1] = op
- args[2] = destKey
- for i, key := range keys {
- args[3+i] = key
- }
- cmd := NewIntCmd(args...)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) BitOpAnd(destKey string, keys ...string) *IntCmd {
- return c.bitOp("and", destKey, keys...)
- }
- func (c *cmdable) BitOpOr(destKey string, keys ...string) *IntCmd {
- return c.bitOp("or", destKey, keys...)
- }
- func (c *cmdable) BitOpXor(destKey string, keys ...string) *IntCmd {
- return c.bitOp("xor", destKey, keys...)
- }
- func (c *cmdable) BitOpNot(destKey string, key string) *IntCmd {
- return c.bitOp("not", destKey, key)
- }
- func (c *cmdable) BitPos(key string, bit int64, pos ...int64) *IntCmd {
- args := make([]interface{}, 3+len(pos))
- args[0] = "bitpos"
- args[1] = key
- args[2] = bit
- switch len(pos) {
- case 0:
- case 1:
- args[3] = pos[0]
- case 2:
- args[3] = pos[0]
- args[4] = pos[1]
- default:
- panic("too many arguments")
- }
- cmd := NewIntCmd(args...)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) Decr(key string) *IntCmd {
- cmd := NewIntCmd("decr", key)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) DecrBy(key string, decrement int64) *IntCmd {
- cmd := NewIntCmd("decrby", key, decrement)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) Get(key string) *StringCmd {
- cmd := NewStringCmd("get", key)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) GetBit(key string, offset int64) *IntCmd {
- cmd := NewIntCmd("getbit", key, offset)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) GetRange(key string, start, end int64) *StringCmd {
- cmd := NewStringCmd("getrange", key, start, end)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) GetSet(key string, value interface{}) *StringCmd {
- cmd := NewStringCmd("getset", key, value)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) Incr(key string) *IntCmd {
- cmd := NewIntCmd("incr", key)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) IncrBy(key string, value int64) *IntCmd {
- cmd := NewIntCmd("incrby", key, value)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) IncrByFloat(key string, value float64) *FloatCmd {
- cmd := NewFloatCmd("incrbyfloat", key, value)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) MGet(keys ...string) *SliceCmd {
- args := make([]interface{}, 1+len(keys))
- args[0] = "mget"
- for i, key := range keys {
- args[1+i] = key
- }
- cmd := NewSliceCmd(args...)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) MSet(pairs ...interface{}) *StatusCmd {
- args := make([]interface{}, 1+len(pairs))
- args[0] = "mset"
- for i, pair := range pairs {
- args[1+i] = pair
- }
- cmd := NewStatusCmd(args...)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) MSetNX(pairs ...interface{}) *BoolCmd {
- args := make([]interface{}, 1+len(pairs))
- args[0] = "msetnx"
- for i, pair := range pairs {
- args[1+i] = pair
- }
- cmd := NewBoolCmd(args...)
- c.process(cmd)
- return cmd
- }
- // Redis `SET key value [expiration]` command.
- //
- // Use expiration for `SETEX`-like behavior.
- // Zero expiration means the key has no expiration time.
- func (c *cmdable) Set(key string, value interface{}, expiration time.Duration) *StatusCmd {
- args := make([]interface{}, 3, 4)
- args[0] = "set"
- args[1] = key
- args[2] = value
- if expiration > 0 {
- if usePrecise(expiration) {
- args = append(args, "px", formatMs(expiration))
- } else {
- args = append(args, "ex", formatSec(expiration))
- }
- }
- cmd := NewStatusCmd(args...)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) SetBit(key string, offset int64, value int) *IntCmd {
- cmd := NewIntCmd(
- "setbit",
- key,
- offset,
- value,
- )
- c.process(cmd)
- return cmd
- }
- // Redis `SET key value [expiration] NX` command.
- //
- // Zero expiration means the key has no expiration time.
- func (c *cmdable) SetNX(key string, value interface{}, expiration time.Duration) *BoolCmd {
- var cmd *BoolCmd
- if expiration == 0 {
- // Use old `SETNX` to support old Redis versions.
- cmd = NewBoolCmd("setnx", key, value)
- } else {
- if usePrecise(expiration) {
- cmd = NewBoolCmd("set", key, value, "px", formatMs(expiration), "nx")
- } else {
- cmd = NewBoolCmd("set", key, value, "ex", formatSec(expiration), "nx")
- }
- }
- c.process(cmd)
- return cmd
- }
- // Redis `SET key value [expiration] XX` command.
- //
- // Zero expiration means the key has no expiration time.
- func (c *cmdable) SetXX(key string, value interface{}, expiration time.Duration) *BoolCmd {
- var cmd *BoolCmd
- if expiration == 0 {
- cmd = NewBoolCmd("set", key, value, "xx")
- } else {
- if usePrecise(expiration) {
- cmd = NewBoolCmd("set", key, value, "px", formatMs(expiration), "xx")
- } else {
- cmd = NewBoolCmd("set", key, value, "ex", formatSec(expiration), "xx")
- }
- }
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) SetRange(key string, offset int64, value string) *IntCmd {
- cmd := NewIntCmd("setrange", key, offset, value)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) StrLen(key string) *IntCmd {
- cmd := NewIntCmd("strlen", key)
- c.process(cmd)
- return cmd
- }
- //------------------------------------------------------------------------------
- func (c *cmdable) HDel(key string, fields ...string) *IntCmd {
- args := make([]interface{}, 2+len(fields))
- args[0] = "hdel"
- args[1] = key
- for i, field := range fields {
- args[2+i] = field
- }
- cmd := NewIntCmd(args...)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) HExists(key, field string) *BoolCmd {
- cmd := NewBoolCmd("hexists", key, field)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) HGet(key, field string) *StringCmd {
- cmd := NewStringCmd("hget", key, field)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) HGetAll(key string) *StringStringMapCmd {
- cmd := NewStringStringMapCmd("hgetall", key)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) HIncrBy(key, field string, incr int64) *IntCmd {
- cmd := NewIntCmd("hincrby", key, field, incr)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) HIncrByFloat(key, field string, incr float64) *FloatCmd {
- cmd := NewFloatCmd("hincrbyfloat", key, field, incr)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) HKeys(key string) *StringSliceCmd {
- cmd := NewStringSliceCmd("hkeys", key)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) HLen(key string) *IntCmd {
- cmd := NewIntCmd("hlen", key)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) HMGet(key string, fields ...string) *SliceCmd {
- args := make([]interface{}, 2+len(fields))
- args[0] = "hmget"
- args[1] = key
- for i, field := range fields {
- args[2+i] = field
- }
- cmd := NewSliceCmd(args...)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) HMSet(key string, fields map[string]string) *StatusCmd {
- args := make([]interface{}, 2+len(fields)*2)
- args[0] = "hmset"
- args[1] = key
- i := 2
- for k, v := range fields {
- args[i] = k
- args[i+1] = v
- i += 2
- }
- cmd := NewStatusCmd(args...)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) HSet(key, field string, value interface{}) *BoolCmd {
- cmd := NewBoolCmd("hset", key, field, value)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) HSetNX(key, field string, value interface{}) *BoolCmd {
- cmd := NewBoolCmd("hsetnx", key, field, value)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) HVals(key string) *StringSliceCmd {
- cmd := NewStringSliceCmd("hvals", key)
- c.process(cmd)
- return cmd
- }
- //------------------------------------------------------------------------------
- func (c *cmdable) BLPop(timeout time.Duration, keys ...string) *StringSliceCmd {
- args := make([]interface{}, 1+len(keys)+1)
- args[0] = "blpop"
- for i, key := range keys {
- args[1+i] = key
- }
- args[len(args)-1] = formatSec(timeout)
- cmd := NewStringSliceCmd(args...)
- cmd.setReadTimeout(readTimeout(timeout))
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) BRPop(timeout time.Duration, keys ...string) *StringSliceCmd {
- args := make([]interface{}, 1+len(keys)+1)
- args[0] = "brpop"
- for i, key := range keys {
- args[1+i] = key
- }
- args[len(keys)+1] = formatSec(timeout)
- cmd := NewStringSliceCmd(args...)
- cmd.setReadTimeout(readTimeout(timeout))
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) BRPopLPush(source, destination string, timeout time.Duration) *StringCmd {
- cmd := NewStringCmd(
- "brpoplpush",
- source,
- destination,
- formatSec(timeout),
- )
- cmd.setReadTimeout(readTimeout(timeout))
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) LIndex(key string, index int64) *StringCmd {
- cmd := NewStringCmd("lindex", key, index)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) LInsert(key, op string, pivot, value interface{}) *IntCmd {
- cmd := NewIntCmd("linsert", key, op, pivot, value)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) LInsertBefore(key string, pivot, value interface{}) *IntCmd {
- cmd := NewIntCmd("linsert", key, "before", pivot, value)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) LInsertAfter(key string, pivot, value interface{}) *IntCmd {
- cmd := NewIntCmd("linsert", key, "after", pivot, value)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) LLen(key string) *IntCmd {
- cmd := NewIntCmd("llen", key)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) LPop(key string) *StringCmd {
- cmd := NewStringCmd("lpop", key)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) LPush(key string, values ...interface{}) *IntCmd {
- args := make([]interface{}, 2+len(values))
- args[0] = "lpush"
- args[1] = key
- for i, value := range values {
- args[2+i] = value
- }
- cmd := NewIntCmd(args...)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) LPushX(key string, value interface{}) *IntCmd {
- cmd := NewIntCmd("lpushx", key, value)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) LRange(key string, start, stop int64) *StringSliceCmd {
- cmd := NewStringSliceCmd(
- "lrange",
- key,
- start,
- stop,
- )
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) LRem(key string, count int64, value interface{}) *IntCmd {
- cmd := NewIntCmd("lrem", key, count, value)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) LSet(key string, index int64, value interface{}) *StatusCmd {
- cmd := NewStatusCmd("lset", key, index, value)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) LTrim(key string, start, stop int64) *StatusCmd {
- cmd := NewStatusCmd(
- "ltrim",
- key,
- start,
- stop,
- )
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) RPop(key string) *StringCmd {
- cmd := NewStringCmd("rpop", key)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) RPopLPush(source, destination string) *StringCmd {
- cmd := NewStringCmd("rpoplpush", source, destination)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) RPush(key string, values ...interface{}) *IntCmd {
- args := make([]interface{}, 2+len(values))
- args[0] = "rpush"
- args[1] = key
- for i, value := range values {
- args[2+i] = value
- }
- cmd := NewIntCmd(args...)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) RPushX(key string, value interface{}) *IntCmd {
- cmd := NewIntCmd("rpushx", key, value)
- c.process(cmd)
- return cmd
- }
- //------------------------------------------------------------------------------
- func (c *cmdable) SAdd(key string, members ...interface{}) *IntCmd {
- args := make([]interface{}, 2+len(members))
- args[0] = "sadd"
- args[1] = key
- for i, member := range members {
- args[2+i] = member
- }
- cmd := NewIntCmd(args...)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) SCard(key string) *IntCmd {
- cmd := NewIntCmd("scard", key)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) SDiff(keys ...string) *StringSliceCmd {
- args := make([]interface{}, 1+len(keys))
- args[0] = "sdiff"
- for i, key := range keys {
- args[1+i] = key
- }
- cmd := NewStringSliceCmd(args...)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) SDiffStore(destination string, keys ...string) *IntCmd {
- args := make([]interface{}, 2+len(keys))
- args[0] = "sdiffstore"
- args[1] = destination
- for i, key := range keys {
- args[2+i] = key
- }
- cmd := NewIntCmd(args...)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) SInter(keys ...string) *StringSliceCmd {
- args := make([]interface{}, 1+len(keys))
- args[0] = "sinter"
- for i, key := range keys {
- args[1+i] = key
- }
- cmd := NewStringSliceCmd(args...)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) SInterStore(destination string, keys ...string) *IntCmd {
- args := make([]interface{}, 2+len(keys))
- args[0] = "sinterstore"
- args[1] = destination
- for i, key := range keys {
- args[2+i] = key
- }
- cmd := NewIntCmd(args...)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) SIsMember(key string, member interface{}) *BoolCmd {
- cmd := NewBoolCmd("sismember", key, member)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) SMembers(key string) *StringSliceCmd {
- cmd := NewStringSliceCmd("smembers", key)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) SMove(source, destination string, member interface{}) *BoolCmd {
- cmd := NewBoolCmd("smove", source, destination, member)
- c.process(cmd)
- return cmd
- }
- // Redis `SPOP key` command.
- func (c *cmdable) SPop(key string) *StringCmd {
- cmd := NewStringCmd("spop", key)
- c.process(cmd)
- return cmd
- }
- // Redis `SPOP key count` command.
- func (c *cmdable) SPopN(key string, count int64) *StringSliceCmd {
- cmd := NewStringSliceCmd("spop", key, count)
- c.process(cmd)
- return cmd
- }
- // Redis `SRANDMEMBER key` command.
- func (c *cmdable) SRandMember(key string) *StringCmd {
- cmd := NewStringCmd("srandmember", key)
- c.process(cmd)
- return cmd
- }
- // Redis `SRANDMEMBER key count` command.
- func (c *cmdable) SRandMemberN(key string, count int64) *StringSliceCmd {
- cmd := NewStringSliceCmd("srandmember", key, count)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) SRem(key string, members ...interface{}) *IntCmd {
- args := make([]interface{}, 2+len(members))
- args[0] = "srem"
- args[1] = key
- for i, member := range members {
- args[2+i] = member
- }
- cmd := NewIntCmd(args...)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) SUnion(keys ...string) *StringSliceCmd {
- args := make([]interface{}, 1+len(keys))
- args[0] = "sunion"
- for i, key := range keys {
- args[1+i] = key
- }
- cmd := NewStringSliceCmd(args...)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) SUnionStore(destination string, keys ...string) *IntCmd {
- args := make([]interface{}, 2+len(keys))
- args[0] = "sunionstore"
- args[1] = destination
- for i, key := range keys {
- args[2+i] = key
- }
- cmd := NewIntCmd(args...)
- c.process(cmd)
- return cmd
- }
- //------------------------------------------------------------------------------
- // Z represents sorted set member.
- type Z struct {
- Score float64
- Member interface{}
- }
- // ZStore is used as an arg to ZInterStore and ZUnionStore.
- type ZStore struct {
- Weights []float64
- // Can be SUM, MIN or MAX.
- Aggregate string
- }
- func (c *cmdable) zAdd(a []interface{}, n int, members ...Z) *IntCmd {
- for i, m := range members {
- a[n+2*i] = m.Score
- a[n+2*i+1] = m.Member
- }
- cmd := NewIntCmd(a...)
- c.process(cmd)
- return cmd
- }
- // Redis `ZADD key score member [score member ...]` command.
- func (c *cmdable) ZAdd(key string, members ...Z) *IntCmd {
- const n = 2
- a := make([]interface{}, n+2*len(members))
- a[0], a[1] = "zadd", key
- return c.zAdd(a, n, members...)
- }
- // Redis `ZADD key NX score member [score member ...]` command.
- func (c *cmdable) ZAddNX(key string, members ...Z) *IntCmd {
- const n = 3
- a := make([]interface{}, n+2*len(members))
- a[0], a[1], a[2] = "zadd", key, "nx"
- return c.zAdd(a, n, members...)
- }
- // Redis `ZADD key XX score member [score member ...]` command.
- func (c *cmdable) ZAddXX(key string, members ...Z) *IntCmd {
- const n = 3
- a := make([]interface{}, n+2*len(members))
- a[0], a[1], a[2] = "zadd", key, "xx"
- return c.zAdd(a, n, members...)
- }
- // Redis `ZADD key CH score member [score member ...]` command.
- func (c *cmdable) ZAddCh(key string, members ...Z) *IntCmd {
- const n = 3
- a := make([]interface{}, n+2*len(members))
- a[0], a[1], a[2] = "zadd", key, "ch"
- return c.zAdd(a, n, members...)
- }
- // Redis `ZADD key NX CH score member [score member ...]` command.
- func (c *cmdable) ZAddNXCh(key string, members ...Z) *IntCmd {
- const n = 4
- a := make([]interface{}, n+2*len(members))
- a[0], a[1], a[2], a[3] = "zadd", key, "nx", "ch"
- return c.zAdd(a, n, members...)
- }
- // Redis `ZADD key XX CH score member [score member ...]` command.
- func (c *cmdable) ZAddXXCh(key string, members ...Z) *IntCmd {
- const n = 4
- a := make([]interface{}, n+2*len(members))
- a[0], a[1], a[2], a[3] = "zadd", key, "xx", "ch"
- return c.zAdd(a, n, members...)
- }
- func (c *cmdable) zIncr(a []interface{}, n int, members ...Z) *FloatCmd {
- for i, m := range members {
- a[n+2*i] = m.Score
- a[n+2*i+1] = m.Member
- }
- cmd := NewFloatCmd(a...)
- c.process(cmd)
- return cmd
- }
- // Redis `ZADD key INCR score member` command.
- func (c *cmdable) ZIncr(key string, member Z) *FloatCmd {
- const n = 3
- a := make([]interface{}, n+2)
- a[0], a[1], a[2] = "zadd", key, "incr"
- return c.zIncr(a, n, member)
- }
- // Redis `ZADD key NX INCR score member` command.
- func (c *cmdable) ZIncrNX(key string, member Z) *FloatCmd {
- const n = 4
- a := make([]interface{}, n+2)
- a[0], a[1], a[2], a[3] = "zadd", key, "incr", "nx"
- return c.zIncr(a, n, member)
- }
- // Redis `ZADD key XX INCR score member` command.
- func (c *cmdable) ZIncrXX(key string, member Z) *FloatCmd {
- const n = 4
- a := make([]interface{}, n+2)
- a[0], a[1], a[2], a[3] = "zadd", key, "incr", "xx"
- return c.zIncr(a, n, member)
- }
- func (c *cmdable) ZCard(key string) *IntCmd {
- cmd := NewIntCmd("zcard", key)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ZCount(key, min, max string) *IntCmd {
- cmd := NewIntCmd("zcount", key, min, max)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ZIncrBy(key string, increment float64, member string) *FloatCmd {
- cmd := NewFloatCmd("zincrby", key, increment, member)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ZInterStore(destination string, store ZStore, keys ...string) *IntCmd {
- args := make([]interface{}, 3+len(keys))
- args[0] = "zinterstore"
- args[1] = destination
- args[2] = strconv.Itoa(len(keys))
- for i, key := range keys {
- args[3+i] = key
- }
- if len(store.Weights) > 0 {
- args = append(args, "weights")
- for _, weight := range store.Weights {
- args = append(args, weight)
- }
- }
- if store.Aggregate != "" {
- args = append(args, "aggregate", store.Aggregate)
- }
- cmd := NewIntCmd(args...)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) zRange(key string, start, stop int64, withScores bool) *StringSliceCmd {
- args := []interface{}{
- "zrange",
- key,
- start,
- stop,
- }
- if withScores {
- args = append(args, "withscores")
- }
- cmd := NewStringSliceCmd(args...)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ZRange(key string, start, stop int64) *StringSliceCmd {
- return c.zRange(key, start, stop, false)
- }
- func (c *cmdable) ZRangeWithScores(key string, start, stop int64) *ZSliceCmd {
- cmd := NewZSliceCmd("zrange", key, start, stop, "withscores")
- c.process(cmd)
- return cmd
- }
- type ZRangeBy struct {
- Min, Max string
- Offset, Count int64
- }
- func (c *cmdable) zRangeBy(zcmd, key string, opt ZRangeBy, withScores bool) *StringSliceCmd {
- args := []interface{}{zcmd, key, opt.Min, opt.Max}
- if withScores {
- args = append(args, "withscores")
- }
- if opt.Offset != 0 || opt.Count != 0 {
- args = append(
- args,
- "limit",
- opt.Offset,
- opt.Count,
- )
- }
- cmd := NewStringSliceCmd(args...)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ZRangeByScore(key string, opt ZRangeBy) *StringSliceCmd {
- return c.zRangeBy("zrangebyscore", key, opt, false)
- }
- func (c *cmdable) ZRangeByLex(key string, opt ZRangeBy) *StringSliceCmd {
- return c.zRangeBy("zrangebylex", key, opt, false)
- }
- func (c *cmdable) ZRangeByScoreWithScores(key string, opt ZRangeBy) *ZSliceCmd {
- args := []interface{}{"zrangebyscore", key, opt.Min, opt.Max, "withscores"}
- if opt.Offset != 0 || opt.Count != 0 {
- args = append(
- args,
- "limit",
- opt.Offset,
- opt.Count,
- )
- }
- cmd := NewZSliceCmd(args...)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ZRank(key, member string) *IntCmd {
- cmd := NewIntCmd("zrank", key, member)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ZRem(key string, members ...interface{}) *IntCmd {
- args := make([]interface{}, 2+len(members))
- args[0] = "zrem"
- args[1] = key
- for i, member := range members {
- args[2+i] = member
- }
- cmd := NewIntCmd(args...)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ZRemRangeByRank(key string, start, stop int64) *IntCmd {
- cmd := NewIntCmd(
- "zremrangebyrank",
- key,
- start,
- stop,
- )
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ZRemRangeByScore(key, min, max string) *IntCmd {
- cmd := NewIntCmd("zremrangebyscore", key, min, max)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ZRemRangeByLex(key, min, max string) *IntCmd {
- cmd := NewIntCmd("zremrangebylex", key, min, max)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ZRevRange(key string, start, stop int64) *StringSliceCmd {
- cmd := NewStringSliceCmd("zrevrange", key, start, stop)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ZRevRangeWithScores(key string, start, stop int64) *ZSliceCmd {
- cmd := NewZSliceCmd("zrevrange", key, start, stop, "withscores")
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) zRevRangeBy(zcmd, key string, opt ZRangeBy) *StringSliceCmd {
- args := []interface{}{zcmd, key, opt.Max, opt.Min}
- if opt.Offset != 0 || opt.Count != 0 {
- args = append(
- args,
- "limit",
- opt.Offset,
- opt.Count,
- )
- }
- cmd := NewStringSliceCmd(args...)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ZRevRangeByScore(key string, opt ZRangeBy) *StringSliceCmd {
- return c.zRevRangeBy("zrevrangebyscore", key, opt)
- }
- func (c *cmdable) ZRevRangeByLex(key string, opt ZRangeBy) *StringSliceCmd {
- return c.zRevRangeBy("zrevrangebylex", key, opt)
- }
- func (c *cmdable) ZRevRangeByScoreWithScores(key string, opt ZRangeBy) *ZSliceCmd {
- args := []interface{}{"zrevrangebyscore", key, opt.Max, opt.Min, "withscores"}
- if opt.Offset != 0 || opt.Count != 0 {
- args = append(
- args,
- "limit",
- opt.Offset,
- opt.Count,
- )
- }
- cmd := NewZSliceCmd(args...)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ZRevRank(key, member string) *IntCmd {
- cmd := NewIntCmd("zrevrank", key, member)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ZScore(key, member string) *FloatCmd {
- cmd := NewFloatCmd("zscore", key, member)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ZUnionStore(dest string, store ZStore, keys ...string) *IntCmd {
- args := make([]interface{}, 3+len(keys))
- args[0] = "zunionstore"
- args[1] = dest
- args[2] = strconv.Itoa(len(keys))
- for i, key := range keys {
- args[3+i] = key
- }
- if len(store.Weights) > 0 {
- args = append(args, "weights")
- for _, weight := range store.Weights {
- args = append(args, weight)
- }
- }
- if store.Aggregate != "" {
- args = append(args, "aggregate", store.Aggregate)
- }
- cmd := NewIntCmd(args...)
- c.process(cmd)
- return cmd
- }
- //------------------------------------------------------------------------------
- func (c *cmdable) PFAdd(key string, els ...interface{}) *IntCmd {
- args := make([]interface{}, 2+len(els))
- args[0] = "pfadd"
- args[1] = key
- for i, el := range els {
- args[2+i] = el
- }
- cmd := NewIntCmd(args...)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) PFCount(keys ...string) *IntCmd {
- args := make([]interface{}, 1+len(keys))
- args[0] = "pfcount"
- for i, key := range keys {
- args[1+i] = key
- }
- cmd := NewIntCmd(args...)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) PFMerge(dest string, keys ...string) *StatusCmd {
- args := make([]interface{}, 2+len(keys))
- args[0] = "pfmerge"
- args[1] = dest
- for i, key := range keys {
- args[2+i] = key
- }
- cmd := NewStatusCmd(args...)
- c.process(cmd)
- return cmd
- }
- //------------------------------------------------------------------------------
- func (c *cmdable) BgRewriteAOF() *StatusCmd {
- cmd := NewStatusCmd("bgrewriteaof")
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) BgSave() *StatusCmd {
- cmd := NewStatusCmd("bgsave")
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ClientKill(ipPort string) *StatusCmd {
- cmd := NewStatusCmd("client", "kill", ipPort)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ClientList() *StringCmd {
- cmd := NewStringCmd("client", "list")
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ClientPause(dur time.Duration) *BoolCmd {
- cmd := NewBoolCmd("client", "pause", formatMs(dur))
- c.process(cmd)
- return cmd
- }
- // ClientSetName assigns a name to the connection.
- func (c *statefulCmdable) ClientSetName(name string) *BoolCmd {
- cmd := NewBoolCmd("client", "setname", name)
- c.process(cmd)
- return cmd
- }
- // ClientGetName returns the name of the connection.
- func (c *statefulCmdable) ClientGetName() *StringCmd {
- cmd := NewStringCmd("client", "getname")
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ConfigGet(parameter string) *SliceCmd {
- cmd := NewSliceCmd("config", "get", parameter)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ConfigResetStat() *StatusCmd {
- cmd := NewStatusCmd("config", "resetstat")
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ConfigSet(parameter, value string) *StatusCmd {
- cmd := NewStatusCmd("config", "set", parameter, value)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) DbSize() *IntCmd {
- cmd := NewIntCmd("dbsize")
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) FlushAll() *StatusCmd {
- cmd := NewStatusCmd("flushall")
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) FlushDb() *StatusCmd {
- cmd := NewStatusCmd("flushdb")
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) Info(section ...string) *StringCmd {
- args := []interface{}{"info"}
- if len(section) > 0 {
- args = append(args, section[0])
- }
- cmd := NewStringCmd(args...)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) LastSave() *IntCmd {
- cmd := NewIntCmd("lastsave")
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) Save() *StatusCmd {
- cmd := NewStatusCmd("save")
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) shutdown(modifier string) *StatusCmd {
- var args []interface{}
- if modifier == "" {
- args = []interface{}{"shutdown"}
- } else {
- args = []interface{}{"shutdown", modifier}
- }
- cmd := NewStatusCmd(args...)
- c.process(cmd)
- if err := cmd.Err(); err != nil {
- if err == io.EOF {
- // Server quit as expected.
- cmd.err = nil
- }
- } else {
- // Server did not quit. String reply contains the reason.
- cmd.err = internal.RedisError(cmd.val)
- cmd.val = ""
- }
- return cmd
- }
- func (c *cmdable) Shutdown() *StatusCmd {
- return c.shutdown("")
- }
- func (c *cmdable) ShutdownSave() *StatusCmd {
- return c.shutdown("save")
- }
- func (c *cmdable) ShutdownNoSave() *StatusCmd {
- return c.shutdown("nosave")
- }
- func (c *cmdable) SlaveOf(host, port string) *StatusCmd {
- cmd := NewStatusCmd("slaveof", host, port)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) SlowLog() {
- panic("not implemented")
- }
- func (c *cmdable) Sync() {
- panic("not implemented")
- }
- func (c *cmdable) Time() *TimeCmd {
- cmd := NewTimeCmd("time")
- c.process(cmd)
- return cmd
- }
- //------------------------------------------------------------------------------
- func (c *cmdable) Eval(script string, keys []string, args ...interface{}) *Cmd {
- cmdArgs := make([]interface{}, 3+len(keys)+len(args))
- cmdArgs[0] = "eval"
- cmdArgs[1] = script
- cmdArgs[2] = strconv.Itoa(len(keys))
- for i, key := range keys {
- cmdArgs[3+i] = key
- }
- pos := 3 + len(keys)
- for i, arg := range args {
- cmdArgs[pos+i] = arg
- }
- cmd := NewCmd(cmdArgs...)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) EvalSha(sha1 string, keys []string, args ...interface{}) *Cmd {
- cmdArgs := make([]interface{}, 3+len(keys)+len(args))
- cmdArgs[0] = "evalsha"
- cmdArgs[1] = sha1
- cmdArgs[2] = strconv.Itoa(len(keys))
- for i, key := range keys {
- cmdArgs[3+i] = key
- }
- pos := 3 + len(keys)
- for i, arg := range args {
- cmdArgs[pos+i] = arg
- }
- cmd := NewCmd(cmdArgs...)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ScriptExists(scripts ...string) *BoolSliceCmd {
- args := make([]interface{}, 2+len(scripts))
- args[0] = "script"
- args[1] = "exists"
- for i, script := range scripts {
- args[2+i] = script
- }
- cmd := NewBoolSliceCmd(args...)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ScriptFlush() *StatusCmd {
- cmd := NewStatusCmd("script", "flush")
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ScriptKill() *StatusCmd {
- cmd := NewStatusCmd("script", "kill")
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ScriptLoad(script string) *StringCmd {
- cmd := NewStringCmd("script", "load", script)
- c.process(cmd)
- return cmd
- }
- //------------------------------------------------------------------------------
- func (c *cmdable) DebugObject(key string) *StringCmd {
- cmd := NewStringCmd("debug", "object", key)
- c.process(cmd)
- return cmd
- }
- //------------------------------------------------------------------------------
- // Publish posts the message to the channel.
- func (c *cmdable) Publish(channel, message string) *IntCmd {
- cmd := NewIntCmd("PUBLISH", channel, message)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) PubSubChannels(pattern string) *StringSliceCmd {
- args := []interface{}{"pubsub", "channels"}
- if pattern != "*" {
- args = append(args, pattern)
- }
- cmd := NewStringSliceCmd(args...)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) PubSubNumSub(channels ...string) *StringIntMapCmd {
- args := make([]interface{}, 2+len(channels))
- args[0] = "pubsub"
- args[1] = "numsub"
- for i, channel := range channels {
- args[2+i] = channel
- }
- cmd := NewStringIntMapCmd(args...)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) PubSubNumPat() *IntCmd {
- cmd := NewIntCmd("pubsub", "numpat")
- c.process(cmd)
- return cmd
- }
- //------------------------------------------------------------------------------
- func (c *cmdable) ClusterSlots() *ClusterSlotsCmd {
- cmd := NewClusterSlotsCmd("cluster", "slots")
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ClusterNodes() *StringCmd {
- cmd := NewStringCmd("cluster", "nodes")
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ClusterMeet(host, port string) *StatusCmd {
- cmd := NewStatusCmd("cluster", "meet", host, port)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ClusterForget(nodeID string) *StatusCmd {
- cmd := NewStatusCmd("cluster", "forget", nodeID)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ClusterReplicate(nodeID string) *StatusCmd {
- cmd := NewStatusCmd("cluster", "replicate", nodeID)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ClusterResetSoft() *StatusCmd {
- cmd := NewStatusCmd("cluster", "reset", "soft")
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ClusterResetHard() *StatusCmd {
- cmd := NewStatusCmd("cluster", "reset", "hard")
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ClusterInfo() *StringCmd {
- cmd := NewStringCmd("cluster", "info")
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ClusterKeySlot(key string) *IntCmd {
- cmd := NewIntCmd("cluster", "keyslot", key)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ClusterCountFailureReports(nodeID string) *IntCmd {
- cmd := NewIntCmd("cluster", "count-failure-reports", nodeID)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ClusterCountKeysInSlot(slot int) *IntCmd {
- cmd := NewIntCmd("cluster", "countkeysinslot", slot)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ClusterDelSlots(slots ...int) *StatusCmd {
- args := make([]interface{}, 2+len(slots))
- args[0] = "cluster"
- args[1] = "delslots"
- for i, slot := range slots {
- args[2+i] = slot
- }
- cmd := NewStatusCmd(args...)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ClusterDelSlotsRange(min, max int) *StatusCmd {
- size := max - min + 1
- slots := make([]int, size)
- for i := 0; i < size; i++ {
- slots[i] = min + i
- }
- return c.ClusterDelSlots(slots...)
- }
- func (c *cmdable) ClusterSaveConfig() *StatusCmd {
- cmd := NewStatusCmd("cluster", "saveconfig")
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ClusterSlaves(nodeID string) *StringSliceCmd {
- cmd := NewStringSliceCmd("cluster", "slaves", nodeID)
- c.process(cmd)
- return cmd
- }
- func (c *statefulCmdable) ReadOnly() *StatusCmd {
- cmd := NewStatusCmd("readonly")
- c.process(cmd)
- return cmd
- }
- func (c *statefulCmdable) ReadWrite() *StatusCmd {
- cmd := NewStatusCmd("readwrite")
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ClusterFailover() *StatusCmd {
- cmd := NewStatusCmd("cluster", "failover")
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ClusterAddSlots(slots ...int) *StatusCmd {
- args := make([]interface{}, 2+len(slots))
- args[0] = "cluster"
- args[1] = "addslots"
- for i, num := range slots {
- args[2+i] = strconv.Itoa(num)
- }
- cmd := NewStatusCmd(args...)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) ClusterAddSlotsRange(min, max int) *StatusCmd {
- size := max - min + 1
- slots := make([]int, size)
- for i := 0; i < size; i++ {
- slots[i] = min + i
- }
- return c.ClusterAddSlots(slots...)
- }
- //------------------------------------------------------------------------------
- func (c *cmdable) GeoAdd(key string, geoLocation ...*GeoLocation) *IntCmd {
- args := make([]interface{}, 2+3*len(geoLocation))
- args[0] = "geoadd"
- args[1] = key
- for i, eachLoc := range geoLocation {
- args[2+3*i] = eachLoc.Longitude
- args[2+3*i+1] = eachLoc.Latitude
- args[2+3*i+2] = eachLoc.Name
- }
- cmd := NewIntCmd(args...)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) GeoRadius(key string, longitude, latitude float64, query *GeoRadiusQuery) *GeoLocationCmd {
- cmd := NewGeoLocationCmd(query, "georadius", key, longitude, latitude)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) GeoRadiusByMember(key, member string, query *GeoRadiusQuery) *GeoLocationCmd {
- cmd := NewGeoLocationCmd(query, "georadiusbymember", key, member)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) GeoDist(key string, member1, member2, unit string) *FloatCmd {
- if unit == "" {
- unit = "km"
- }
- cmd := NewFloatCmd("geodist", key, member1, member2, unit)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) GeoHash(key string, members ...string) *StringSliceCmd {
- args := make([]interface{}, 2+len(members))
- args[0] = "geohash"
- args[1] = key
- for i, member := range members {
- args[2+i] = member
- }
- cmd := NewStringSliceCmd(args...)
- c.process(cmd)
- return cmd
- }
- func (c *cmdable) GeoPos(key string, members ...string) *GeoPosCmd {
- args := make([]interface{}, 2+len(members))
- args[0] = "geopos"
- args[1] = key
- for i, member := range members {
- args[2+i] = member
- }
- cmd := NewGeoPosCmd(args...)
- c.process(cmd)
- return cmd
- }
- //------------------------------------------------------------------------------
- func (c *cmdable) Command() *CommandsInfoCmd {
- cmd := NewCommandsInfoCmd("command")
- c.process(cmd)
- return cmd
- }
|