redis.go 879 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package recommend
  2. import (
  3. "context"
  4. "strconv"
  5. "go-common/library/cache/redis"
  6. "github.com/pkg/errors"
  7. )
  8. const (
  9. _prefixPos = "p_"
  10. )
  11. func keyPos(mid int64) string {
  12. return _prefixPos + strconv.FormatInt(mid%100000, 10)
  13. }
  14. func (d *Dao) PositionCache(c context.Context, mid int64) (pos int, err error) {
  15. var (
  16. key = keyPos(mid)
  17. conn = d.redis.Get(c)
  18. )
  19. defer conn.Close()
  20. if pos, err = redis.Int(conn.Do("HGET", key, mid)); err != nil {
  21. if err == redis.ErrNil {
  22. err = nil
  23. return
  24. }
  25. err = errors.Wrapf(err, "conn.Do(HGET,%s,%d)", key, mid)
  26. }
  27. return
  28. }
  29. func (d *Dao) AddPositionCache(c context.Context, mid int64, pos int) (err error) {
  30. var (
  31. key = keyPos(mid)
  32. conn = d.redis.Get(c)
  33. )
  34. defer conn.Close()
  35. if _, err = conn.Do("HSET", key, mid, pos); err != nil {
  36. err = errors.Wrapf(err, "conn.Do(HSET,%s,%d,%d)", key, mid, pos)
  37. }
  38. return
  39. }