redis.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package videoshot
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/library/cache/redis"
  6. "go-common/library/log"
  7. )
  8. const (
  9. // _hashKeyNum the max count of hash key
  10. _hashKeyNum = int64(100000)
  11. _keyPrefix = "vs_"
  12. )
  13. func hashKey(cid int64) string {
  14. return fmt.Sprintf("%s%d", _keyPrefix, cid%_hashKeyNum)
  15. }
  16. // cache get videoshot's count by id.
  17. func (d *Dao) cache(c context.Context, cid int64) (count, ver int, err error) {
  18. var (
  19. key = hashKey(cid)
  20. conn = d.rds.Get(c)
  21. out int64
  22. )
  23. defer conn.Close()
  24. if out, err = redis.Int64(conn.Do("HGET", key, cid)); err != nil {
  25. if err == redis.ErrNil {
  26. err = nil
  27. } else {
  28. log.Error("HGET(%s, %d) error(%v)", key, cid, err)
  29. }
  30. return
  31. }
  32. ver = int(out >> 32)
  33. count = int(int32(out))
  34. return
  35. }
  36. // addCache set videoshot info into redis.
  37. func (d *Dao) addCache(c context.Context, cid int64, ver, count int) (err error) {
  38. var (
  39. key = hashKey(cid)
  40. conn = d.rds.Get(c)
  41. in int64
  42. )
  43. in = int64(ver)<<32 | int64(count)
  44. defer conn.Close()
  45. if _, err = conn.Do("HSET", key, cid, in); err != nil {
  46. log.Error("HSET(%s, %d, %d)", key, cid, count, err)
  47. }
  48. return
  49. }