redis.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/library/cache/redis"
  6. )
  7. const (
  8. _keyVersionID = "abtest:versionid:%d"
  9. )
  10. // PingRedis check redis connection
  11. func (d *Dao) PingRedis(c context.Context) (err error) {
  12. conn := d.redis.Get(c)
  13. defer conn.Close()
  14. _, err = conn.Do("PING")
  15. return
  16. }
  17. //RedisVersionID 获取redis中的分组版本
  18. func (d *Dao) RedisVersionID(c context.Context, group int) (ver int64, err error) {
  19. conn := d.redis.Get(c)
  20. defer conn.Close()
  21. ver, err = redis.Int64(conn.Do("GET", fmt.Sprintf(_keyVersionID, group)))
  22. return
  23. }
  24. //SetnxRedisVersionID 使用v设置redis中的版本号
  25. func (d *Dao) SetnxRedisVersionID(c context.Context, group int, v int64) (err error) {
  26. conn := d.redis.Get(c)
  27. defer conn.Close()
  28. _, err = conn.Do("SETNX", fmt.Sprintf(_keyVersionID, group), v)
  29. return
  30. }
  31. //UpdateRedisVersionID 使用v更新redis中的分组版本
  32. func (d *Dao) UpdateRedisVersionID(c context.Context, group int, v int64) (err error) {
  33. conn := d.redis.Get(c)
  34. defer conn.Close()
  35. _, err = conn.Do("SETEX", fmt.Sprintf(_keyVersionID, group), d.verifyExpire, v)
  36. return
  37. }