redis.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. v1pb "go-common/app/service/live/gift/api/grpc/v1"
  8. "go-common/app/service/live/gift/model"
  9. "go-common/library/cache/redis"
  10. "go-common/library/log"
  11. "math/rand"
  12. "time"
  13. )
  14. func dailyBagKey(uid int64) string {
  15. return fmt.Sprintf("gift:daily_bag:%s:%d", time.Now().Format("20060102"), uid)
  16. }
  17. // GetDailyBagCache GetDailyBagCache
  18. func (d *Dao) GetDailyBagCache(ctx context.Context, uid int64) (res []*v1pb.DailyBagResp_BagList, err error) {
  19. key := dailyBagKey(uid)
  20. conn := d.redis.Get(ctx)
  21. defer conn.Close()
  22. item, err := redis.Bytes(conn.Do("GET", key))
  23. if err != nil {
  24. if err == redis.ErrNil {
  25. err = nil
  26. res = nil
  27. } else {
  28. log.Error("conn.Do(GET, %s) error(%v)", key, err)
  29. }
  30. return
  31. }
  32. if err = json.Unmarshal(item, &res); err != nil {
  33. log.Error("json.Unmarshal(%s) error(%v)", string(item), err)
  34. }
  35. return
  36. }
  37. // SetDailyBagCache SetDailyBagCache
  38. func (d *Dao) SetDailyBagCache(ctx context.Context, uid int64, data []*v1pb.DailyBagResp_BagList, expire int64) (err error) {
  39. key := dailyBagKey(uid)
  40. conn := d.redis.Get(ctx)
  41. defer conn.Close()
  42. bs, err := json.Marshal(data)
  43. if err != nil {
  44. log.Error("json.Marshal(%v) err(%v)", data, err)
  45. return
  46. }
  47. _, err = conn.Do("SETEX", key, expire, bs)
  48. if err != nil {
  49. log.Error("conn.Do(SETEX, %s) error(%v)", key, err)
  50. }
  51. return
  52. }
  53. func dailyMedalBagKey(uid int64) string {
  54. return fmt.Sprintf("gift:medal:daily_gift_bag:%s:%d", time.Now().Format("20060102"), uid)
  55. }
  56. // GetMedalDailyBagCache GetMedalDailyBagCache
  57. func (d *Dao) GetMedalDailyBagCache(ctx context.Context, uid int64) (res *model.BagGiftStatus, err error) {
  58. key := dailyMedalBagKey(uid)
  59. fmt.Println(key)
  60. res = &model.BagGiftStatus{}
  61. conn := d.redis.Get(ctx)
  62. defer conn.Close()
  63. item, err := redis.Bytes(conn.Do("GET", key))
  64. if err != nil {
  65. if err == redis.ErrNil {
  66. err = nil
  67. res = nil
  68. } else {
  69. log.Error("conn.Do(GET, %s) error(%v)", key, err)
  70. }
  71. return
  72. }
  73. if err = json.Unmarshal(item, &res); err != nil {
  74. log.Error("json.Unmarshal(%s) error(%v)", string(item), err)
  75. }
  76. return
  77. }
  78. // SetMedalDailyBagCache SetMedalDailyBagCache
  79. func (d *Dao) SetMedalDailyBagCache(ctx context.Context, uid int64, data *model.BagGiftStatus, expire int64) (err error) {
  80. key := dailyMedalBagKey(uid)
  81. conn := d.redis.Get(ctx)
  82. defer conn.Close()
  83. bs, err := json.Marshal(data)
  84. if err != nil {
  85. log.Error("json.Marshal(%v) err(%v)", data, err)
  86. return
  87. }
  88. _, err = conn.Do("SETEX", key, expire, bs)
  89. if err != nil {
  90. log.Error("conn.Do(SETEX, %s) error(%v)", key, err)
  91. }
  92. return
  93. }
  94. func weekLevelBagKey(uid, level int64) string {
  95. _, week := time.Now().ISOWeek()
  96. return fmt.Sprintf("gift:level:week_gift_bag:%d:%d:%d", week, uid, level)
  97. }
  98. // GetWeekLevelBagCache GetWeekLevelBagCache
  99. func (d *Dao) GetWeekLevelBagCache(ctx context.Context, uid, level int64) (res *model.BagGiftStatus, err error) {
  100. key := weekLevelBagKey(uid, level)
  101. res = &model.BagGiftStatus{}
  102. conn := d.redis.Get(ctx)
  103. defer conn.Close()
  104. item, err := redis.Bytes(conn.Do("GET", key))
  105. if err != nil {
  106. if err == redis.ErrNil {
  107. err = nil
  108. res = nil
  109. } else {
  110. log.Error("conn.Do(GET, %s) error(%v)", key, err)
  111. }
  112. return
  113. }
  114. if err = json.Unmarshal(item, &res); err != nil {
  115. log.Error("json.Unmarshal(%s) error(%v)", string(item), err)
  116. }
  117. return
  118. }
  119. // SetWeekLevelBagCache SetWeekLevelBagCache
  120. func (d *Dao) SetWeekLevelBagCache(ctx context.Context, uid, level int64, data *model.BagGiftStatus, expire int64) (err error) {
  121. key := weekLevelBagKey(uid, level)
  122. conn := d.redis.Get(ctx)
  123. defer conn.Close()
  124. bs, err := json.Marshal(data)
  125. if err != nil {
  126. log.Error("json.Marshal(%v) err(%v)", data, err)
  127. return
  128. }
  129. _, err = conn.Do("SETEX", key, expire, bs)
  130. if err != nil {
  131. log.Error("conn.Do(SETEX, %s) error(%v)", key, err)
  132. }
  133. return
  134. }
  135. //Lock Lock
  136. func (d *Dao) Lock(ctx context.Context, key string, ttl int, retry int, retryDelay int) (gotLock bool, lockValue string, err error) {
  137. if retry <= 0 {
  138. retry = 1
  139. }
  140. lockValue = "locked:" + randomString(5)
  141. retryTimes := 0
  142. conn := d.redis.Get(ctx)
  143. defer conn.Close()
  144. realKey := lockKey(key)
  145. for ; retryTimes < retry; retryTimes++ {
  146. var res interface{}
  147. res, err = conn.Do("SET", realKey, lockValue, "PX", ttl, "NX")
  148. if err != nil {
  149. log.Error("redis_lock failed:%s:%v", realKey, err)
  150. break
  151. }
  152. if res != nil {
  153. gotLock = true
  154. break
  155. }
  156. time.Sleep(time.Duration(retryDelay) * time.Millisecond)
  157. }
  158. return
  159. }
  160. // UnLock UnLock
  161. func (d *Dao) UnLock(ctx context.Context, key string, lockValue string) (err error) {
  162. conn := d.redis.Get(ctx)
  163. defer conn.Close()
  164. realKey := lockKey(key)
  165. res, err := redis.String(conn.Do("GET", realKey))
  166. if err != nil {
  167. if err == redis.ErrNil {
  168. err = nil
  169. } else {
  170. log.Error("conn.Do(GET, %s) error(%v)", key, err)
  171. }
  172. return
  173. }
  174. if res != lockValue {
  175. err = errors.New("unlock value error")
  176. return
  177. }
  178. _, err = conn.Do("DEL", realKey)
  179. return
  180. }
  181. //ForceUnLock UnLock without lockValue
  182. func (d *Dao) ForceUnLock(ctx context.Context, key string) (err error) {
  183. realKey := lockKey(key)
  184. conn := d.redis.Get(ctx)
  185. defer conn.Close()
  186. _, err = conn.Do("DEL", realKey)
  187. return
  188. }
  189. func lockKey(key string) string {
  190. return fmt.Sprintf("gift_lock:%s", key)
  191. }
  192. func randomString(l int) string {
  193. str := "0123456789abcdefghijklmnopqrstuvwxyz"
  194. bytes := []byte(str)
  195. result := []byte{}
  196. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  197. for i := 0; i < l; i++ {
  198. result = append(result, bytes[r.Intn(len(bytes))])
  199. }
  200. return string(result)
  201. }
  202. func bagIDCache(uid, giftID, expireAt int64) string {
  203. return fmt.Sprintf("bag_id:%d:%d:%d", uid, giftID, expireAt)
  204. }
  205. // GetBagIDCache GetBagIDCache
  206. func (d *Dao) GetBagIDCache(ctx context.Context, uid, giftID, expireAt int64) (bagID int64, err error) {
  207. key := bagIDCache(uid, giftID, expireAt)
  208. conn := d.redis.Get(ctx)
  209. defer conn.Close()
  210. bagID, err = redis.Int64(conn.Do("GET", key))
  211. if err != nil {
  212. if err == redis.ErrNil {
  213. err = nil
  214. } else {
  215. log.Error("conn.Do(GET, %s) error(%v)", key, err)
  216. }
  217. return
  218. }
  219. return
  220. }
  221. // SetBagIDCache SetBagIDCache
  222. func (d *Dao) SetBagIDCache(ctx context.Context, uid, giftID, expireAt, bagID, expire int64) (err error) {
  223. key := bagIDCache(uid, giftID, expireAt)
  224. conn := d.redis.Get(ctx)
  225. defer conn.Close()
  226. _, err = conn.Do("SETEX", key, expire, bagID)
  227. if err != nil {
  228. log.Error("conn.Do(SETEX, %s) error(%v)", key, err)
  229. }
  230. return
  231. }
  232. func bagListKey(uid int64) string {
  233. return fmt.Sprintf("bag_list:%d", uid)
  234. }
  235. // GetBagListCache GetBagListCache
  236. func (d *Dao) GetBagListCache(ctx context.Context, uid int64) (res []*model.BagGiftList, err error) {
  237. key := bagListKey(uid)
  238. conn := d.redis.Get(ctx)
  239. defer conn.Close()
  240. item, err := redis.Bytes(conn.Do("GET", key))
  241. if err != nil {
  242. if err == redis.ErrNil {
  243. err = nil
  244. res = nil
  245. } else {
  246. log.Error("conn.Do(GET, %s) error(%v)", key, err)
  247. }
  248. return
  249. }
  250. if err = json.Unmarshal(item, &res); err != nil {
  251. log.Error("json.Unmarshal(%s) error(%v)", string(item), err)
  252. }
  253. return
  254. }
  255. // SetBagListCache SetBagListCache
  256. func (d *Dao) SetBagListCache(ctx context.Context, uid int64, data []*model.BagGiftList, expire int64) (err error) {
  257. key := bagListKey(uid)
  258. conn := d.redis.Get(ctx)
  259. defer conn.Close()
  260. bs, err := json.Marshal(data)
  261. if err != nil {
  262. log.Error("json.Marshal(%v) err(%v)", data, err)
  263. return
  264. }
  265. _, err = conn.Do("SETEX", key, expire, bs)
  266. if err != nil {
  267. log.Error("conn.Do(SETEX, %s) error(%v)", key, err)
  268. }
  269. return
  270. }
  271. // ClearBagListCache ClearBagListCache
  272. func (d *Dao) ClearBagListCache(ctx context.Context, uid int64) (err error) {
  273. key := bagListKey(uid)
  274. conn := d.redis.Get(ctx)
  275. defer conn.Close()
  276. _, err = conn.Do("DEL", key)
  277. if err != nil {
  278. log.Error("conn.Do(DEL, %s) error(%v)", key, err)
  279. }
  280. return
  281. }
  282. func bagNumKey(uid, giftID, expireAt int64) string {
  283. return fmt.Sprintf("bag_num:%d:%d:%d", uid, giftID, expireAt)
  284. }
  285. // SetBagNumCache SetBagNumCache
  286. func (d *Dao) SetBagNumCache(ctx context.Context, uid, giftID, expireAt, giftNum, expire int64) (err error) {
  287. key := bagNumKey(uid, giftID, expireAt)
  288. conn := d.redis.Get(ctx)
  289. defer conn.Close()
  290. _, err = conn.Do("SETEX", key, expire, giftNum)
  291. if err != nil {
  292. log.Error("conn.Do(SETEX, %s) error(%v)", key, err)
  293. }
  294. return
  295. }
  296. func vipMonthBag(uid int64) string {
  297. return fmt.Sprintf("gift:vip_month:%s:%d", time.Now().Format("200601"), uid)
  298. }
  299. // GetVipStatusCache GetVipStatusCache
  300. func (d *Dao) GetVipStatusCache(ctx context.Context, uid int64) (status int64, err error) {
  301. key := vipMonthBag(uid)
  302. conn := d.redis.Get(ctx)
  303. defer conn.Close()
  304. status, err = redis.Int64(conn.Do("GET", key))
  305. if err != nil {
  306. if err == redis.ErrNil {
  307. err = nil
  308. } else {
  309. log.Error("conn.Do(GET, %s) error(%v)", key, err)
  310. }
  311. return
  312. }
  313. return
  314. }
  315. // ClearVipStatusCache ClearVipStatusCache
  316. func (d *Dao) ClearVipStatusCache(ctx context.Context, uid int64) (err error) {
  317. key := vipMonthBag(uid)
  318. conn := d.redis.Get(ctx)
  319. defer conn.Close()
  320. _, err = conn.Do("DEL", key)
  321. if err != nil {
  322. log.Error("conn.Do(DEL, %s) error(%v)", key, err)
  323. }
  324. return
  325. }
  326. func giftBagStatus(uid int64) string {
  327. return fmt.Sprintf("gift:bag:status:%d", uid)
  328. }
  329. // GetBagStatusCache GetBagStatusCache
  330. func (d *Dao) GetBagStatusCache(ctx context.Context, uid int64) (status int64, err error) {
  331. key := giftBagStatus(uid)
  332. conn := d.redis.Get(ctx)
  333. defer conn.Close()
  334. status, err = redis.Int64(conn.Do("GET", key))
  335. if err != nil {
  336. if err == redis.ErrNil {
  337. err = nil
  338. status = -100 // means cache miss
  339. } else {
  340. log.Error("conn.Do(GET, %s) error(%v)", key, err)
  341. }
  342. return
  343. }
  344. return
  345. }
  346. // SetBagStatusCache SetBagStatusCache
  347. func (d *Dao) SetBagStatusCache(ctx context.Context, uid, status int64, expire int64) (err error) {
  348. key := giftBagStatus(uid)
  349. conn := d.redis.Get(ctx)
  350. defer conn.Close()
  351. _, err = conn.Do("SETEX", key, expire, status)
  352. if err != nil {
  353. log.Error("conn.Do(SETEX, %s) error(%v)", key, err)
  354. }
  355. return
  356. }