monitor.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package redis
  2. import (
  3. "context"
  4. "go-common/library/log"
  5. "time"
  6. )
  7. // AddMonitorStats add stay stats
  8. func (d *Dao) AddMonitorStats(c context.Context, key string, oid int64) (err error) {
  9. var (
  10. conn = d.secondary.Get(c)
  11. now = time.Now().Unix()
  12. age = 7 * 24 * 60 * 60
  13. )
  14. defer conn.Close()
  15. if _, err = conn.Do("ZADD", key, now, oid); err != nil {
  16. log.Error("conn.Do(ZADD, %s, %d, %d) error(%v)", key, now, oid, err)
  17. return
  18. }
  19. if _, err = conn.Do("EXPIRE", key, age); err != nil {
  20. log.Error("conn.Do(EXPIRE, %s, %d) error(%v)", key, age, err)
  21. }
  22. return
  23. }
  24. // RemMonitorStats remove stay stats
  25. func (d *Dao) RemMonitorStats(c context.Context, key string, oid int64) (err error) {
  26. var (
  27. conn = d.secondary.Get(c)
  28. )
  29. defer conn.Close()
  30. if _, err = conn.Do("ZREM", key, oid); err != nil {
  31. log.Error("conn.Do(ZREM, %s, %d) error(%v)", key, oid, err)
  32. }
  33. return
  34. }
  35. // ClearMonitorStats clear expire stats
  36. func (d *Dao) ClearMonitorStats(c context.Context, key string) (err error) {
  37. var (
  38. conn = d.secondary.Get(c)
  39. now = time.Now().Unix()
  40. min int64
  41. max = now - 7*24*60*60
  42. )
  43. defer conn.Close()
  44. if _, err = conn.Do("ZREMRANGEBYSCORE", key, min, max); err != nil {
  45. log.Error("conn.Do(ZREMRANGEBYSCORE, %s, %d, %d) error(%v)", key, min, max, err)
  46. }
  47. return
  48. }