goredis.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. package redis
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "hash/crc32"
  7. "log"
  8. "math"
  9. "regexp"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "github.com/go-redis/redis/v8"
  14. )
  15. //goredis 工具类对象
  16. type GoRedis struct {
  17. //Password string
  18. Code string
  19. DB int //默认库
  20. DBS []int //数据库列表
  21. Nodes []int //节点列表
  22. HashDb int //是否是多个数据库 0单库 1单节点多库 2多节点单库 3多节点多库
  23. Ctx context.Context
  24. CMap map[int]map[int]*redis.Client
  25. BakDb bool //是否有备用节点
  26. Bak *GoRedis //备用节点连接
  27. }
  28. //初始化单个"[other=]127.0.0.1:2203[|#]127.0.0.1:2204[/0]=0-1=1-10=300" [代码]、地址[|备用地址[/代表默认库][#多节点地址,默认库都为0]、库、最大池、空闲时间默认300秒
  29. /*
  30. 示例
  31. 127.0.0.1:2203
  32. 127.0.0.1:2203=2=15 默认库为2,最大连接为15,最小连接是0.2*15为3
  33. other=127.0.0.1:2203 带code配置
  34. other=127.0.0.1:2203=0=10
  35. other=127.0.0.1:2203=0=10=300 空闲时间300秒
  36. other=127.0.0.1:2203=0-8=10=300 带code配置,使用多个库即0 1 2 3 4 5 6 7 8,最大连接为10,最小连接是0.2*10为2,空闲时间300秒
  37. other=127.0.0.1:2203=0-8=1-10=300 带code配置,使用多个库即0 1 2 3 4 5 6 7 8,最大连接为10,最小连接1,空闲时间300秒
  38. other=127.0.0.1:2203|127.0.0.1:2204=0-8=10=300 带code配置,使用备用节点(仅供取数据时使用),使用多个库即0 1 2 3 4 5 6 7 8,最大连接为10,最小连接1,空闲时间300秒
  39. other=127.0.0.1:2203|127.0.0.1:2204/0=0-8=10=300 带code配置,使用备用节点(仅供取数据时使用,备用节点默认库为0),使用多个库即0 1 2 3 4 5 6 7 8,最大连接为10,最小连接1,空闲时间300秒
  40. other=127.0.0.1:2203#127.0.0.1:2204#127.0.0.1:2205#127.0.0.1:2206=0=10=300 带code配置,使用多节点模式(多节点所有默认库为0),最大连接为10,最小连接2,空闲时间300秒
  41. 注意:
  42. 1、当使用多节点时,用正则获取keys时,再用Get取模获取数据时,一定要保障放入和取出是同一个hashcode,否则数据永远取不到
  43. */
  44. //解析配置并初始化,opt为string或map
  45. func (r *GoRedis) Init(opt interface{}) {
  46. check := false
  47. //代码 地址 []库范围 []池配置 空闲时间
  48. code, addr, dbs, pool, idle := "", "", []int{0}, []int{2, 30}, 300
  49. if so, ok := opt.(string); ok {
  50. arr := strings.Split(so, "=")
  51. regAddr := regexp.MustCompile("[0-9.a-zA-Z/]+:[0-9]+.*")
  52. if len(arr) == 1 { //只是一个串
  53. if regAddr.MatchString(arr[0]) {
  54. check = true
  55. addr = arr[0]
  56. }
  57. } else if len(arr) > 1 {
  58. index := 0
  59. if regAddr.MatchString(arr[0]) { //第1个是地址
  60. index = 1
  61. addr = arr[0]
  62. check = true
  63. } else if regAddr.MatchString(arr[1]) { //第二个是地址
  64. check = true
  65. addr = arr[1]
  66. code = arr[0]
  67. }
  68. //解析库配置
  69. if len(arr) > 2-index { //dbs配置
  70. dbs1 := strings.Split(arr[2-index], "-") //库范围的配置
  71. if len(dbs1) == 1 || len(dbs1) == 2 {
  72. check = true
  73. dbs[0], _ = strconv.Atoi(dbs1[0])
  74. if len(dbs1) == 2 {
  75. tmp, _ := strconv.Atoi(dbs1[1])
  76. dbs = append(dbs, tmp)
  77. }
  78. } else {
  79. check = false
  80. }
  81. //解析连接池配置
  82. if len(arr) > 3-index {
  83. pool1 := strings.Split(arr[3-index], "-")
  84. if len(pool1) == 1 || len(pool1) == 2 {
  85. check = true
  86. if len(pool1) == 1 {
  87. pool[1], _ = strconv.Atoi(pool1[0])
  88. pool[0] = int(math.Ceil(float64(pool[1]) * 0.2))
  89. } else {
  90. pool[0], _ = strconv.Atoi(pool1[0])
  91. pool[1], _ = strconv.Atoi(pool1[1])
  92. }
  93. } else {
  94. check = false
  95. }
  96. //解析最大空闲时间配置
  97. if len(arr) > 4-index {
  98. idle, _ = strconv.Atoi(arr[4-index])
  99. if idle == 0 {
  100. idle = 300
  101. }
  102. }
  103. }
  104. }
  105. }
  106. } else if _, ok := opt.(map[string]interface{}); ok {
  107. }
  108. if check {
  109. log.Println("代码 地址 []库范围 []池配置 空闲时间", code, addr, dbs, pool, idle)
  110. addrs := strings.Split(addr, "|") //备用节点模式
  111. if len(addrs) == 2 { //备用节点的模式 2选1
  112. r.init(addrs[0], code, dbs, pool[1], pool[0], idle)
  113. r.BakDb = true
  114. //有备用节点,集群先不考虑,支持指定的库
  115. addr1 := strings.Split(addrs[1], "/")
  116. r.Bak = &GoRedis{}
  117. if len(addr1) == 2 { //没有指定库 根据/区分
  118. i, _ := strconv.Atoi(addr1[1])
  119. dbs = []int{i}
  120. }
  121. r.Bak.init(addr1[0], code, dbs, pool[1], pool[0], idle)
  122. } else if len(addrs) == 1 {
  123. addr1 := strings.Split(addrs[0], "#") //是多节点模式,所有库默认为0
  124. if len(addr1) == 1 {
  125. r.init(addr1[0], code, dbs, pool[1], pool[0], idle)
  126. } else { //多节点模式
  127. r.Code = code
  128. r.HashDb = 1 + len(dbs)
  129. if len(dbs) > 1 {
  130. r.DBS = []int{}
  131. for i := dbs[0]; i <= dbs[1]; i++ {
  132. r.DBS = append(r.DBS, i)
  133. }
  134. } else {
  135. r.DBS = dbs
  136. }
  137. r.DB = dbs[0]
  138. r.Ctx = context.Background()
  139. r.CMap = map[int]map[int]*redis.Client{}
  140. r.Nodes = []int{}
  141. for i := 0; i < len(addr1); i++ {
  142. r.Nodes = append(r.Nodes, i)
  143. r.CMap[i] = map[int]*redis.Client{}
  144. for k, v := range r.DBS { //按节点的每个库初始化
  145. r.CMap[i][k] = redis.NewClient(&redis.Options{
  146. Addr: addr1[i],
  147. DB: v,
  148. PoolSize: pool[1],
  149. MinIdleConns: pool[0],
  150. IdleTimeout: time.Duration(idle) * time.Second,
  151. })
  152. }
  153. }
  154. }
  155. }
  156. }
  157. log.Println(check, code, addr, dbs, pool, idle, r.DBS)
  158. }
  159. //初始化连接
  160. func (r *GoRedis) init(addr, code string, dbs []int, poolSize, minIdleConns, idleTimeOut int) {
  161. r.Code = code
  162. r.DB = dbs[0]
  163. r.Ctx = context.Background()
  164. r.CMap = map[int]map[int]*redis.Client{}
  165. r.CMap[0] = map[int]*redis.Client{}
  166. if len(dbs) > 1 {
  167. r.HashDb = len(dbs) - 1
  168. r.DBS = []int{}
  169. for i := dbs[0]; i <= dbs[1]; i++ {
  170. r.DBS = append(r.DBS, i)
  171. }
  172. } else {
  173. r.DBS = dbs
  174. }
  175. for k, v := range r.DBS { //按节点的每个库初始化
  176. r.CMap[0][k] = redis.NewClient(&redis.Options{
  177. Addr: addr,
  178. DB: v,
  179. PoolSize: poolSize,
  180. MinIdleConns: minIdleConns,
  181. IdleTimeout: time.Duration(idleTimeOut) * time.Second,
  182. })
  183. }
  184. }
  185. //int转timeDuration
  186. func D(t int) time.Duration {
  187. return time.Duration(t) * time.Second
  188. }
  189. //取db
  190. func (r *GoRedis) GetDB(key string) (int, int) {
  191. switch r.HashDb {
  192. case 0: //单节点单库
  193. return 0, 0
  194. case 1: //单节点多库
  195. return 0, hashCode(key) % len(r.DBS)
  196. case 2: //多节点单库
  197. return hashCode(key[len(key)/2:]) % len(r.Nodes), 0
  198. case 3: ////多节点多库
  199. return hashCode(key[len(key)/2:]) % len(r.Nodes), hashCode(key) % len(r.DBS)
  200. }
  201. // if r.HashDb {
  202. // //return int(key[len(key)-1]) % len(r.DBS)
  203. // return hashCode(key) % len(r.DBS)
  204. // } else {
  205. // return r.DB
  206. // }
  207. return 0, 0
  208. }
  209. //根据key取hash
  210. func hashCode(key string) int {
  211. v := int(crc32.ChecksumIEEE([]byte(key)))
  212. if v < 0 {
  213. v = -v
  214. }
  215. return v
  216. }
  217. func (r *GoRedis) Client(key string) *redis.Client {
  218. _i, _k := r.GetDB(key)
  219. m := r.CMap[_i]
  220. if m != nil {
  221. c := m[_k]
  222. if c != nil {
  223. return c
  224. }
  225. }
  226. return &redis.Client{}
  227. }
  228. //-----具体方法
  229. //简单的Put方法
  230. func (r *GoRedis) Put(key string, val interface{}) (string, error) {
  231. stutsCmd := r.Client(key).Set(r.Ctx, key, val, 0)
  232. str, err := stutsCmd.Result()
  233. return str, err
  234. }
  235. //存key,加过期时间
  236. func (r *GoRedis) Set(key string, val interface{}, timeout int) (string, error) {
  237. stutsCmd := r.Client(key).Set(r.Ctx, key, val, D(timeout))
  238. //cmd := r.CMap[r.GetDB(key)].Do(r.Ctx, "setex", key, timeout, val)
  239. str, err := stutsCmd.Result()
  240. return str, err
  241. }
  242. //简单的Get方法,无结果返回空串,err: redis: nil
  243. func (r *GoRedis) Get(key string) (string, error) {
  244. stutsCmd := r.Client(key).Get(r.Ctx, key)
  245. str, err := stutsCmd.Result()
  246. // log.Println("1", str, "-", err)
  247. if err != nil {
  248. //有备用节点
  249. if r.BakDb {
  250. str, err = r.Bak.Get(key)
  251. }
  252. }
  253. return str, err
  254. }
  255. //根据正则取key,未考虑负载、多库
  256. func (r *GoRedis) GetByPattern(key string) (res []string, err error) {
  257. serr := ""
  258. for _, v1 := range r.CMap {
  259. for _, v := range v1 {
  260. strSlice := v.Keys(r.Ctx, key)
  261. arr, err1 := strSlice.Result()
  262. if len(arr) > 0 {
  263. res = append(res, arr...)
  264. } else if err1 != nil {
  265. serr += err1.Error()
  266. }
  267. }
  268. }
  269. if serr != "" {
  270. err = errors.New(serr)
  271. }
  272. return
  273. }
  274. //批量保存数据
  275. // 不支持- MSet("key1", "value1", "key2", "value2")
  276. // 不支持- MSet([]string{"key1", "value1", "key2", "value2"})
  277. // []interface{[]interface{k1,v1},[]interface{k2,v2},[]interface{k3,v3} }
  278. // - MSet(map[string]interface{}{"key1": "value1", "key2": "value2"})
  279. func (r *GoRedis) BulkPut(timeout int, obj interface{}) {
  280. if r.HashDb > 0 {
  281. if timeout < 1 {
  282. timeout = 0
  283. }
  284. timeEx := time.Duration(timeout) * time.Second
  285. switch obj.(type) {
  286. case []interface{}:
  287. if objs, ok := obj.([]interface{}); ok {
  288. for _, _tmp := range objs {
  289. tmp, ok := _tmp.([]interface{})
  290. if ok && len(tmp) == 2 {
  291. key, ok1 := tmp[0].(string)
  292. if ok1 {
  293. r.Client(key).Set(r.Ctx, key, tmp[1], timeEx)
  294. }
  295. }
  296. }
  297. }
  298. case map[string]interface{}:
  299. if objs, ok := obj.(map[string]interface{}); ok {
  300. for k, v := range objs {
  301. r.Client(k).Set(r.Ctx, k, v, timeEx)
  302. }
  303. }
  304. }
  305. } else {
  306. //单库直接保存
  307. if timeout < 1 {
  308. stutsCmd := r.CMap[0][0].MSet(r.Ctx, obj)
  309. str, err := stutsCmd.Result()
  310. log.Println(str, err)
  311. } else {
  312. timeEx := time.Duration(timeout) * time.Second
  313. //设置超时
  314. lenth := 0
  315. cmds, err := r.CMap[0][0].Pipelined(r.Ctx, func(pipe redis.Pipeliner) error {
  316. if objs, ok := obj.([]interface{}); ok {
  317. lenth = len(objs)
  318. for _, _tmp := range objs {
  319. tmp, ok := _tmp.([]interface{})
  320. if ok && len(tmp) == 2 {
  321. key, ok1 := tmp[0].(string)
  322. if ok1 {
  323. pipe.Set(r.Ctx, key, tmp[1], timeEx)
  324. }
  325. }
  326. }
  327. } else if objs, ok := obj.(map[string]interface{}); ok {
  328. lenth = len(objs)
  329. for k, v := range objs {
  330. pipe.Set(r.Ctx, k, v, timeEx)
  331. }
  332. } else {
  333. log.Println("bulkPut type error")
  334. }
  335. return nil
  336. })
  337. if err != nil {
  338. log.Println("bulkPut error", err.Error())
  339. }
  340. if len(cmds) == lenth { //数据相等
  341. for _, v := range cmds {
  342. log.Println("cmd", v.Args(), v.String())
  343. }
  344. }
  345. }
  346. }
  347. }
  348. //设置超时时间,单位秒
  349. func (r *GoRedis) SetExpire(key string, expire int) error {
  350. boolCmd := r.Client(key).Expire(r.Ctx, key, D(expire))
  351. return boolCmd.Err()
  352. }
  353. //判断一个key是否存在
  354. func (r *GoRedis) Exists(key string) bool {
  355. intCmd := r.Client(key).Exists(r.Ctx, key)
  356. i, err := intCmd.Result()
  357. if err != nil {
  358. log.Println("redisutil-exists", key, err)
  359. }
  360. return i == 1
  361. }
  362. //直接返回字节流
  363. func (r *GoRedis) GetBytes(key string) (ret *[]byte, err error) {
  364. cmd := r.Client(key).Do(r.Ctx, "GET", key)
  365. res, err := cmd.Result()
  366. log.Println(res)
  367. if err != nil {
  368. log.Println("redisutil-GetBytesError", err)
  369. } else {
  370. if tmp, ok := res.([]byte); ok {
  371. ret = &tmp
  372. } else if tmp, ok := res.(string); ok {
  373. bs := []byte(tmp)
  374. ret = &bs
  375. } else {
  376. err = errors.New("redis返回数据格式不对")
  377. }
  378. }
  379. return
  380. }
  381. //支持删除多个key
  382. func (r *GoRedis) Del(key ...string) (b bool) {
  383. i := 0
  384. if r.HashDb > 0 {
  385. for _, k := range key {
  386. cmd := r.Client(k).Del(r.Ctx, k)
  387. i1, _ := cmd.Result()
  388. i += int(i1)
  389. }
  390. } else {
  391. intCmd := r.CMap[0][0].Del(r.Ctx, key...)
  392. i1, _ := intCmd.Result()
  393. i = int(i1)
  394. }
  395. return i == len(key)
  396. }
  397. //根据代码和前辍key删除多个
  398. func (r *GoRedis) DelByPattern(key string) {
  399. res, _ := r.GetByPattern(key)
  400. if len(res) > 0 {
  401. r.Del(res...)
  402. }
  403. }
  404. //自增计数器
  405. func (r *GoRedis) Incr(key string) (int64, error) {
  406. intCmd := r.Client(key).Incr(r.Ctx, key)
  407. i, err := intCmd.Result()
  408. return i, err
  409. }
  410. //自减
  411. func (r *GoRedis) Decrby(key string, val int) (int64, error) {
  412. intCmd := r.Client(key).DecrBy(r.Ctx, key, int64(val))
  413. i, err := intCmd.Result()
  414. return i, err
  415. }
  416. //批量取多个key
  417. func (r *GoRedis) Mget(key []string) []interface{} {
  418. if r.HashDb > 0 {
  419. mdb := map[int]map[int][]string{}
  420. for _, v := range key { //分组
  421. _i, _k := r.GetDB(v)
  422. mdb[_i] = map[int][]string{}
  423. arr := mdb[_i][_k]
  424. if arr == nil {
  425. arr = []string{v}
  426. } else {
  427. arr = append(arr, v)
  428. }
  429. mdb[_i][_k] = arr
  430. }
  431. res := []interface{}{}
  432. for k, v := range mdb {
  433. for k1, v1 := range v {
  434. sliceCmd := r.CMap[k][k1].MGet(r.Ctx, v1...)
  435. res1, err := sliceCmd.Result()
  436. if err != nil {
  437. log.Println("Mget error", err)
  438. }
  439. res = append(res, res1...)
  440. }
  441. }
  442. return res
  443. } else {
  444. sliceCmd := r.CMap[0][0].MGet(r.Ctx, key...)
  445. res, err := sliceCmd.Result()
  446. if err != nil {
  447. log.Println("Mget error", err)
  448. }
  449. return res
  450. }
  451. }
  452. //取出并删除Key
  453. func (r *GoRedis) Pop(key string) (result interface{}) {
  454. c := r.Client(key)
  455. strCmd := c.Get(r.Ctx, key)
  456. b, err := strCmd.Bytes()
  457. if err != nil {
  458. log.Println("Poperr bytes", err)
  459. return
  460. }
  461. err1 := json.Unmarshal(b, &result)
  462. if err1 != nil {
  463. log.Println("Poperr json ", err)
  464. return
  465. } else {
  466. go c.Del(r.Ctx, key)
  467. }
  468. return
  469. }
  470. //list操作
  471. func (r *GoRedis) LPOP(list string) (result interface{}) {
  472. strCmd := r.Client(list).LPop(r.Ctx, list)
  473. b, err := strCmd.Bytes()
  474. if err != nil {
  475. log.Println("LPOP bytes", err)
  476. return
  477. }
  478. err1 := json.Unmarshal(b, &result)
  479. if err1 != nil {
  480. log.Println("LPOP json ", err)
  481. return
  482. }
  483. return
  484. }
  485. //将一个或多个值插入到列表的尾部
  486. func (r *GoRedis) RPUSH(list string, val ...interface{}) bool {
  487. intCmd := r.Client(list).RPush(r.Ctx, list, val...)
  488. i, err := intCmd.Result()
  489. if err != nil {
  490. log.Println("RPUSH bytes", err)
  491. return false
  492. }
  493. return i == 1
  494. }
  495. //获取列表长度
  496. func (r *GoRedis) LLEN(list string) int64 {
  497. intCmd := r.Client(list).LLen(r.Ctx, list)
  498. i, err := intCmd.Result()
  499. if err != nil {
  500. log.Println("RPUSH bytes", err)
  501. }
  502. return i
  503. }