redis_read.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/library/cache/redis"
  6. "go-common/library/log"
  7. )
  8. // GetsetReadPing 设置并获取上次阅读心跳时间,不存在则返回0
  9. func (d *Dao) GetsetReadPing(c context.Context, buvid string, aid int64, cur int64) (last int64, err error) {
  10. var (
  11. key = readPingKey(buvid, aid)
  12. conn = d.redis.Get(c)
  13. )
  14. defer conn.Close()
  15. if last, err = redis.Int64(conn.Do("GETSET", key, cur)); err != nil && err != redis.ErrNil {
  16. log.Error("conn.Do(GETSET, %s, %d) error(%+v)", key, cur, err)
  17. return
  18. }
  19. if _, err = conn.Do("EXPIRE", key, d.redisReadPingExpire); err != nil {
  20. log.Error("conn.Do(EXPIRE, %s, %d) error(%+v)", key, cur, err)
  21. return
  22. }
  23. return
  24. }
  25. // AddReadPingSet 添加新的阅读记录
  26. func (d *Dao) AddReadPingSet(c context.Context, buvid string, aid int64, mid int64, ip string, cur int64, source string) (err error) {
  27. var (
  28. key = readPingSetKey()
  29. value = fmt.Sprintf("%s|%d|%d|%s|%d|%s", buvid, aid, mid, ip, cur, source)
  30. conn = d.redis.Get(c)
  31. )
  32. defer conn.Close()
  33. if _, err = conn.Do("SADD", key, value); err != nil {
  34. log.Error("conn.Do(SADD, %s, %s) error(%+v)", key, value, err)
  35. return
  36. }
  37. if _, err = conn.Do("EXPIRE", key, d.redisReadSetExpire); err != nil {
  38. log.Error("conn.Do(EXPIRE, %s, %d) error(%+v)", key, cur, err)
  39. return
  40. }
  41. return
  42. }