redis.go 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749
  1. package redis
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "strconv"
  7. "time"
  8. red "github.com/go-redis/redis/v8"
  9. "github.com/zeromicro/go-zero/core/breaker"
  10. "github.com/zeromicro/go-zero/core/mapping"
  11. "github.com/zeromicro/go-zero/core/syncx"
  12. )
  13. const (
  14. // ClusterType means redis cluster.
  15. ClusterType = "cluster"
  16. // NodeType means redis node.
  17. NodeType = "node"
  18. // Nil is an alias of redis.Nil.
  19. Nil = red.Nil
  20. blockingQueryTimeout = 5 * time.Second
  21. readWriteTimeout = 2 * time.Second
  22. defaultSlowThreshold = time.Millisecond * 100
  23. )
  24. var (
  25. // ErrNilNode is an error that indicates a nil redis node.
  26. ErrNilNode = errors.New("nil redis node")
  27. slowThreshold = syncx.ForAtomicDuration(defaultSlowThreshold)
  28. )
  29. type (
  30. // Option defines the method to customize a Redis.
  31. Option func(r *Redis)
  32. // A Pair is a key/pair set used in redis zset.
  33. Pair struct {
  34. Key string
  35. Score int64
  36. }
  37. PairFloat struct {
  38. Key string
  39. Score float64
  40. }
  41. // Redis defines a redis node/cluster. It is thread-safe.
  42. Redis struct {
  43. Addr string
  44. Type string
  45. Pass string
  46. tls bool
  47. brk breaker.Breaker
  48. }
  49. // RedisNode interface represents a redis node.
  50. RedisNode interface {
  51. red.Cmdable
  52. }
  53. // GeoLocation is used with GeoAdd to add geospatial location.
  54. GeoLocation = red.GeoLocation
  55. // GeoRadiusQuery is used with GeoRadius to query geospatial index.
  56. GeoRadiusQuery = red.GeoRadiusQuery
  57. // GeoPos is used to represent a geo position.
  58. GeoPos = red.GeoPos
  59. // Pipeliner is an alias of redis.Pipeliner.
  60. Pipeliner = red.Pipeliner
  61. // Z represents sorted set member.
  62. Z = red.Z
  63. // ZStore is an alias of redis.ZStore.
  64. ZStore = red.ZStore
  65. // IntCmd is an alias of redis.IntCmd.
  66. IntCmd = red.IntCmd
  67. // FloatCmd is an alias of redis.FloatCmd.
  68. FloatCmd = red.FloatCmd
  69. // StringCmd is an alias of redis.StringCmd.
  70. StringCmd = red.StringCmd
  71. )
  72. // New returns a Redis with given options.
  73. func New(addr string, opts ...Option) *Redis {
  74. r := &Redis{
  75. Addr: addr,
  76. Type: NodeType,
  77. brk: breaker.NewBreaker(),
  78. }
  79. for _, opt := range opts {
  80. opt(r)
  81. }
  82. return r
  83. }
  84. // BitCount is redis bitcount command implementation.
  85. func (s *Redis) BitCount(key string, start, end int64) (int64, error) {
  86. return s.BitCountCtx(context.Background(), key, start, end)
  87. }
  88. // BitCountCtx is redis bitcount command implementation.
  89. func (s *Redis) BitCountCtx(ctx context.Context, key string, start, end int64) (val int64, err error) {
  90. err = s.brk.DoWithAcceptable(func() error {
  91. conn, err := getRedis(s)
  92. if err != nil {
  93. return err
  94. }
  95. val, err = conn.BitCount(ctx, key, &red.BitCount{
  96. Start: start,
  97. End: end,
  98. }).Result()
  99. return err
  100. }, acceptable)
  101. return
  102. }
  103. // BitOpAnd is redis bit operation (and) command implementation.
  104. func (s *Redis) BitOpAnd(destKey string, keys ...string) (int64, error) {
  105. return s.BitOpAndCtx(context.Background(), destKey, keys...)
  106. }
  107. // BitOpAndCtx is redis bit operation (and) command implementation.
  108. func (s *Redis) BitOpAndCtx(ctx context.Context, destKey string, keys ...string) (val int64, err error) {
  109. err = s.brk.DoWithAcceptable(func() error {
  110. conn, err := getRedis(s)
  111. if err != nil {
  112. return err
  113. }
  114. val, err = conn.BitOpAnd(ctx, destKey, keys...).Result()
  115. return err
  116. }, acceptable)
  117. return
  118. }
  119. // BitOpNot is redis bit operation (not) command implementation.
  120. func (s *Redis) BitOpNot(destKey, key string) (int64, error) {
  121. return s.BitOpNotCtx(context.Background(), destKey, key)
  122. }
  123. // BitOpNotCtx is redis bit operation (not) command implementation.
  124. func (s *Redis) BitOpNotCtx(ctx context.Context, destKey, key string) (val int64, err error) {
  125. err = s.brk.DoWithAcceptable(func() error {
  126. conn, err := getRedis(s)
  127. if err != nil {
  128. return err
  129. }
  130. val, err = conn.BitOpNot(ctx, destKey, key).Result()
  131. return err
  132. }, acceptable)
  133. return
  134. }
  135. // BitOpOr is redis bit operation (or) command implementation.
  136. func (s *Redis) BitOpOr(destKey string, keys ...string) (int64, error) {
  137. return s.BitOpOrCtx(context.Background(), destKey, keys...)
  138. }
  139. // BitOpOrCtx is redis bit operation (or) command implementation.
  140. func (s *Redis) BitOpOrCtx(ctx context.Context, destKey string, keys ...string) (val int64, err error) {
  141. err = s.brk.DoWithAcceptable(func() error {
  142. conn, err := getRedis(s)
  143. if err != nil {
  144. return err
  145. }
  146. val, err = conn.BitOpOr(ctx, destKey, keys...).Result()
  147. return err
  148. }, acceptable)
  149. return
  150. }
  151. // BitOpXor is redis bit operation (xor) command implementation.
  152. func (s *Redis) BitOpXor(destKey string, keys ...string) (int64, error) {
  153. return s.BitOpXorCtx(context.Background(), destKey, keys...)
  154. }
  155. // BitOpXorCtx is redis bit operation (xor) command implementation.
  156. func (s *Redis) BitOpXorCtx(ctx context.Context, destKey string, keys ...string) (val int64, err error) {
  157. err = s.brk.DoWithAcceptable(func() error {
  158. conn, err := getRedis(s)
  159. if err != nil {
  160. return err
  161. }
  162. val, err = conn.BitOpXor(ctx, destKey, keys...).Result()
  163. return err
  164. }, acceptable)
  165. return
  166. }
  167. // BitPos is redis bitpos command implementation.
  168. func (s *Redis) BitPos(key string, bit, start, end int64) (int64, error) {
  169. return s.BitPosCtx(context.Background(), key, bit, start, end)
  170. }
  171. // BitPosCtx is redis bitpos command implementation.
  172. func (s *Redis) BitPosCtx(ctx context.Context, key string, bit, start, end int64) (val int64, err error) {
  173. err = s.brk.DoWithAcceptable(func() error {
  174. conn, err := getRedis(s)
  175. if err != nil {
  176. return err
  177. }
  178. val, err = conn.BitPos(ctx, key, bit, start, end).Result()
  179. return err
  180. }, acceptable)
  181. return
  182. }
  183. // Blpop uses passed in redis connection to execute blocking queries.
  184. // Doesn't benefit from pooling redis connections of blocking queries
  185. func (s *Redis) Blpop(node RedisNode, key string) (string, error) {
  186. return s.BlpopCtx(context.Background(), node, key)
  187. }
  188. // BlpopCtx uses passed in redis connection to execute blocking queries.
  189. // Doesn't benefit from pooling redis connections of blocking queries
  190. func (s *Redis) BlpopCtx(ctx context.Context, node RedisNode, key string) (string, error) {
  191. return s.BlpopWithTimeoutCtx(ctx, node, blockingQueryTimeout, key)
  192. }
  193. // BlpopEx uses passed in redis connection to execute blpop command.
  194. // The difference against Blpop is that this method returns a bool to indicate success.
  195. func (s *Redis) BlpopEx(node RedisNode, key string) (string, bool, error) {
  196. return s.BlpopExCtx(context.Background(), node, key)
  197. }
  198. // BlpopExCtx uses passed in redis connection to execute blpop command.
  199. // The difference against Blpop is that this method returns a bool to indicate success.
  200. func (s *Redis) BlpopExCtx(ctx context.Context, node RedisNode, key string) (string, bool, error) {
  201. if node == nil {
  202. return "", false, ErrNilNode
  203. }
  204. vals, err := node.BLPop(ctx, blockingQueryTimeout, key).Result()
  205. if err != nil {
  206. return "", false, err
  207. }
  208. if len(vals) < 2 {
  209. return "", false, fmt.Errorf("no value on key: %s", key)
  210. }
  211. return vals[1], true, nil
  212. }
  213. // BlpopWithTimeout uses passed in redis connection to execute blpop command.
  214. // Control blocking query timeout
  215. func (s *Redis) BlpopWithTimeout(node RedisNode, timeout time.Duration, key string) (string, error) {
  216. return s.BlpopWithTimeoutCtx(context.Background(), node, timeout, key)
  217. }
  218. // BlpopWithTimeoutCtx uses passed in redis connection to execute blpop command.
  219. // Control blocking query timeout
  220. func (s *Redis) BlpopWithTimeoutCtx(ctx context.Context, node RedisNode, timeout time.Duration,
  221. key string) (string, error) {
  222. if node == nil {
  223. return "", ErrNilNode
  224. }
  225. vals, err := node.BLPop(ctx, timeout, key).Result()
  226. if err != nil {
  227. return "", err
  228. }
  229. if len(vals) < 2 {
  230. return "", fmt.Errorf("no value on key: %s", key)
  231. }
  232. return vals[1], nil
  233. }
  234. // Decr is the implementation of redis decr command.
  235. func (s *Redis) Decr(key string) (int64, error) {
  236. return s.DecrCtx(context.Background(), key)
  237. }
  238. // DecrCtx is the implementation of redis decr command.
  239. func (s *Redis) DecrCtx(ctx context.Context, key string) (val int64, err error) {
  240. err = s.brk.DoWithAcceptable(func() error {
  241. conn, err := getRedis(s)
  242. if err != nil {
  243. return err
  244. }
  245. val, err = conn.Decr(ctx, key).Result()
  246. return err
  247. }, acceptable)
  248. return
  249. }
  250. // Decrby is the implementation of redis decrby command.
  251. func (s *Redis) Decrby(key string, decrement int64) (int64, error) {
  252. return s.DecrbyCtx(context.Background(), key, decrement)
  253. }
  254. // DecrbyCtx is the implementation of redis decrby command.
  255. func (s *Redis) DecrbyCtx(ctx context.Context, key string, decrement int64) (val int64, err error) {
  256. err = s.brk.DoWithAcceptable(func() error {
  257. conn, err := getRedis(s)
  258. if err != nil {
  259. return err
  260. }
  261. val, err = conn.DecrBy(ctx, key, decrement).Result()
  262. return err
  263. }, acceptable)
  264. return
  265. }
  266. // Del deletes keys.
  267. func (s *Redis) Del(keys ...string) (int, error) {
  268. return s.DelCtx(context.Background(), keys...)
  269. }
  270. // DelCtx deletes keys.
  271. func (s *Redis) DelCtx(ctx context.Context, keys ...string) (val int, err error) {
  272. err = s.brk.DoWithAcceptable(func() error {
  273. conn, err := getRedis(s)
  274. if err != nil {
  275. return err
  276. }
  277. v, err := conn.Del(ctx, keys...).Result()
  278. if err != nil {
  279. return err
  280. }
  281. val = int(v)
  282. return nil
  283. }, acceptable)
  284. return
  285. }
  286. // Eval is the implementation of redis eval command.
  287. func (s *Redis) Eval(script string, keys []string, args ...interface{}) (interface{}, error) {
  288. return s.EvalCtx(context.Background(), script, keys, args...)
  289. }
  290. // EvalCtx is the implementation of redis eval command.
  291. func (s *Redis) EvalCtx(ctx context.Context, script string, keys []string,
  292. args ...interface{}) (val interface{}, err error) {
  293. err = s.brk.DoWithAcceptable(func() error {
  294. conn, err := getRedis(s)
  295. if err != nil {
  296. return err
  297. }
  298. val, err = conn.Eval(ctx, script, keys, args...).Result()
  299. return err
  300. }, acceptable)
  301. return
  302. }
  303. // EvalSha is the implementation of redis evalsha command.
  304. func (s *Redis) EvalSha(sha string, keys []string, args ...interface{}) (interface{}, error) {
  305. return s.EvalShaCtx(context.Background(), sha, keys, args...)
  306. }
  307. // EvalShaCtx is the implementation of redis evalsha command.
  308. func (s *Redis) EvalShaCtx(ctx context.Context, sha string, keys []string,
  309. args ...interface{}) (val interface{}, err error) {
  310. err = s.brk.DoWithAcceptable(func() error {
  311. conn, err := getRedis(s)
  312. if err != nil {
  313. return err
  314. }
  315. val, err = conn.EvalSha(ctx, sha, keys, args...).Result()
  316. return err
  317. }, acceptable)
  318. return
  319. }
  320. // Exists is the implementation of redis exists command.
  321. func (s *Redis) Exists(key string) (bool, error) {
  322. return s.ExistsCtx(context.Background(), key)
  323. }
  324. // ExistsCtx is the implementation of redis exists command.
  325. func (s *Redis) ExistsCtx(ctx context.Context, key string) (val bool, err error) {
  326. err = s.brk.DoWithAcceptable(func() error {
  327. conn, err := getRedis(s)
  328. if err != nil {
  329. return err
  330. }
  331. v, err := conn.Exists(ctx, key).Result()
  332. if err != nil {
  333. return err
  334. }
  335. val = v == 1
  336. return nil
  337. }, acceptable)
  338. return
  339. }
  340. // Expire is the implementation of redis expire command.
  341. func (s *Redis) Expire(key string, seconds int) error {
  342. return s.ExpireCtx(context.Background(), key, seconds)
  343. }
  344. // ExpireCtx is the implementation of redis expire command.
  345. func (s *Redis) ExpireCtx(ctx context.Context, key string, seconds int) error {
  346. return s.brk.DoWithAcceptable(func() error {
  347. conn, err := getRedis(s)
  348. if err != nil {
  349. return err
  350. }
  351. return conn.Expire(ctx, key, time.Duration(seconds)*time.Second).Err()
  352. }, acceptable)
  353. }
  354. // Expireat is the implementation of redis expireat command.
  355. func (s *Redis) Expireat(key string, expireTime int64) error {
  356. return s.ExpireatCtx(context.Background(), key, expireTime)
  357. }
  358. // ExpireatCtx is the implementation of redis expireat command.
  359. func (s *Redis) ExpireatCtx(ctx context.Context, key string, expireTime int64) error {
  360. return s.brk.DoWithAcceptable(func() error {
  361. conn, err := getRedis(s)
  362. if err != nil {
  363. return err
  364. }
  365. return conn.ExpireAt(ctx, key, time.Unix(expireTime, 0)).Err()
  366. }, acceptable)
  367. }
  368. // GeoAdd is the implementation of redis geoadd command.
  369. func (s *Redis) GeoAdd(key string, geoLocation ...*GeoLocation) (int64, error) {
  370. return s.GeoAddCtx(context.Background(), key, geoLocation...)
  371. }
  372. // GeoAddCtx is the implementation of redis geoadd command.
  373. func (s *Redis) GeoAddCtx(ctx context.Context, key string, geoLocation ...*GeoLocation) (
  374. val int64, err error) {
  375. err = s.brk.DoWithAcceptable(func() error {
  376. conn, err := getRedis(s)
  377. if err != nil {
  378. return err
  379. }
  380. v, err := conn.GeoAdd(ctx, key, geoLocation...).Result()
  381. if err != nil {
  382. return err
  383. }
  384. val = v
  385. return nil
  386. }, acceptable)
  387. return
  388. }
  389. // GeoDist is the implementation of redis geodist command.
  390. func (s *Redis) GeoDist(key, member1, member2, unit string) (float64, error) {
  391. return s.GeoDistCtx(context.Background(), key, member1, member2, unit)
  392. }
  393. // GeoDistCtx is the implementation of redis geodist command.
  394. func (s *Redis) GeoDistCtx(ctx context.Context, key, member1, member2, unit string) (
  395. val float64, err error) {
  396. err = s.brk.DoWithAcceptable(func() error {
  397. conn, err := getRedis(s)
  398. if err != nil {
  399. return err
  400. }
  401. v, err := conn.GeoDist(ctx, key, member1, member2, unit).Result()
  402. if err != nil {
  403. return err
  404. }
  405. val = v
  406. return nil
  407. }, acceptable)
  408. return
  409. }
  410. // GeoHash is the implementation of redis geohash command.
  411. func (s *Redis) GeoHash(key string, members ...string) ([]string, error) {
  412. return s.GeoHashCtx(context.Background(), key, members...)
  413. }
  414. // GeoHashCtx is the implementation of redis geohash command.
  415. func (s *Redis) GeoHashCtx(ctx context.Context, key string, members ...string) (
  416. val []string, err error) {
  417. err = s.brk.DoWithAcceptable(func() error {
  418. conn, err := getRedis(s)
  419. if err != nil {
  420. return err
  421. }
  422. v, err := conn.GeoHash(ctx, key, members...).Result()
  423. if err != nil {
  424. return err
  425. }
  426. val = v
  427. return nil
  428. }, acceptable)
  429. return
  430. }
  431. // GeoRadius is the implementation of redis georadius command.
  432. func (s *Redis) GeoRadius(key string, longitude, latitude float64, query *GeoRadiusQuery) (
  433. []GeoLocation, error) {
  434. return s.GeoRadiusCtx(context.Background(), key, longitude, latitude, query)
  435. }
  436. // GeoRadiusCtx is the implementation of redis georadius command.
  437. func (s *Redis) GeoRadiusCtx(ctx context.Context, key string, longitude, latitude float64,
  438. query *GeoRadiusQuery) (val []GeoLocation, err error) {
  439. err = s.brk.DoWithAcceptable(func() error {
  440. conn, err := getRedis(s)
  441. if err != nil {
  442. return err
  443. }
  444. v, err := conn.GeoRadius(ctx, key, longitude, latitude, query).Result()
  445. if err != nil {
  446. return err
  447. }
  448. val = v
  449. return nil
  450. }, acceptable)
  451. return
  452. }
  453. // GeoRadiusByMember is the implementation of redis georadiusbymember command.
  454. func (s *Redis) GeoRadiusByMember(key, member string, query *GeoRadiusQuery) ([]GeoLocation, error) {
  455. return s.GeoRadiusByMemberCtx(context.Background(), key, member, query)
  456. }
  457. // GeoRadiusByMemberCtx is the implementation of redis georadiusbymember command.
  458. func (s *Redis) GeoRadiusByMemberCtx(ctx context.Context, key, member string,
  459. query *GeoRadiusQuery) (val []GeoLocation, err error) {
  460. err = s.brk.DoWithAcceptable(func() error {
  461. conn, err := getRedis(s)
  462. if err != nil {
  463. return err
  464. }
  465. v, err := conn.GeoRadiusByMember(ctx, key, member, query).Result()
  466. if err != nil {
  467. return err
  468. }
  469. val = v
  470. return nil
  471. }, acceptable)
  472. return
  473. }
  474. // GeoPos is the implementation of redis geopos command.
  475. func (s *Redis) GeoPos(key string, members ...string) ([]*GeoPos, error) {
  476. return s.GeoPosCtx(context.Background(), key, members...)
  477. }
  478. // GeoPosCtx is the implementation of redis geopos command.
  479. func (s *Redis) GeoPosCtx(ctx context.Context, key string, members ...string) (
  480. val []*GeoPos, err error) {
  481. err = s.brk.DoWithAcceptable(func() error {
  482. conn, err := getRedis(s)
  483. if err != nil {
  484. return err
  485. }
  486. v, err := conn.GeoPos(ctx, key, members...).Result()
  487. if err != nil {
  488. return err
  489. }
  490. val = v
  491. return nil
  492. }, acceptable)
  493. return
  494. }
  495. // Get is the implementation of redis get command.
  496. func (s *Redis) Get(key string) (string, error) {
  497. return s.GetCtx(context.Background(), key)
  498. }
  499. // GetCtx is the implementation of redis get command.
  500. func (s *Redis) GetCtx(ctx context.Context, key string) (val string, err error) {
  501. err = s.brk.DoWithAcceptable(func() error {
  502. conn, err := getRedis(s)
  503. if err != nil {
  504. return err
  505. }
  506. if val, err = conn.Get(ctx, key).Result(); err == red.Nil {
  507. return nil
  508. } else if err != nil {
  509. return err
  510. } else {
  511. return nil
  512. }
  513. }, acceptable)
  514. return
  515. }
  516. // GetBit is the implementation of redis getbit command.
  517. func (s *Redis) GetBit(key string, offset int64) (int, error) {
  518. return s.GetBitCtx(context.Background(), key, offset)
  519. }
  520. // GetBitCtx is the implementation of redis getbit command.
  521. func (s *Redis) GetBitCtx(ctx context.Context, key string, offset int64) (val int, err error) {
  522. err = s.brk.DoWithAcceptable(func() error {
  523. conn, err := getRedis(s)
  524. if err != nil {
  525. return err
  526. }
  527. v, err := conn.GetBit(ctx, key, offset).Result()
  528. if err != nil {
  529. return err
  530. }
  531. val = int(v)
  532. return nil
  533. }, acceptable)
  534. return
  535. }
  536. // GetSet is the implementation of redis getset command.
  537. func (s *Redis) GetSet(key, value string) (string, error) {
  538. return s.GetSetCtx(context.Background(), key, value)
  539. }
  540. // GetSetCtx is the implementation of redis getset command.
  541. func (s *Redis) GetSetCtx(ctx context.Context, key, value string) (val string, err error) {
  542. err = s.brk.DoWithAcceptable(func() error {
  543. conn, err := getRedis(s)
  544. if err != nil {
  545. return err
  546. }
  547. if val, err = conn.GetSet(ctx, key, value).Result(); err == red.Nil {
  548. return nil
  549. }
  550. return err
  551. }, acceptable)
  552. return
  553. }
  554. // Hdel is the implementation of redis hdel command.
  555. func (s *Redis) Hdel(key string, fields ...string) (bool, error) {
  556. return s.HdelCtx(context.Background(), key, fields...)
  557. }
  558. // HdelCtx is the implementation of redis hdel command.
  559. func (s *Redis) HdelCtx(ctx context.Context, key string, fields ...string) (val bool, err error) {
  560. err = s.brk.DoWithAcceptable(func() error {
  561. conn, err := getRedis(s)
  562. if err != nil {
  563. return err
  564. }
  565. v, err := conn.HDel(ctx, key, fields...).Result()
  566. if err != nil {
  567. return err
  568. }
  569. val = v >= 1
  570. return nil
  571. }, acceptable)
  572. return
  573. }
  574. // Hexists is the implementation of redis hexists command.
  575. func (s *Redis) Hexists(key, field string) (bool, error) {
  576. return s.HexistsCtx(context.Background(), key, field)
  577. }
  578. // HexistsCtx is the implementation of redis hexists command.
  579. func (s *Redis) HexistsCtx(ctx context.Context, key, field string) (val bool, err error) {
  580. err = s.brk.DoWithAcceptable(func() error {
  581. conn, err := getRedis(s)
  582. if err != nil {
  583. return err
  584. }
  585. val, err = conn.HExists(ctx, key, field).Result()
  586. return err
  587. }, acceptable)
  588. return
  589. }
  590. // Hget is the implementation of redis hget command.
  591. func (s *Redis) Hget(key, field string) (string, error) {
  592. return s.HgetCtx(context.Background(), key, field)
  593. }
  594. // HgetCtx is the implementation of redis hget command.
  595. func (s *Redis) HgetCtx(ctx context.Context, key, field string) (val string, err error) {
  596. err = s.brk.DoWithAcceptable(func() error {
  597. conn, err := getRedis(s)
  598. if err != nil {
  599. return err
  600. }
  601. val, err = conn.HGet(ctx, key, field).Result()
  602. return err
  603. }, acceptable)
  604. return
  605. }
  606. // Hgetall is the implementation of redis hgetall command.
  607. func (s *Redis) Hgetall(key string) (map[string]string, error) {
  608. return s.HgetallCtx(context.Background(), key)
  609. }
  610. // HgetallCtx is the implementation of redis hgetall command.
  611. func (s *Redis) HgetallCtx(ctx context.Context, key string) (val map[string]string, err error) {
  612. err = s.brk.DoWithAcceptable(func() error {
  613. conn, err := getRedis(s)
  614. if err != nil {
  615. return err
  616. }
  617. val, err = conn.HGetAll(ctx, key).Result()
  618. return err
  619. }, acceptable)
  620. return
  621. }
  622. // Hincrby is the implementation of redis hincrby command.
  623. func (s *Redis) Hincrby(key, field string, increment int) (int, error) {
  624. return s.HincrbyCtx(context.Background(), key, field, increment)
  625. }
  626. // HincrbyCtx is the implementation of redis hincrby command.
  627. func (s *Redis) HincrbyCtx(ctx context.Context, key, field string, increment int) (val int, err error) {
  628. err = s.brk.DoWithAcceptable(func() error {
  629. conn, err := getRedis(s)
  630. if err != nil {
  631. return err
  632. }
  633. v, err := conn.HIncrBy(ctx, key, field, int64(increment)).Result()
  634. if err != nil {
  635. return err
  636. }
  637. val = int(v)
  638. return nil
  639. }, acceptable)
  640. return
  641. }
  642. // HincrbyFloat is the implementation of redis hincrby command.
  643. func (s *Redis) HincrbyFloat(key, field string, increment float64) (float64, error) {
  644. return s.HincrbyFloatCtx(context.Background(), key, field, increment)
  645. }
  646. // HincrbyFloatCtx is the implementation of redis hincrby command.
  647. func (s *Redis) HincrbyFloatCtx(ctx context.Context, key, field string, increment float64) (val float64, err error) {
  648. err = s.brk.DoWithAcceptable(func() error {
  649. conn, err := getRedis(s)
  650. if err != nil {
  651. return err
  652. }
  653. val, err = conn.HIncrByFloat(ctx, key, field, increment).Result()
  654. if err != nil {
  655. return err
  656. }
  657. return nil
  658. }, acceptable)
  659. return
  660. }
  661. // Hkeys is the implementation of redis hkeys command.
  662. func (s *Redis) Hkeys(key string) ([]string, error) {
  663. return s.HkeysCtx(context.Background(), key)
  664. }
  665. // HkeysCtx is the implementation of redis hkeys command.
  666. func (s *Redis) HkeysCtx(ctx context.Context, key string) (val []string, err error) {
  667. err = s.brk.DoWithAcceptable(func() error {
  668. conn, err := getRedis(s)
  669. if err != nil {
  670. return err
  671. }
  672. val, err = conn.HKeys(ctx, key).Result()
  673. return err
  674. }, acceptable)
  675. return
  676. }
  677. // Hlen is the implementation of redis hlen command.
  678. func (s *Redis) Hlen(key string) (int, error) {
  679. return s.HlenCtx(context.Background(), key)
  680. }
  681. // HlenCtx is the implementation of redis hlen command.
  682. func (s *Redis) HlenCtx(ctx context.Context, key string) (val int, err error) {
  683. err = s.brk.DoWithAcceptable(func() error {
  684. conn, err := getRedis(s)
  685. if err != nil {
  686. return err
  687. }
  688. v, err := conn.HLen(ctx, key).Result()
  689. if err != nil {
  690. return err
  691. }
  692. val = int(v)
  693. return nil
  694. }, acceptable)
  695. return
  696. }
  697. // Hmget is the implementation of redis hmget command.
  698. func (s *Redis) Hmget(key string, fields ...string) ([]string, error) {
  699. return s.HmgetCtx(context.Background(), key, fields...)
  700. }
  701. // HmgetCtx is the implementation of redis hmget command.
  702. func (s *Redis) HmgetCtx(ctx context.Context, key string, fields ...string) (val []string, err error) {
  703. err = s.brk.DoWithAcceptable(func() error {
  704. conn, err := getRedis(s)
  705. if err != nil {
  706. return err
  707. }
  708. v, err := conn.HMGet(ctx, key, fields...).Result()
  709. if err != nil {
  710. return err
  711. }
  712. val = toStrings(v)
  713. return nil
  714. }, acceptable)
  715. return
  716. }
  717. // Hset is the implementation of redis hset command.
  718. func (s *Redis) Hset(key, field, value string) error {
  719. return s.HsetCtx(context.Background(), key, field, value)
  720. }
  721. // HsetCtx is the implementation of redis hset command.
  722. func (s *Redis) HsetCtx(ctx context.Context, key, field, value string) error {
  723. return s.brk.DoWithAcceptable(func() error {
  724. conn, err := getRedis(s)
  725. if err != nil {
  726. return err
  727. }
  728. return conn.HSet(ctx, key, field, value).Err()
  729. }, acceptable)
  730. }
  731. // Hsetnx is the implementation of redis hsetnx command.
  732. func (s *Redis) Hsetnx(key, field, value string) (bool, error) {
  733. return s.HsetnxCtx(context.Background(), key, field, value)
  734. }
  735. // HsetnxCtx is the implementation of redis hsetnx command.
  736. func (s *Redis) HsetnxCtx(ctx context.Context, key, field, value string) (val bool, err error) {
  737. err = s.brk.DoWithAcceptable(func() error {
  738. conn, err := getRedis(s)
  739. if err != nil {
  740. return err
  741. }
  742. val, err = conn.HSetNX(ctx, key, field, value).Result()
  743. return err
  744. }, acceptable)
  745. return
  746. }
  747. // Hmset is the implementation of redis hmset command.
  748. func (s *Redis) Hmset(key string, fieldsAndValues map[string]string) error {
  749. return s.HmsetCtx(context.Background(), key, fieldsAndValues)
  750. }
  751. // HmsetCtx is the implementation of redis hmset command.
  752. func (s *Redis) HmsetCtx(ctx context.Context, key string, fieldsAndValues map[string]string) error {
  753. return s.brk.DoWithAcceptable(func() error {
  754. conn, err := getRedis(s)
  755. if err != nil {
  756. return err
  757. }
  758. vals := make(map[string]interface{}, len(fieldsAndValues))
  759. for k, v := range fieldsAndValues {
  760. vals[k] = v
  761. }
  762. return conn.HMSet(ctx, key, vals).Err()
  763. }, acceptable)
  764. }
  765. // Hscan is the implementation of redis hscan command.
  766. func (s *Redis) Hscan(key string, cursor uint64, match string, count int64) (
  767. keys []string, cur uint64, err error) {
  768. return s.HscanCtx(context.Background(), key, cursor, match, count)
  769. }
  770. // HscanCtx is the implementation of redis hscan command.
  771. func (s *Redis) HscanCtx(ctx context.Context, key string, cursor uint64, match string, count int64) (
  772. keys []string, cur uint64, err error) {
  773. err = s.brk.DoWithAcceptable(func() error {
  774. conn, err := getRedis(s)
  775. if err != nil {
  776. return err
  777. }
  778. keys, cur, err = conn.HScan(ctx, key, cursor, match, count).Result()
  779. return err
  780. }, acceptable)
  781. return
  782. }
  783. // Hvals is the implementation of redis hvals command.
  784. func (s *Redis) Hvals(key string) ([]string, error) {
  785. return s.HvalsCtx(context.Background(), key)
  786. }
  787. // HvalsCtx is the implementation of redis hvals command.
  788. func (s *Redis) HvalsCtx(ctx context.Context, key string) (val []string, err error) {
  789. err = s.brk.DoWithAcceptable(func() error {
  790. conn, err := getRedis(s)
  791. if err != nil {
  792. return err
  793. }
  794. val, err = conn.HVals(ctx, key).Result()
  795. return err
  796. }, acceptable)
  797. return
  798. }
  799. // Incr is the implementation of redis incr command.
  800. func (s *Redis) Incr(key string) (int64, error) {
  801. return s.IncrCtx(context.Background(), key)
  802. }
  803. // IncrCtx is the implementation of redis incr command.
  804. func (s *Redis) IncrCtx(ctx context.Context, key string) (val int64, err error) {
  805. err = s.brk.DoWithAcceptable(func() error {
  806. conn, err := getRedis(s)
  807. if err != nil {
  808. return err
  809. }
  810. val, err = conn.Incr(ctx, key).Result()
  811. return err
  812. }, acceptable)
  813. return
  814. }
  815. // Incrby is the implementation of redis incrby command.
  816. func (s *Redis) Incrby(key string, increment int64) (int64, error) {
  817. return s.IncrbyCtx(context.Background(), key, increment)
  818. }
  819. // IncrbyCtx is the implementation of redis incrby command.
  820. func (s *Redis) IncrbyCtx(ctx context.Context, key string, increment int64) (val int64, err error) {
  821. err = s.brk.DoWithAcceptable(func() error {
  822. conn, err := getRedis(s)
  823. if err != nil {
  824. return err
  825. }
  826. val, err = conn.IncrBy(ctx, key, increment).Result()
  827. return err
  828. }, acceptable)
  829. return
  830. }
  831. // Incrby is the implementation of redis incrby command.
  832. func (s *Redis) IncrbyFloat(key string, increment float64) (float64, error) {
  833. return s.IncrbyFloatCtx(context.Background(), key, increment)
  834. }
  835. // IncrbyFloatCtx is the implementation of redis incrby command.
  836. func (s *Redis) IncrbyFloatCtx(ctx context.Context, key string, increment float64) (val float64, err error) {
  837. err = s.brk.DoWithAcceptable(func() error {
  838. conn, err := getRedis(s)
  839. if err != nil {
  840. return err
  841. }
  842. val, err = conn.IncrByFloat(ctx, key, increment).Result()
  843. return err
  844. }, acceptable)
  845. return
  846. }
  847. // Keys is the implementation of redis keys command.
  848. func (s *Redis) Keys(pattern string) ([]string, error) {
  849. return s.KeysCtx(context.Background(), pattern)
  850. }
  851. // KeysCtx is the implementation of redis keys command.
  852. func (s *Redis) KeysCtx(ctx context.Context, pattern string) (val []string, err error) {
  853. err = s.brk.DoWithAcceptable(func() error {
  854. conn, err := getRedis(s)
  855. if err != nil {
  856. return err
  857. }
  858. val, err = conn.Keys(ctx, pattern).Result()
  859. return err
  860. }, acceptable)
  861. return
  862. }
  863. // Llen is the implementation of redis llen command.
  864. func (s *Redis) Llen(key string) (int, error) {
  865. return s.LlenCtx(context.Background(), key)
  866. }
  867. // LlenCtx is the implementation of redis llen command.
  868. func (s *Redis) LlenCtx(ctx context.Context, key string) (val int, err error) {
  869. err = s.brk.DoWithAcceptable(func() error {
  870. conn, err := getRedis(s)
  871. if err != nil {
  872. return err
  873. }
  874. v, err := conn.LLen(ctx, key).Result()
  875. if err != nil {
  876. return err
  877. }
  878. val = int(v)
  879. return nil
  880. }, acceptable)
  881. return
  882. }
  883. // Lindex is the implementation of redis lindex command.
  884. func (s *Redis) Lindex(key string, index int64) (string, error) {
  885. return s.LindexCtx(context.Background(), key, index)
  886. }
  887. // LindexCtx is the implementation of redis lindex command.
  888. func (s *Redis) LindexCtx(ctx context.Context, key string, index int64) (val string, err error) {
  889. err = s.brk.DoWithAcceptable(func() error {
  890. conn, err := getRedis(s)
  891. if err != nil {
  892. return err
  893. }
  894. val, err = conn.LIndex(ctx, key, index).Result()
  895. return err
  896. }, acceptable)
  897. return
  898. }
  899. // Lpop is the implementation of redis lpop command.
  900. func (s *Redis) Lpop(key string) (string, error) {
  901. return s.LpopCtx(context.Background(), key)
  902. }
  903. // LpopCtx is the implementation of redis lpop command.
  904. func (s *Redis) LpopCtx(ctx context.Context, key string) (val string, err error) {
  905. err = s.brk.DoWithAcceptable(func() error {
  906. conn, err := getRedis(s)
  907. if err != nil {
  908. return err
  909. }
  910. val, err = conn.LPop(ctx, key).Result()
  911. return err
  912. }, acceptable)
  913. return
  914. }
  915. // Lpush is the implementation of redis lpush command.
  916. func (s *Redis) Lpush(key string, values ...interface{}) (int, error) {
  917. return s.LpushCtx(context.Background(), key, values...)
  918. }
  919. // LpushCtx is the implementation of redis lpush command.
  920. func (s *Redis) LpushCtx(ctx context.Context, key string, values ...interface{}) (val int, err error) {
  921. err = s.brk.DoWithAcceptable(func() error {
  922. conn, err := getRedis(s)
  923. if err != nil {
  924. return err
  925. }
  926. v, err := conn.LPush(ctx, key, values...).Result()
  927. if err != nil {
  928. return err
  929. }
  930. val = int(v)
  931. return nil
  932. }, acceptable)
  933. return
  934. }
  935. // Lrange is the implementation of redis lrange command.
  936. func (s *Redis) Lrange(key string, start, stop int) ([]string, error) {
  937. return s.LrangeCtx(context.Background(), key, start, stop)
  938. }
  939. // LrangeCtx is the implementation of redis lrange command.
  940. func (s *Redis) LrangeCtx(ctx context.Context, key string, start, stop int) (val []string, err error) {
  941. err = s.brk.DoWithAcceptable(func() error {
  942. conn, err := getRedis(s)
  943. if err != nil {
  944. return err
  945. }
  946. val, err = conn.LRange(ctx, key, int64(start), int64(stop)).Result()
  947. return err
  948. }, acceptable)
  949. return
  950. }
  951. // Lrem is the implementation of redis lrem command.
  952. func (s *Redis) Lrem(key string, count int, value string) (int, error) {
  953. return s.LremCtx(context.Background(), key, count, value)
  954. }
  955. // LremCtx is the implementation of redis lrem command.
  956. func (s *Redis) LremCtx(ctx context.Context, key string, count int, value string) (val int, err error) {
  957. err = s.brk.DoWithAcceptable(func() error {
  958. conn, err := getRedis(s)
  959. if err != nil {
  960. return err
  961. }
  962. v, err := conn.LRem(ctx, key, int64(count), value).Result()
  963. if err != nil {
  964. return err
  965. }
  966. val = int(v)
  967. return nil
  968. }, acceptable)
  969. return
  970. }
  971. // Ltrim is the implementation of redis ltrim command.
  972. func (s *Redis) Ltrim(key string, start, stop int64) error {
  973. return s.LtrimCtx(context.Background(), key, start, stop)
  974. }
  975. // LtrimCtx is the implementation of redis ltrim command.
  976. func (s *Redis) LtrimCtx(ctx context.Context, key string, start, stop int64) error {
  977. return s.brk.DoWithAcceptable(func() error {
  978. conn, err := getRedis(s)
  979. if err != nil {
  980. return err
  981. }
  982. return conn.LTrim(ctx, key, start, stop).Err()
  983. }, acceptable)
  984. }
  985. // Mget is the implementation of redis mget command.
  986. func (s *Redis) Mget(keys ...string) ([]string, error) {
  987. return s.MgetCtx(context.Background(), keys...)
  988. }
  989. // MgetCtx is the implementation of redis mget command.
  990. func (s *Redis) MgetCtx(ctx context.Context, keys ...string) (val []string, err error) {
  991. err = s.brk.DoWithAcceptable(func() error {
  992. conn, err := getRedis(s)
  993. if err != nil {
  994. return err
  995. }
  996. v, err := conn.MGet(ctx, keys...).Result()
  997. if err != nil {
  998. return err
  999. }
  1000. val = toStrings(v)
  1001. return nil
  1002. }, acceptable)
  1003. return
  1004. }
  1005. // Persist is the implementation of redis persist command.
  1006. func (s *Redis) Persist(key string) (bool, error) {
  1007. return s.PersistCtx(context.Background(), key)
  1008. }
  1009. // PersistCtx is the implementation of redis persist command.
  1010. func (s *Redis) PersistCtx(ctx context.Context, key string) (val bool, err error) {
  1011. err = s.brk.DoWithAcceptable(func() error {
  1012. conn, err := getRedis(s)
  1013. if err != nil {
  1014. return err
  1015. }
  1016. val, err = conn.Persist(ctx, key).Result()
  1017. return err
  1018. }, acceptable)
  1019. return
  1020. }
  1021. // Pfadd is the implementation of redis pfadd command.
  1022. func (s *Redis) Pfadd(key string, values ...interface{}) (bool, error) {
  1023. return s.PfaddCtx(context.Background(), key, values...)
  1024. }
  1025. // PfaddCtx is the implementation of redis pfadd command.
  1026. func (s *Redis) PfaddCtx(ctx context.Context, key string, values ...interface{}) (val bool, err error) {
  1027. err = s.brk.DoWithAcceptable(func() error {
  1028. conn, err := getRedis(s)
  1029. if err != nil {
  1030. return err
  1031. }
  1032. v, err := conn.PFAdd(ctx, key, values...).Result()
  1033. if err != nil {
  1034. return err
  1035. }
  1036. val = v >= 1
  1037. return nil
  1038. }, acceptable)
  1039. return
  1040. }
  1041. // Pfcount is the implementation of redis pfcount command.
  1042. func (s *Redis) Pfcount(key string) (int64, error) {
  1043. return s.PfcountCtx(context.Background(), key)
  1044. }
  1045. // PfcountCtx is the implementation of redis pfcount command.
  1046. func (s *Redis) PfcountCtx(ctx context.Context, key string) (val int64, err error) {
  1047. err = s.brk.DoWithAcceptable(func() error {
  1048. conn, err := getRedis(s)
  1049. if err != nil {
  1050. return err
  1051. }
  1052. val, err = conn.PFCount(ctx, key).Result()
  1053. return err
  1054. }, acceptable)
  1055. return
  1056. }
  1057. // Pfmerge is the implementation of redis pfmerge command.
  1058. func (s *Redis) Pfmerge(dest string, keys ...string) error {
  1059. return s.PfmergeCtx(context.Background(), dest, keys...)
  1060. }
  1061. // PfmergeCtx is the implementation of redis pfmerge command.
  1062. func (s *Redis) PfmergeCtx(ctx context.Context, dest string, keys ...string) error {
  1063. return s.brk.DoWithAcceptable(func() error {
  1064. conn, err := getRedis(s)
  1065. if err != nil {
  1066. return err
  1067. }
  1068. _, err = conn.PFMerge(ctx, dest, keys...).Result()
  1069. return err
  1070. }, acceptable)
  1071. }
  1072. // Ping is the implementation of redis ping command.
  1073. func (s *Redis) Ping() bool {
  1074. return s.PingCtx(context.Background())
  1075. }
  1076. // PingCtx is the implementation of redis ping command.
  1077. func (s *Redis) PingCtx(ctx context.Context) (val bool) {
  1078. // ignore error, error means false
  1079. _ = s.brk.DoWithAcceptable(func() error {
  1080. conn, err := getRedis(s)
  1081. if err != nil {
  1082. val = false
  1083. return nil
  1084. }
  1085. v, err := conn.Ping(ctx).Result()
  1086. if err != nil {
  1087. val = false
  1088. return nil
  1089. }
  1090. val = v == "PONG"
  1091. return nil
  1092. }, acceptable)
  1093. return
  1094. }
  1095. // Pipelined lets fn execute pipelined commands.
  1096. func (s *Redis) Pipelined(fn func(Pipeliner) error) error {
  1097. return s.PipelinedCtx(context.Background(), fn)
  1098. }
  1099. // PipelinedCtx lets fn execute pipelined commands.
  1100. // Results need to be retrieved by calling Pipeline.Exec()
  1101. func (s *Redis) PipelinedCtx(ctx context.Context, fn func(Pipeliner) error) error {
  1102. return s.brk.DoWithAcceptable(func() error {
  1103. conn, err := getRedis(s)
  1104. if err != nil {
  1105. return err
  1106. }
  1107. _, err = conn.Pipelined(ctx, fn)
  1108. return err
  1109. }, acceptable)
  1110. }
  1111. // Rpop is the implementation of redis rpop command.
  1112. func (s *Redis) Rpop(key string) (string, error) {
  1113. return s.RpopCtx(context.Background(), key)
  1114. }
  1115. // RpopCtx is the implementation of redis rpop command.
  1116. func (s *Redis) RpopCtx(ctx context.Context, key string) (val string, err error) {
  1117. err = s.brk.DoWithAcceptable(func() error {
  1118. conn, err := getRedis(s)
  1119. if err != nil {
  1120. return err
  1121. }
  1122. val, err = conn.RPop(ctx, key).Result()
  1123. return err
  1124. }, acceptable)
  1125. return
  1126. }
  1127. // Rpush is the implementation of redis rpush command.
  1128. func (s *Redis) Rpush(key string, values ...interface{}) (int, error) {
  1129. return s.RpushCtx(context.Background(), key, values...)
  1130. }
  1131. // RpushCtx is the implementation of redis rpush command.
  1132. func (s *Redis) RpushCtx(ctx context.Context, key string, values ...interface{}) (val int, err error) {
  1133. err = s.brk.DoWithAcceptable(func() error {
  1134. conn, err := getRedis(s)
  1135. if err != nil {
  1136. return err
  1137. }
  1138. v, err := conn.RPush(ctx, key, values...).Result()
  1139. if err != nil {
  1140. return err
  1141. }
  1142. val = int(v)
  1143. return nil
  1144. }, acceptable)
  1145. return
  1146. }
  1147. // Sadd is the implementation of redis sadd command.
  1148. func (s *Redis) Sadd(key string, values ...interface{}) (int, error) {
  1149. return s.SaddCtx(context.Background(), key, values...)
  1150. }
  1151. // SaddCtx is the implementation of redis sadd command.
  1152. func (s *Redis) SaddCtx(ctx context.Context, key string, values ...interface{}) (val int, err error) {
  1153. err = s.brk.DoWithAcceptable(func() error {
  1154. conn, err := getRedis(s)
  1155. if err != nil {
  1156. return err
  1157. }
  1158. v, err := conn.SAdd(ctx, key, values...).Result()
  1159. if err != nil {
  1160. return err
  1161. }
  1162. val = int(v)
  1163. return nil
  1164. }, acceptable)
  1165. return
  1166. }
  1167. // Scan is the implementation of redis scan command.
  1168. func (s *Redis) Scan(cursor uint64, match string, count int64) (keys []string, cur uint64, err error) {
  1169. return s.ScanCtx(context.Background(), cursor, match, count)
  1170. }
  1171. // ScanCtx is the implementation of redis scan command.
  1172. func (s *Redis) ScanCtx(ctx context.Context, cursor uint64, match string, count int64) (
  1173. keys []string, cur uint64, err error) {
  1174. err = s.brk.DoWithAcceptable(func() error {
  1175. conn, err := getRedis(s)
  1176. if err != nil {
  1177. return err
  1178. }
  1179. keys, cur, err = conn.Scan(ctx, cursor, match, count).Result()
  1180. return err
  1181. }, acceptable)
  1182. return
  1183. }
  1184. // SetBit is the implementation of redis setbit command.
  1185. func (s *Redis) SetBit(key string, offset int64, value int) (int, error) {
  1186. return s.SetBitCtx(context.Background(), key, offset, value)
  1187. }
  1188. // SetBitCtx is the implementation of redis setbit command.
  1189. func (s *Redis) SetBitCtx(ctx context.Context, key string, offset int64, value int) (val int, err error) {
  1190. err = s.brk.DoWithAcceptable(func() error {
  1191. conn, err := getRedis(s)
  1192. if err != nil {
  1193. return err
  1194. }
  1195. v, err := conn.SetBit(ctx, key, offset, value).Result()
  1196. if err != nil {
  1197. return err
  1198. }
  1199. val = int(v)
  1200. return nil
  1201. }, acceptable)
  1202. return
  1203. }
  1204. // Sscan is the implementation of redis sscan command.
  1205. func (s *Redis) Sscan(key string, cursor uint64, match string, count int64) (
  1206. keys []string, cur uint64, err error) {
  1207. return s.SscanCtx(context.Background(), key, cursor, match, count)
  1208. }
  1209. // SscanCtx is the implementation of redis sscan command.
  1210. func (s *Redis) SscanCtx(ctx context.Context, key string, cursor uint64, match string, count int64) (
  1211. keys []string, cur uint64, err error) {
  1212. err = s.brk.DoWithAcceptable(func() error {
  1213. conn, err := getRedis(s)
  1214. if err != nil {
  1215. return err
  1216. }
  1217. keys, cur, err = conn.SScan(ctx, key, cursor, match, count).Result()
  1218. return err
  1219. }, acceptable)
  1220. return
  1221. }
  1222. // Scard is the implementation of redis scard command.
  1223. func (s *Redis) Scard(key string) (int64, error) {
  1224. return s.ScardCtx(context.Background(), key)
  1225. }
  1226. // ScardCtx is the implementation of redis scard command.
  1227. func (s *Redis) ScardCtx(ctx context.Context, key string) (val int64, err error) {
  1228. err = s.brk.DoWithAcceptable(func() error {
  1229. conn, err := getRedis(s)
  1230. if err != nil {
  1231. return err
  1232. }
  1233. val, err = conn.SCard(ctx, key).Result()
  1234. return err
  1235. }, acceptable)
  1236. return
  1237. }
  1238. // ScriptLoad is the implementation of redis script load command.
  1239. func (s *Redis) ScriptLoad(script string) (string, error) {
  1240. return s.ScriptLoadCtx(context.Background(), script)
  1241. }
  1242. // ScriptLoadCtx is the implementation of redis script load command.
  1243. func (s *Redis) ScriptLoadCtx(ctx context.Context, script string) (string, error) {
  1244. conn, err := getRedis(s)
  1245. if err != nil {
  1246. return "", err
  1247. }
  1248. return conn.ScriptLoad(ctx, script).Result()
  1249. }
  1250. // Set is the implementation of redis set command.
  1251. func (s *Redis) Set(key, value string) error {
  1252. return s.SetCtx(context.Background(), key, value)
  1253. }
  1254. // SetCtx is the implementation of redis set command.
  1255. func (s *Redis) SetCtx(ctx context.Context, key, value string) error {
  1256. return s.brk.DoWithAcceptable(func() error {
  1257. conn, err := getRedis(s)
  1258. if err != nil {
  1259. return err
  1260. }
  1261. return conn.Set(ctx, key, value, 0).Err()
  1262. }, acceptable)
  1263. }
  1264. // Setex is the implementation of redis setex command.
  1265. func (s *Redis) Setex(key, value string, seconds int) error {
  1266. return s.SetexCtx(context.Background(), key, value, seconds)
  1267. }
  1268. // SetexCtx is the implementation of redis setex command.
  1269. func (s *Redis) SetexCtx(ctx context.Context, key, value string, seconds int) error {
  1270. return s.brk.DoWithAcceptable(func() error {
  1271. conn, err := getRedis(s)
  1272. if err != nil {
  1273. return err
  1274. }
  1275. return conn.Set(ctx, key, value, time.Duration(seconds)*time.Second).Err()
  1276. }, acceptable)
  1277. }
  1278. // Setnx is the implementation of redis setnx command.
  1279. func (s *Redis) Setnx(key, value string) (bool, error) {
  1280. return s.SetnxCtx(context.Background(), key, value)
  1281. }
  1282. // SetnxCtx is the implementation of redis setnx command.
  1283. func (s *Redis) SetnxCtx(ctx context.Context, key, value string) (val bool, err error) {
  1284. err = s.brk.DoWithAcceptable(func() error {
  1285. conn, err := getRedis(s)
  1286. if err != nil {
  1287. return err
  1288. }
  1289. val, err = conn.SetNX(ctx, key, value, 0).Result()
  1290. return err
  1291. }, acceptable)
  1292. return
  1293. }
  1294. // SetnxEx is the implementation of redis setnx command with expire.
  1295. func (s *Redis) SetnxEx(key, value string, seconds int) (bool, error) {
  1296. return s.SetnxExCtx(context.Background(), key, value, seconds)
  1297. }
  1298. // SetnxExCtx is the implementation of redis setnx command with expire.
  1299. func (s *Redis) SetnxExCtx(ctx context.Context, key, value string, seconds int) (val bool, err error) {
  1300. err = s.brk.DoWithAcceptable(func() error {
  1301. conn, err := getRedis(s)
  1302. if err != nil {
  1303. return err
  1304. }
  1305. val, err = conn.SetNX(ctx, key, value, time.Duration(seconds)*time.Second).Result()
  1306. return err
  1307. }, acceptable)
  1308. return
  1309. }
  1310. // Sismember is the implementation of redis sismember command.
  1311. func (s *Redis) Sismember(key string, value interface{}) (bool, error) {
  1312. return s.SismemberCtx(context.Background(), key, value)
  1313. }
  1314. // SismemberCtx is the implementation of redis sismember command.
  1315. func (s *Redis) SismemberCtx(ctx context.Context, key string, value interface{}) (val bool, err error) {
  1316. err = s.brk.DoWithAcceptable(func() error {
  1317. conn, err := getRedis(s)
  1318. if err != nil {
  1319. return err
  1320. }
  1321. val, err = conn.SIsMember(ctx, key, value).Result()
  1322. return err
  1323. }, acceptable)
  1324. return
  1325. }
  1326. // Smembers is the implementation of redis smembers command.
  1327. func (s *Redis) Smembers(key string) ([]string, error) {
  1328. return s.SmembersCtx(context.Background(), key)
  1329. }
  1330. // SmembersCtx is the implementation of redis smembers command.
  1331. func (s *Redis) SmembersCtx(ctx context.Context, key string) (val []string, err error) {
  1332. err = s.brk.DoWithAcceptable(func() error {
  1333. conn, err := getRedis(s)
  1334. if err != nil {
  1335. return err
  1336. }
  1337. val, err = conn.SMembers(ctx, key).Result()
  1338. return err
  1339. }, acceptable)
  1340. return
  1341. }
  1342. // Spop is the implementation of redis spop command.
  1343. func (s *Redis) Spop(key string) (string, error) {
  1344. return s.SpopCtx(context.Background(), key)
  1345. }
  1346. // SpopCtx is the implementation of redis spop command.
  1347. func (s *Redis) SpopCtx(ctx context.Context, key string) (val string, err error) {
  1348. err = s.brk.DoWithAcceptable(func() error {
  1349. conn, err := getRedis(s)
  1350. if err != nil {
  1351. return err
  1352. }
  1353. val, err = conn.SPop(ctx, key).Result()
  1354. return err
  1355. }, acceptable)
  1356. return
  1357. }
  1358. // Srandmember is the implementation of redis srandmember command.
  1359. func (s *Redis) Srandmember(key string, count int) ([]string, error) {
  1360. return s.SrandmemberCtx(context.Background(), key, count)
  1361. }
  1362. // SrandmemberCtx is the implementation of redis srandmember command.
  1363. func (s *Redis) SrandmemberCtx(ctx context.Context, key string, count int) (val []string, err error) {
  1364. err = s.brk.DoWithAcceptable(func() error {
  1365. conn, err := getRedis(s)
  1366. if err != nil {
  1367. return err
  1368. }
  1369. val, err = conn.SRandMemberN(ctx, key, int64(count)).Result()
  1370. return err
  1371. }, acceptable)
  1372. return
  1373. }
  1374. // Srem is the implementation of redis srem command.
  1375. func (s *Redis) Srem(key string, values ...interface{}) (int, error) {
  1376. return s.SremCtx(context.Background(), key, values...)
  1377. }
  1378. // SremCtx is the implementation of redis srem command.
  1379. func (s *Redis) SremCtx(ctx context.Context, key string, values ...interface{}) (val int, err error) {
  1380. err = s.brk.DoWithAcceptable(func() error {
  1381. conn, err := getRedis(s)
  1382. if err != nil {
  1383. return err
  1384. }
  1385. v, err := conn.SRem(ctx, key, values...).Result()
  1386. if err != nil {
  1387. return err
  1388. }
  1389. val = int(v)
  1390. return nil
  1391. }, acceptable)
  1392. return
  1393. }
  1394. // String returns the string representation of s.
  1395. func (s *Redis) String() string {
  1396. return s.Addr
  1397. }
  1398. // Sunion is the implementation of redis sunion command.
  1399. func (s *Redis) Sunion(keys ...string) ([]string, error) {
  1400. return s.SunionCtx(context.Background(), keys...)
  1401. }
  1402. // SunionCtx is the implementation of redis sunion command.
  1403. func (s *Redis) SunionCtx(ctx context.Context, keys ...string) (val []string, err error) {
  1404. err = s.brk.DoWithAcceptable(func() error {
  1405. conn, err := getRedis(s)
  1406. if err != nil {
  1407. return err
  1408. }
  1409. val, err = conn.SUnion(ctx, keys...).Result()
  1410. return err
  1411. }, acceptable)
  1412. return
  1413. }
  1414. // Sunionstore is the implementation of redis sunionstore command.
  1415. func (s *Redis) Sunionstore(destination string, keys ...string) (int, error) {
  1416. return s.SunionstoreCtx(context.Background(), destination, keys...)
  1417. }
  1418. // SunionstoreCtx is the implementation of redis sunionstore command.
  1419. func (s *Redis) SunionstoreCtx(ctx context.Context, destination string, keys ...string) (
  1420. val int, err error) {
  1421. err = s.brk.DoWithAcceptable(func() error {
  1422. conn, err := getRedis(s)
  1423. if err != nil {
  1424. return err
  1425. }
  1426. v, err := conn.SUnionStore(ctx, destination, keys...).Result()
  1427. if err != nil {
  1428. return err
  1429. }
  1430. val = int(v)
  1431. return nil
  1432. }, acceptable)
  1433. return
  1434. }
  1435. // Sdiff is the implementation of redis sdiff command.
  1436. func (s *Redis) Sdiff(keys ...string) ([]string, error) {
  1437. return s.SdiffCtx(context.Background(), keys...)
  1438. }
  1439. // SdiffCtx is the implementation of redis sdiff command.
  1440. func (s *Redis) SdiffCtx(ctx context.Context, keys ...string) (val []string, err error) {
  1441. err = s.brk.DoWithAcceptable(func() error {
  1442. conn, err := getRedis(s)
  1443. if err != nil {
  1444. return err
  1445. }
  1446. val, err = conn.SDiff(ctx, keys...).Result()
  1447. return err
  1448. }, acceptable)
  1449. return
  1450. }
  1451. // Sdiffstore is the implementation of redis sdiffstore command.
  1452. func (s *Redis) Sdiffstore(destination string, keys ...string) (int, error) {
  1453. return s.SdiffstoreCtx(context.Background(), destination, keys...)
  1454. }
  1455. // SdiffstoreCtx is the implementation of redis sdiffstore command.
  1456. func (s *Redis) SdiffstoreCtx(ctx context.Context, destination string, keys ...string) (
  1457. val int, err error) {
  1458. err = s.brk.DoWithAcceptable(func() error {
  1459. conn, err := getRedis(s)
  1460. if err != nil {
  1461. return err
  1462. }
  1463. v, err := conn.SDiffStore(ctx, destination, keys...).Result()
  1464. if err != nil {
  1465. return err
  1466. }
  1467. val = int(v)
  1468. return nil
  1469. }, acceptable)
  1470. return
  1471. }
  1472. // Sinter is the implementation of redis sinter command.
  1473. func (s *Redis) Sinter(keys ...string) ([]string, error) {
  1474. return s.SinterCtx(context.Background(), keys...)
  1475. }
  1476. // SinterCtx is the implementation of redis sinter command.
  1477. func (s *Redis) SinterCtx(ctx context.Context, keys ...string) (val []string, err error) {
  1478. err = s.brk.DoWithAcceptable(func() error {
  1479. conn, err := getRedis(s)
  1480. if err != nil {
  1481. return err
  1482. }
  1483. val, err = conn.SInter(ctx, keys...).Result()
  1484. return err
  1485. }, acceptable)
  1486. return
  1487. }
  1488. // Sinterstore is the implementation of redis sinterstore command.
  1489. func (s *Redis) Sinterstore(destination string, keys ...string) (int, error) {
  1490. return s.SinterstoreCtx(context.Background(), destination, keys...)
  1491. }
  1492. // SinterstoreCtx is the implementation of redis sinterstore command.
  1493. func (s *Redis) SinterstoreCtx(ctx context.Context, destination string, keys ...string) (
  1494. val int, err error) {
  1495. err = s.brk.DoWithAcceptable(func() error {
  1496. conn, err := getRedis(s)
  1497. if err != nil {
  1498. return err
  1499. }
  1500. v, err := conn.SInterStore(ctx, destination, keys...).Result()
  1501. if err != nil {
  1502. return err
  1503. }
  1504. val = int(v)
  1505. return nil
  1506. }, acceptable)
  1507. return
  1508. }
  1509. // Ttl is the implementation of redis ttl command.
  1510. func (s *Redis) Ttl(key string) (int, error) {
  1511. return s.TtlCtx(context.Background(), key)
  1512. }
  1513. // TtlCtx is the implementation of redis ttl command.
  1514. func (s *Redis) TtlCtx(ctx context.Context, key string) (val int, err error) {
  1515. err = s.brk.DoWithAcceptable(func() error {
  1516. conn, err := getRedis(s)
  1517. if err != nil {
  1518. return err
  1519. }
  1520. duration, err := conn.TTL(ctx, key).Result()
  1521. if err != nil {
  1522. return err
  1523. }
  1524. val = int(duration / time.Second)
  1525. return nil
  1526. }, acceptable)
  1527. return
  1528. }
  1529. // Zadd is the implementation of redis zadd command.
  1530. func (s *Redis) Zadd(key string, score int64, value string) (bool, error) {
  1531. return s.ZaddCtx(context.Background(), key, score, value)
  1532. }
  1533. // ZaddFloat is the implementation of redis zadd command.
  1534. func (s *Redis) ZaddFloat(key string, score float64, value string) (bool, error) {
  1535. return s.ZaddFloatCtx(context.Background(), key, score, value)
  1536. }
  1537. // ZaddCtx is the implementation of redis zadd command.
  1538. func (s *Redis) ZaddCtx(ctx context.Context, key string, score int64, value string) (
  1539. val bool, err error) {
  1540. return s.ZaddFloatCtx(ctx, key, float64(score), value)
  1541. }
  1542. // ZaddFloatCtx is the implementation of redis zadd command.
  1543. func (s *Redis) ZaddFloatCtx(ctx context.Context, key string, score float64, value string) (
  1544. val bool, err error) {
  1545. err = s.brk.DoWithAcceptable(func() error {
  1546. conn, err := getRedis(s)
  1547. if err != nil {
  1548. return err
  1549. }
  1550. v, err := conn.ZAdd(ctx, key, &red.Z{
  1551. Score: score,
  1552. Member: value,
  1553. }).Result()
  1554. if err != nil {
  1555. return err
  1556. }
  1557. val = v == 1
  1558. return nil
  1559. }, acceptable)
  1560. return
  1561. }
  1562. // Zadds is the implementation of redis zadds command.
  1563. func (s *Redis) Zadds(key string, ps ...Pair) (int64, error) {
  1564. return s.ZaddsCtx(context.Background(), key, ps...)
  1565. }
  1566. // ZaddsCtx is the implementation of redis zadds command.
  1567. func (s *Redis) ZaddsCtx(ctx context.Context, key string, ps ...Pair) (val int64, err error) {
  1568. err = s.brk.DoWithAcceptable(func() error {
  1569. conn, err := getRedis(s)
  1570. if err != nil {
  1571. return err
  1572. }
  1573. var zs []*red.Z
  1574. for _, p := range ps {
  1575. z := &red.Z{Score: float64(p.Score), Member: p.Key}
  1576. zs = append(zs, z)
  1577. }
  1578. v, err := conn.ZAdd(ctx, key, zs...).Result()
  1579. if err != nil {
  1580. return err
  1581. }
  1582. val = v
  1583. return nil
  1584. }, acceptable)
  1585. return
  1586. }
  1587. // Zcard is the implementation of redis zcard command.
  1588. func (s *Redis) Zcard(key string) (int, error) {
  1589. return s.ZcardCtx(context.Background(), key)
  1590. }
  1591. // ZcardCtx is the implementation of redis zcard command.
  1592. func (s *Redis) ZcardCtx(ctx context.Context, key string) (val int, err error) {
  1593. err = s.brk.DoWithAcceptable(func() error {
  1594. conn, err := getRedis(s)
  1595. if err != nil {
  1596. return err
  1597. }
  1598. v, err := conn.ZCard(ctx, key).Result()
  1599. if err != nil {
  1600. return err
  1601. }
  1602. val = int(v)
  1603. return nil
  1604. }, acceptable)
  1605. return
  1606. }
  1607. // Zcount is the implementation of redis zcount command.
  1608. func (s *Redis) Zcount(key string, start, stop int64) (int, error) {
  1609. return s.ZcountCtx(context.Background(), key, start, stop)
  1610. }
  1611. // ZcountCtx is the implementation of redis zcount command.
  1612. func (s *Redis) ZcountCtx(ctx context.Context, key string, start, stop int64) (val int, err error) {
  1613. err = s.brk.DoWithAcceptable(func() error {
  1614. conn, err := getRedis(s)
  1615. if err != nil {
  1616. return err
  1617. }
  1618. v, err := conn.ZCount(ctx, key, strconv.FormatInt(start, 10),
  1619. strconv.FormatInt(stop, 10)).Result()
  1620. if err != nil {
  1621. return err
  1622. }
  1623. val = int(v)
  1624. return nil
  1625. }, acceptable)
  1626. return
  1627. }
  1628. // Zincrby is the implementation of redis zincrby command.
  1629. func (s *Redis) Zincrby(key string, increment int64, field string) (int64, error) {
  1630. return s.ZincrbyCtx(context.Background(), key, increment, field)
  1631. }
  1632. // ZincrbyCtx is the implementation of redis zincrby command.
  1633. func (s *Redis) ZincrbyCtx(ctx context.Context, key string, increment int64, field string) (
  1634. val int64, err error) {
  1635. err = s.brk.DoWithAcceptable(func() error {
  1636. conn, err := getRedis(s)
  1637. if err != nil {
  1638. return err
  1639. }
  1640. v, err := conn.ZIncrBy(ctx, key, float64(increment), field).Result()
  1641. if err != nil {
  1642. return err
  1643. }
  1644. val = int64(v)
  1645. return nil
  1646. }, acceptable)
  1647. return
  1648. }
  1649. // Zscore is the implementation of redis zscore command.
  1650. func (s *Redis) Zscore(key, value string) (int64, error) {
  1651. return s.ZscoreCtx(context.Background(), key, value)
  1652. }
  1653. // ZscoreCtx is the implementation of redis zscore command.
  1654. func (s *Redis) ZscoreCtx(ctx context.Context, key, value string) (val int64, err error) {
  1655. err = s.brk.DoWithAcceptable(func() error {
  1656. conn, err := getRedis(s)
  1657. if err != nil {
  1658. return err
  1659. }
  1660. v, err := conn.ZScore(ctx, key, value).Result()
  1661. if err != nil {
  1662. return err
  1663. }
  1664. val = int64(v)
  1665. return nil
  1666. }, acceptable)
  1667. return
  1668. }
  1669. // ZscoreByFloat is the implementation of redis zscore command score by float.
  1670. func (s *Redis) ZscoreByFloat(key, value string) (float64, error) {
  1671. return s.ZscoreByFloatCtx(context.Background(), key, value)
  1672. }
  1673. // ZscoreByFloatCtx is the implementation of redis zscore command score by float.
  1674. func (s *Redis) ZscoreByFloatCtx(ctx context.Context, key, value string) (val float64, err error) {
  1675. err = s.brk.DoWithAcceptable(func() error {
  1676. conn, err := getRedis(s)
  1677. if err != nil {
  1678. return err
  1679. }
  1680. val, err = conn.ZScore(ctx, key, value).Result()
  1681. return err
  1682. }, acceptable)
  1683. return
  1684. }
  1685. // Zscan is the implementation of redis zscan command.
  1686. func (s *Redis) Zscan(key string, cursor uint64, match string, count int64) (
  1687. keys []string, cur uint64, err error) {
  1688. return s.ZscanCtx(context.Background(), key, cursor, match, count)
  1689. }
  1690. // ZscanCtx is the implementation of redis zscan command.
  1691. func (s *Redis) ZscanCtx(ctx context.Context, key string, cursor uint64, match string, count int64) (
  1692. keys []string, cur uint64, err error) {
  1693. err = s.brk.DoWithAcceptable(func() error {
  1694. conn, err := getRedis(s)
  1695. if err != nil {
  1696. return err
  1697. }
  1698. keys, cur, err = conn.ZScan(ctx, key, cursor, match, count).Result()
  1699. return err
  1700. }, acceptable)
  1701. return
  1702. }
  1703. // Zrank is the implementation of redis zrank command.
  1704. func (s *Redis) Zrank(key, field string) (int64, error) {
  1705. return s.ZrankCtx(context.Background(), key, field)
  1706. }
  1707. // ZrankCtx is the implementation of redis zrank command.
  1708. func (s *Redis) ZrankCtx(ctx context.Context, key, field string) (val int64, err error) {
  1709. err = s.brk.DoWithAcceptable(func() error {
  1710. conn, err := getRedis(s)
  1711. if err != nil {
  1712. return err
  1713. }
  1714. val, err = conn.ZRank(ctx, key, field).Result()
  1715. return err
  1716. }, acceptable)
  1717. return
  1718. }
  1719. // Zrem is the implementation of redis zrem command.
  1720. func (s *Redis) Zrem(key string, values ...interface{}) (int, error) {
  1721. return s.ZremCtx(context.Background(), key, values...)
  1722. }
  1723. // ZremCtx is the implementation of redis zrem command.
  1724. func (s *Redis) ZremCtx(ctx context.Context, key string, values ...interface{}) (val int, err error) {
  1725. err = s.brk.DoWithAcceptable(func() error {
  1726. conn, err := getRedis(s)
  1727. if err != nil {
  1728. return err
  1729. }
  1730. v, err := conn.ZRem(ctx, key, values...).Result()
  1731. if err != nil {
  1732. return err
  1733. }
  1734. val = int(v)
  1735. return nil
  1736. }, acceptable)
  1737. return
  1738. }
  1739. // Zremrangebyscore is the implementation of redis zremrangebyscore command.
  1740. func (s *Redis) Zremrangebyscore(key string, start, stop int64) (int, error) {
  1741. return s.ZremrangebyscoreCtx(context.Background(), key, start, stop)
  1742. }
  1743. // ZremrangebyscoreCtx is the implementation of redis zremrangebyscore command.
  1744. func (s *Redis) ZremrangebyscoreCtx(ctx context.Context, key string, start, stop int64) (
  1745. val int, err error) {
  1746. err = s.brk.DoWithAcceptable(func() error {
  1747. conn, err := getRedis(s)
  1748. if err != nil {
  1749. return err
  1750. }
  1751. v, err := conn.ZRemRangeByScore(ctx, key, strconv.FormatInt(start, 10),
  1752. strconv.FormatInt(stop, 10)).Result()
  1753. if err != nil {
  1754. return err
  1755. }
  1756. val = int(v)
  1757. return nil
  1758. }, acceptable)
  1759. return
  1760. }
  1761. // Zremrangebyrank is the implementation of redis zremrangebyrank command.
  1762. func (s *Redis) Zremrangebyrank(key string, start, stop int64) (int, error) {
  1763. return s.ZremrangebyrankCtx(context.Background(), key, start, stop)
  1764. }
  1765. // ZremrangebyrankCtx is the implementation of redis zremrangebyrank command.
  1766. func (s *Redis) ZremrangebyrankCtx(ctx context.Context, key string, start, stop int64) (
  1767. val int, err error) {
  1768. err = s.brk.DoWithAcceptable(func() error {
  1769. conn, err := getRedis(s)
  1770. if err != nil {
  1771. return err
  1772. }
  1773. v, err := conn.ZRemRangeByRank(ctx, key, start, stop).Result()
  1774. if err != nil {
  1775. return err
  1776. }
  1777. val = int(v)
  1778. return nil
  1779. }, acceptable)
  1780. return
  1781. }
  1782. // Zrange is the implementation of redis zrange command.
  1783. func (s *Redis) Zrange(key string, start, stop int64) ([]string, error) {
  1784. return s.ZrangeCtx(context.Background(), key, start, stop)
  1785. }
  1786. // ZrangeCtx is the implementation of redis zrange command.
  1787. func (s *Redis) ZrangeCtx(ctx context.Context, key string, start, stop int64) (
  1788. val []string, err error) {
  1789. err = s.brk.DoWithAcceptable(func() error {
  1790. conn, err := getRedis(s)
  1791. if err != nil {
  1792. return err
  1793. }
  1794. val, err = conn.ZRange(ctx, key, start, stop).Result()
  1795. return err
  1796. }, acceptable)
  1797. return
  1798. }
  1799. // ZrangeWithScores is the implementation of redis zrange command with scores.
  1800. func (s *Redis) ZrangeWithScores(key string, start, stop int64) ([]Pair, error) {
  1801. return s.ZrangeWithScoresCtx(context.Background(), key, start, stop)
  1802. }
  1803. // ZrangeWithScoresCtx is the implementation of redis zrange command with scores.
  1804. func (s *Redis) ZrangeWithScoresCtx(ctx context.Context, key string, start, stop int64) (
  1805. val []Pair, err error) {
  1806. err = s.brk.DoWithAcceptable(func() error {
  1807. conn, err := getRedis(s)
  1808. if err != nil {
  1809. return err
  1810. }
  1811. v, err := conn.ZRangeWithScores(ctx, key, start, stop).Result()
  1812. if err != nil {
  1813. return err
  1814. }
  1815. val = toPairs(v)
  1816. return nil
  1817. }, acceptable)
  1818. return
  1819. }
  1820. // ZrangeWithScoresByFloat is the implementation of redis zrange command with scores by float64.
  1821. func (s *Redis) ZrangeWithScoresByFloat(key string, start, stop int64) ([]PairFloat, error) {
  1822. return s.ZrangeWithScoresByFloatCtx(context.Background(), key, start, stop)
  1823. }
  1824. // ZrangeWithScoresByFloatCtx is the implementation of redis zrange command with scores by float64.
  1825. func (s *Redis) ZrangeWithScoresByFloatCtx(ctx context.Context, key string, start, stop int64) (
  1826. val []PairFloat, err error) {
  1827. err = s.brk.DoWithAcceptable(func() error {
  1828. conn, err := getRedis(s)
  1829. if err != nil {
  1830. return err
  1831. }
  1832. v, err := conn.ZRangeWithScores(ctx, key, start, stop).Result()
  1833. if err != nil {
  1834. return err
  1835. }
  1836. val = toPairsByFloat(v)
  1837. return nil
  1838. }, acceptable)
  1839. return
  1840. }
  1841. // ZRevRangeWithScores is the implementation of redis zrevrange command with scores.
  1842. func (s *Redis) ZRevRangeWithScores(key string, start, stop int64) ([]Pair, error) {
  1843. return s.ZRevRangeWithScoresCtx(context.Background(), key, start, stop)
  1844. }
  1845. // ZRevRangeWithScoresCtx is the implementation of redis zrevrange command with scores.
  1846. func (s *Redis) ZRevRangeWithScoresCtx(ctx context.Context, key string, start, stop int64) (
  1847. val []Pair, err error) {
  1848. err = s.brk.DoWithAcceptable(func() error {
  1849. conn, err := getRedis(s)
  1850. if err != nil {
  1851. return err
  1852. }
  1853. v, err := conn.ZRevRangeWithScores(ctx, key, start, stop).Result()
  1854. if err != nil {
  1855. return err
  1856. }
  1857. val = toPairs(v)
  1858. return nil
  1859. }, acceptable)
  1860. return
  1861. }
  1862. // ZRevRangeWithScoresByFloat is the implementation of redis zrevrange command with scores by float.
  1863. func (s *Redis) ZRevRangeWithScoresByFloat(key string, start, stop int64) ([]PairFloat, error) {
  1864. return s.ZRevRangeWithScoresByFloatCtx(context.Background(), key, start, stop)
  1865. }
  1866. // ZRevRangeWithScoresByFloatCtx is the implementation of redis zrevrange command with scores by float.
  1867. func (s *Redis) ZRevRangeWithScoresByFloatCtx(ctx context.Context, key string, start, stop int64) (
  1868. val []PairFloat, err error) {
  1869. err = s.brk.DoWithAcceptable(func() error {
  1870. conn, err := getRedis(s)
  1871. if err != nil {
  1872. return err
  1873. }
  1874. v, err := conn.ZRevRangeWithScores(ctx, key, start, stop).Result()
  1875. if err != nil {
  1876. return err
  1877. }
  1878. val = toPairsByFloat(v)
  1879. return nil
  1880. }, acceptable)
  1881. return
  1882. }
  1883. // ZrangebyscoreWithScores is the implementation of redis zrangebyscore command with scores.
  1884. func (s *Redis) ZrangebyscoreWithScores(key string, start, stop int64) ([]Pair, error) {
  1885. return s.ZrangebyscoreWithScoresCtx(context.Background(), key, start, stop)
  1886. }
  1887. // ZrangebyscoreWithScoresCtx is the implementation of redis zrangebyscore command with scores.
  1888. func (s *Redis) ZrangebyscoreWithScoresCtx(ctx context.Context, key string, start, stop int64) (
  1889. val []Pair, err error) {
  1890. err = s.brk.DoWithAcceptable(func() error {
  1891. conn, err := getRedis(s)
  1892. if err != nil {
  1893. return err
  1894. }
  1895. v, err := conn.ZRangeByScoreWithScores(ctx, key, &red.ZRangeBy{
  1896. Min: strconv.FormatInt(start, 10),
  1897. Max: strconv.FormatInt(stop, 10),
  1898. }).Result()
  1899. if err != nil {
  1900. return err
  1901. }
  1902. val = toPairs(v)
  1903. return nil
  1904. }, acceptable)
  1905. return
  1906. }
  1907. // ZrangebyscoreWithScoresByFloat is the implementation of redis zrangebyscore command with scores by float.
  1908. func (s *Redis) ZrangebyscoreWithScoresByFloat(key string, start, stop float64) ([]PairFloat, error) {
  1909. return s.ZrangebyscoreWithScoresByFloatCtx(context.Background(), key, start, stop)
  1910. }
  1911. // ZrangebyscoreWithScoresByFloatCtx is the implementation of redis zrangebyscore command with scores by float.
  1912. func (s *Redis) ZrangebyscoreWithScoresByFloatCtx(ctx context.Context, key string, start, stop float64) (
  1913. val []PairFloat, err error) {
  1914. err = s.brk.DoWithAcceptable(func() error {
  1915. conn, err := getRedis(s)
  1916. if err != nil {
  1917. return err
  1918. }
  1919. v, err := conn.ZRangeByScoreWithScores(ctx, key, &red.ZRangeBy{
  1920. Min: fmt.Sprintf("%v", start),
  1921. Max: fmt.Sprintf("%v", stop),
  1922. }).Result()
  1923. if err != nil {
  1924. return err
  1925. }
  1926. val = toPairsByFloat(v)
  1927. return nil
  1928. }, acceptable)
  1929. return
  1930. }
  1931. // ZrangebyscoreWithScoresAndLimit is the implementation of redis zrangebyscore command
  1932. // with scores and limit.
  1933. func (s *Redis) ZrangebyscoreWithScoresAndLimit(key string, start, stop int64,
  1934. page, size int) ([]Pair, error) {
  1935. return s.ZrangebyscoreWithScoresAndLimitCtx(context.Background(), key, start, stop, page, size)
  1936. }
  1937. // ZrangebyscoreWithScoresAndLimitCtx is the implementation of redis zrangebyscore command
  1938. // with scores and limit.
  1939. func (s *Redis) ZrangebyscoreWithScoresAndLimitCtx(ctx context.Context, key string, start,
  1940. stop int64, page, size int) (val []Pair, err error) {
  1941. err = s.brk.DoWithAcceptable(func() error {
  1942. if size <= 0 {
  1943. return nil
  1944. }
  1945. conn, err := getRedis(s)
  1946. if err != nil {
  1947. return err
  1948. }
  1949. v, err := conn.ZRangeByScoreWithScores(ctx, key, &red.ZRangeBy{
  1950. Min: strconv.FormatInt(start, 10),
  1951. Max: strconv.FormatInt(stop, 10),
  1952. Offset: int64(page * size),
  1953. Count: int64(size),
  1954. }).Result()
  1955. if err != nil {
  1956. return err
  1957. }
  1958. val = toPairs(v)
  1959. return nil
  1960. }, acceptable)
  1961. return
  1962. }
  1963. // ZrangebyscoreWithScoresByFloatAndLimit is the implementation of redis zrangebyscore command
  1964. // with scores by float and limit.
  1965. func (s *Redis) ZrangebyscoreWithScoresByFloatAndLimit(key string, start, stop float64,
  1966. page, size int) ([]PairFloat, error) {
  1967. return s.ZrangebyscoreWithScoresByFloatAndLimitCtx(context.Background(), key, start, stop, page, size)
  1968. }
  1969. // ZrangebyscoreWithScoresByFloatAndLimitCtx is the implementation of redis zrangebyscore command
  1970. // with scores by float and limit.
  1971. func (s *Redis) ZrangebyscoreWithScoresByFloatAndLimitCtx(ctx context.Context, key string, start,
  1972. stop float64, page, size int) (val []PairFloat, err error) {
  1973. err = s.brk.DoWithAcceptable(func() error {
  1974. if size <= 0 {
  1975. return nil
  1976. }
  1977. conn, err := getRedis(s)
  1978. if err != nil {
  1979. return err
  1980. }
  1981. v, err := conn.ZRangeByScoreWithScores(ctx, key, &red.ZRangeBy{
  1982. Min: fmt.Sprintf("%v", start),
  1983. Max: fmt.Sprintf("%v", stop),
  1984. Offset: int64(page * size),
  1985. Count: int64(size),
  1986. }).Result()
  1987. if err != nil {
  1988. return err
  1989. }
  1990. val = toPairsByFloat(v)
  1991. return nil
  1992. }, acceptable)
  1993. return
  1994. }
  1995. // Zrevrange is the implementation of redis zrevrange command.
  1996. func (s *Redis) Zrevrange(key string, start, stop int64) ([]string, error) {
  1997. return s.ZrevrangeCtx(context.Background(), key, start, stop)
  1998. }
  1999. // ZrevrangeCtx is the implementation of redis zrevrange command.
  2000. func (s *Redis) ZrevrangeCtx(ctx context.Context, key string, start, stop int64) (
  2001. val []string, err error) {
  2002. err = s.brk.DoWithAcceptable(func() error {
  2003. conn, err := getRedis(s)
  2004. if err != nil {
  2005. return err
  2006. }
  2007. val, err = conn.ZRevRange(ctx, key, start, stop).Result()
  2008. return err
  2009. }, acceptable)
  2010. return
  2011. }
  2012. // ZrevrangebyscoreWithScores is the implementation of redis zrevrangebyscore command with scores.
  2013. func (s *Redis) ZrevrangebyscoreWithScores(key string, start, stop int64) ([]Pair, error) {
  2014. return s.ZrevrangebyscoreWithScoresCtx(context.Background(), key, start, stop)
  2015. }
  2016. // ZrevrangebyscoreWithScoresCtx is the implementation of redis zrevrangebyscore command with scores.
  2017. func (s *Redis) ZrevrangebyscoreWithScoresCtx(ctx context.Context, key string, start, stop int64) (
  2018. val []Pair, err error) {
  2019. err = s.brk.DoWithAcceptable(func() error {
  2020. conn, err := getRedis(s)
  2021. if err != nil {
  2022. return err
  2023. }
  2024. v, err := conn.ZRevRangeByScoreWithScores(ctx, key, &red.ZRangeBy{
  2025. Min: strconv.FormatInt(start, 10),
  2026. Max: strconv.FormatInt(stop, 10),
  2027. }).Result()
  2028. if err != nil {
  2029. return err
  2030. }
  2031. val = toPairs(v)
  2032. return nil
  2033. }, acceptable)
  2034. return
  2035. }
  2036. // ZrevrangebyscoreWithScoresByFloat is the implementation of redis zrevrangebyscore command with scores by float.
  2037. func (s *Redis) ZrevrangebyscoreWithScoresByFloat(key string, start, stop float64) ([]PairFloat, error) {
  2038. return s.ZrevrangebyscoreWithScoresByFloatCtx(context.Background(), key, start, stop)
  2039. }
  2040. // ZrevrangebyscoreWithScoresByFloatCtx is the implementation of redis zrevrangebyscore command with scores by float.
  2041. func (s *Redis) ZrevrangebyscoreWithScoresByFloatCtx(ctx context.Context, key string, start, stop float64) (
  2042. val []PairFloat, err error) {
  2043. err = s.brk.DoWithAcceptable(func() error {
  2044. conn, err := getRedis(s)
  2045. if err != nil {
  2046. return err
  2047. }
  2048. v, err := conn.ZRevRangeByScoreWithScores(ctx, key, &red.ZRangeBy{
  2049. Min: fmt.Sprintf("%v", start),
  2050. Max: fmt.Sprintf("%v", stop),
  2051. }).Result()
  2052. if err != nil {
  2053. return err
  2054. }
  2055. val = toPairsByFloat(v)
  2056. return nil
  2057. }, acceptable)
  2058. return
  2059. }
  2060. // ZrevrangebyscoreWithScoresAndLimit is the implementation of redis zrevrangebyscore command
  2061. // with scores and limit.
  2062. func (s *Redis) ZrevrangebyscoreWithScoresAndLimit(key string, start, stop int64,
  2063. page, size int) ([]Pair, error) {
  2064. return s.ZrevrangebyscoreWithScoresAndLimitCtx(context.Background(), key, start, stop, page, size)
  2065. }
  2066. // ZrevrangebyscoreWithScoresAndLimitCtx is the implementation of redis zrevrangebyscore command
  2067. // with scores and limit.
  2068. func (s *Redis) ZrevrangebyscoreWithScoresAndLimitCtx(ctx context.Context, key string,
  2069. start, stop int64, page, size int) (val []Pair, err error) {
  2070. err = s.brk.DoWithAcceptable(func() error {
  2071. if size <= 0 {
  2072. return nil
  2073. }
  2074. conn, err := getRedis(s)
  2075. if err != nil {
  2076. return err
  2077. }
  2078. v, err := conn.ZRevRangeByScoreWithScores(ctx, key, &red.ZRangeBy{
  2079. Min: strconv.FormatInt(start, 10),
  2080. Max: strconv.FormatInt(stop, 10),
  2081. Offset: int64(page * size),
  2082. Count: int64(size),
  2083. }).Result()
  2084. if err != nil {
  2085. return err
  2086. }
  2087. val = toPairs(v)
  2088. return nil
  2089. }, acceptable)
  2090. return
  2091. }
  2092. // ZrevrangebyscoreWithScoresByFloatAndLimit is the implementation of redis zrevrangebyscore command
  2093. // with scores by float and limit.
  2094. func (s *Redis) ZrevrangebyscoreWithScoresByFloatAndLimit(key string, start, stop float64,
  2095. page, size int) ([]PairFloat, error) {
  2096. return s.ZrevrangebyscoreWithScoresByFloatAndLimitCtx(context.Background(), key, start, stop, page, size)
  2097. }
  2098. // ZrevrangebyscoreWithScoresByFloatAndLimitCtx is the implementation of redis zrevrangebyscore command
  2099. // with scores by float and limit.
  2100. func (s *Redis) ZrevrangebyscoreWithScoresByFloatAndLimitCtx(ctx context.Context, key string,
  2101. start, stop float64, page, size int) (val []PairFloat, err error) {
  2102. err = s.brk.DoWithAcceptable(func() error {
  2103. if size <= 0 {
  2104. return nil
  2105. }
  2106. conn, err := getRedis(s)
  2107. if err != nil {
  2108. return err
  2109. }
  2110. v, err := conn.ZRevRangeByScoreWithScores(ctx, key, &red.ZRangeBy{
  2111. Min: fmt.Sprintf("%v", start),
  2112. Max: fmt.Sprintf("%v", stop),
  2113. Offset: int64(page * size),
  2114. Count: int64(size),
  2115. }).Result()
  2116. if err != nil {
  2117. return err
  2118. }
  2119. val = toPairsByFloat(v)
  2120. return nil
  2121. }, acceptable)
  2122. return
  2123. }
  2124. // Zrevrank is the implementation of redis zrevrank command.
  2125. func (s *Redis) Zrevrank(key, field string) (int64, error) {
  2126. return s.ZrevrankCtx(context.Background(), key, field)
  2127. }
  2128. // ZrevrankCtx is the implementation of redis zrevrank command.
  2129. func (s *Redis) ZrevrankCtx(ctx context.Context, key, field string) (val int64, err error) {
  2130. err = s.brk.DoWithAcceptable(func() error {
  2131. conn, err := getRedis(s)
  2132. if err != nil {
  2133. return err
  2134. }
  2135. val, err = conn.ZRevRank(ctx, key, field).Result()
  2136. return err
  2137. }, acceptable)
  2138. return
  2139. }
  2140. // Zunionstore is the implementation of redis zunionstore command.
  2141. func (s *Redis) Zunionstore(dest string, store *ZStore) (int64, error) {
  2142. return s.ZunionstoreCtx(context.Background(), dest, store)
  2143. }
  2144. // ZunionstoreCtx is the implementation of redis zunionstore command.
  2145. func (s *Redis) ZunionstoreCtx(ctx context.Context, dest string, store *ZStore) (
  2146. val int64, err error) {
  2147. err = s.brk.DoWithAcceptable(func() error {
  2148. conn, err := getRedis(s)
  2149. if err != nil {
  2150. return err
  2151. }
  2152. val, err = conn.ZUnionStore(ctx, dest, store).Result()
  2153. return err
  2154. }, acceptable)
  2155. return
  2156. }
  2157. // Cluster customizes the given Redis as a cluster.
  2158. func Cluster() Option {
  2159. return func(r *Redis) {
  2160. r.Type = ClusterType
  2161. }
  2162. }
  2163. // SetSlowThreshold sets the slow threshold.
  2164. func SetSlowThreshold(threshold time.Duration) {
  2165. slowThreshold.Set(threshold)
  2166. }
  2167. // WithPass customizes the given Redis with given password.
  2168. func WithPass(pass string) Option {
  2169. return func(r *Redis) {
  2170. r.Pass = pass
  2171. }
  2172. }
  2173. // WithTLS customizes the given Redis with TLS enabled.
  2174. func WithTLS() Option {
  2175. return func(r *Redis) {
  2176. r.tls = true
  2177. }
  2178. }
  2179. func acceptable(err error) bool {
  2180. return err == nil || err == red.Nil || err == context.Canceled
  2181. }
  2182. func getRedis(r *Redis) (RedisNode, error) {
  2183. switch r.Type {
  2184. case ClusterType:
  2185. return getCluster(r)
  2186. case NodeType:
  2187. return getClient(r)
  2188. default:
  2189. return nil, fmt.Errorf("redis type '%s' is not supported", r.Type)
  2190. }
  2191. }
  2192. func toPairs(vals []red.Z) []Pair {
  2193. pairs := make([]Pair, len(vals))
  2194. for i, val := range vals {
  2195. switch member := val.Member.(type) {
  2196. case string:
  2197. pairs[i] = Pair{
  2198. Key: member,
  2199. Score: int64(val.Score),
  2200. }
  2201. default:
  2202. pairs[i] = Pair{
  2203. Key: mapping.Repr(val.Member),
  2204. Score: int64(val.Score),
  2205. }
  2206. }
  2207. }
  2208. return pairs
  2209. }
  2210. func toPairsByFloat(vals []red.Z) []PairFloat {
  2211. pairs := make([]PairFloat, len(vals))
  2212. for i, val := range vals {
  2213. switch member := val.Member.(type) {
  2214. case string:
  2215. pairs[i] = PairFloat{
  2216. Key: member,
  2217. Score: val.Score,
  2218. }
  2219. default:
  2220. pairs[i] = PairFloat{
  2221. Key: mapping.Repr(val.Member),
  2222. Score: val.Score,
  2223. }
  2224. }
  2225. }
  2226. return pairs
  2227. }
  2228. func toStrings(vals []interface{}) []string {
  2229. ret := make([]string, len(vals))
  2230. for i, val := range vals {
  2231. if val == nil {
  2232. ret[i] = ""
  2233. } else {
  2234. switch val := val.(type) {
  2235. case string:
  2236. ret[i] = val
  2237. default:
  2238. ret[i] = mapping.Repr(val)
  2239. }
  2240. }
  2241. }
  2242. return ret
  2243. }