redis.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. amodel "go-common/app/admin/openplatform/sug/model"
  6. "go-common/app/job/openplatform/open-sug/model"
  7. "go-common/library/cache/redis"
  8. "strconv"
  9. )
  10. const (
  11. _sugSeason = "SUG:SEASON:%s"
  12. _sugItem = "SUGITEM:%d"
  13. _expire = 93600
  14. )
  15. // SetItem set item info to redis
  16. func (d *Dao) SetItem(c context.Context, item *model.Item) (b bool, err error) {
  17. var location string
  18. picItem := amodel.Item{ItemsID: item.ID, Name: item.Name, Brief: item.Brief, Img: item.HeadImg}
  19. if location, _ = d.CreateItemPNG(picItem); location == "" {
  20. return
  21. }
  22. item.SugImg = location
  23. conn := d.redis.Get(c)
  24. defer conn.Close()
  25. if err = conn.Send("HSET", fmt.Sprintf(_sugItem, item.ID), "items_id", item.ID); err != nil {
  26. return
  27. }
  28. if err = conn.Send("HSET", fmt.Sprintf(_sugItem, item.ID), "name", item.Name); err != nil {
  29. return
  30. }
  31. if err = conn.Send("HSET", fmt.Sprintf(_sugItem, item.ID), "brief", item.Brief); err != nil {
  32. return
  33. }
  34. if err = conn.Send("HSET", fmt.Sprintf(_sugItem, item.ID), "head", item.HeadImg); err != nil {
  35. return
  36. }
  37. if err = conn.Send("HSET", fmt.Sprintf(_sugItem, item.ID), "pic", item.SugImg); err != nil {
  38. return
  39. }
  40. if err = conn.Send("EXPIRE", fmt.Sprintf(_sugItem, item.ID), _expire); err != nil {
  41. return
  42. }
  43. if err = conn.Flush(); err != nil {
  44. return
  45. }
  46. if b, err = redis.Bool(conn.Receive()); err != nil {
  47. return
  48. }
  49. conn.Receive()
  50. return
  51. }
  52. // SetSug ...
  53. func (d *Dao) SetSug(c context.Context, seasonID string, ItemID int64, score float64) (b bool, err error) {
  54. conn := d.redis.Get(c)
  55. defer conn.Close()
  56. if err = conn.Send("ZADD", fmt.Sprintf(_sugSeason, seasonID), score, ItemID); err != nil {
  57. return
  58. }
  59. if err = conn.Send("EXPIRE", fmt.Sprintf(_sugSeason, seasonID), _expire); err != nil {
  60. return
  61. }
  62. if err = conn.Flush(); err != nil {
  63. return
  64. }
  65. if b, err = redis.Bool(conn.Receive()); err != nil {
  66. return
  67. }
  68. conn.Receive()
  69. return
  70. }
  71. // DelSug ...
  72. func (d *Dao) DelSug(c context.Context, seasonID string) (b bool, err error) {
  73. conn := d.redis.Get(c)
  74. defer conn.Close()
  75. if err = conn.Send("DEL", fmt.Sprintf(_sugSeason, seasonID)); err != nil {
  76. return
  77. }
  78. if err = conn.Flush(); err != nil {
  79. return
  80. }
  81. if b, err = redis.Bool(conn.Receive()); err != nil {
  82. return
  83. }
  84. conn.Receive()
  85. return
  86. }
  87. // DelSugItem ...
  88. func (d *Dao) DelSugItem(c context.Context, seasonID, itemsID int64) (b bool, err error) {
  89. conn := d.redis.Get(c)
  90. defer conn.Close()
  91. if err = conn.Send("ZREM", fmt.Sprintf(_sugSeason, strconv.FormatInt(seasonID, 10)), itemsID); err != nil {
  92. return
  93. }
  94. if err = conn.Flush(); err != nil {
  95. return
  96. }
  97. if b, err = redis.Bool(conn.Receive()); err != nil {
  98. return
  99. }
  100. conn.Receive()
  101. return
  102. }