redis.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. //
  6. "go-common/app/admin/openplatform/sug/model"
  7. "go-common/library/cache/redis"
  8. )
  9. const (
  10. _sugSeason = "SUG:SEASON:%d"
  11. _sugItem = "SUGITEM:%d"
  12. _expire = 93600
  13. )
  14. // SetSug set redis sug
  15. func (d *Dao) SetSug(c context.Context, seasonID int64, ItemID int64, score int64) (err error) {
  16. conn := d.redis.Get(c)
  17. defer conn.Close()
  18. key := fmt.Sprintf(_sugSeason, seasonID)
  19. if err = conn.Send("ZADD", key, score, ItemID); err != nil {
  20. return
  21. }
  22. if err = conn.Send("EXPIRE", key, _expire); err != nil {
  23. return
  24. }
  25. if err = conn.Flush(); err != nil {
  26. return
  27. }
  28. return
  29. }
  30. // DelSug del redis sug
  31. func (d *Dao) DelSug(c context.Context, seasonID, itemsID int64) (err error) {
  32. conn := d.redis.Get(c)
  33. defer conn.Close()
  34. if _, err = conn.Do("ZREM", fmt.Sprintf(_sugSeason, seasonID), itemsID); err != nil {
  35. return
  36. }
  37. return
  38. }
  39. // SetItem set redis item
  40. func (d *Dao) SetItem(c context.Context, item *model.Item, location string) (b bool, err error) {
  41. conn := d.redis.Get(c)
  42. defer conn.Close()
  43. if err = conn.Send("HSET", fmt.Sprintf(_sugItem, item.ItemsID), "items_id", item.ItemsID); err != nil {
  44. return
  45. }
  46. if err = conn.Send("HSET", fmt.Sprintf(_sugItem, item.ItemsID), "name", item.Name); err != nil {
  47. return
  48. }
  49. if err = conn.Send("HSET", fmt.Sprintf(_sugItem, item.ItemsID), "brief", item.Brief); err != nil {
  50. return
  51. }
  52. if err = conn.Send("HSET", fmt.Sprintf(_sugItem, item.ItemsID), "head", item.Img); err != nil {
  53. return
  54. }
  55. if err = conn.Send("HSET", fmt.Sprintf(_sugItem, item.ItemsID), "pic", location); err != nil {
  56. return
  57. }
  58. if err = conn.Send("EXPIRE", fmt.Sprintf(_sugItem, item.ItemsID), _expire); err != nil {
  59. return
  60. }
  61. if err = conn.Flush(); err != nil {
  62. return
  63. }
  64. if b, err = redis.Bool(conn.Receive()); err != nil {
  65. return
  66. }
  67. conn.Receive()
  68. return
  69. }