1
0

redis.go 61 KB

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