redis.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/job/main/thumbup/model"
  6. "go-common/library/cache/redis"
  7. "go-common/library/log"
  8. "go-common/library/xstr"
  9. )
  10. func hashStatsKey(businessID, originID int64) string {
  11. return fmt.Sprintf("stats_o_%d_b_%d", originID, businessID)
  12. }
  13. // ExpireHashStatsCache .
  14. func (d *Dao) ExpireHashStatsCache(c context.Context, businessID, originID int64) (ok bool, err error) {
  15. conn := d.redis.Get(c)
  16. defer conn.Close()
  17. key := hashStatsKey(businessID, originID)
  18. if ok, err = redis.Bool(conn.Do("EXPIRE", key, d.redisStatsExpire)); err != nil {
  19. log.Error("conn.Do(EXPIRE, %s, %d) error(%v)", key, d.redisStatsExpire, err)
  20. }
  21. return
  22. }
  23. // AddHashStatsCache .
  24. func (d *Dao) AddHashStatsCache(c context.Context, businessID, originID int64, stats ...*model.Stats) (err error) {
  25. if len(stats) == 0 {
  26. return
  27. }
  28. conn := d.redis.Get(c)
  29. defer conn.Close()
  30. key := hashStatsKey(businessID, originID)
  31. var commonds = []interface{}{key}
  32. for _, stat := range stats {
  33. commonds = append(commonds, stat.ID, xstr.JoinInts([]int64{stat.Likes, stat.Dislikes}))
  34. }
  35. if _, err = conn.Do("HMSET", commonds...); err != nil {
  36. log.Error("conn.DO(HMSET, %s, %v) error(%v)", key, commonds, err)
  37. }
  38. return
  39. }