redis.go 996 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package consumer
  2. import (
  3. "context"
  4. "go-common/library/log"
  5. )
  6. //实时消费缓存设计,异步落地
  7. const VALUE = "value"
  8. const DATE = "date" //是否最新消息,是为1(需要刷新到DB) 否为0(不需要刷新到DB)
  9. const DATE_1 = "1"
  10. //Set 设置实时数据
  11. func (d *Dao) Set(ctx context.Context, redisKey string, value string, timeOut int) (err error) {
  12. conn := d.redis.Get(ctx)
  13. defer conn.Close()
  14. if _, err = conn.Do("HMSET", redisKey, VALUE, value, DATE, DATE_1); err != nil {
  15. log.Error("consumer_set_err:key=%s;err=%v", redisKey, err)
  16. return
  17. }
  18. conn.Do("EXPIRE", redisKey, timeOut)
  19. return
  20. }
  21. //Incr 设置增加数据
  22. func (d *Dao) Incr(ctx context.Context, redisKey string, num int64, timeOut int) (err error) {
  23. conn := d.redis.Get(ctx)
  24. defer conn.Close()
  25. if _, err = conn.Do("HINCRBY", redisKey, VALUE, num, DATE, DATE_1); err != nil {
  26. log.Error("consumer_incr_err:key=%s;err=%v", redisKey, err)
  27. return
  28. }
  29. conn.Do("EXPIRE", redisKey, timeOut)
  30. return
  31. }