redis.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/service/main/ugcpay/conf"
  6. "go-common/library/cache/redis"
  7. "github.com/pkg/errors"
  8. )
  9. func assetRelationKey(mid int64) string {
  10. return fmt.Sprintf("up_ar_%d", mid)
  11. }
  12. func assetRelationField(oid int64, otype string) string {
  13. return fmt.Sprintf("%s_%d", otype, oid)
  14. }
  15. func (d *Dao) pingRedis(c context.Context) (err error) {
  16. conn := d.redis.Get(c)
  17. defer conn.Close()
  18. if _, err = conn.Do("SET", "PING", "PONG"); err != nil {
  19. err = errors.WithStack(err)
  20. }
  21. return
  22. }
  23. // CacheAssetRelationState get asset relation state.
  24. func (d *Dao) CacheAssetRelationState(c context.Context, oid int64, otype string, mid int64) (state string, err error) {
  25. var (
  26. key = assetRelationKey(mid)
  27. field = assetRelationField(oid, otype)
  28. conn = d.redis.Get(c)
  29. )
  30. defer conn.Close()
  31. if state, err = redis.String(conn.Do("HGET", key, field)); err != nil {
  32. if err == redis.ErrNil {
  33. err = nil
  34. state = "miss"
  35. return
  36. }
  37. err = errors.Wrapf(err, "conn.Do(HGET, %s ,%s)", key, field)
  38. return
  39. }
  40. return
  41. }
  42. // AddCacheAssetRelationState set asset relation state.
  43. func (d *Dao) AddCacheAssetRelationState(c context.Context, oid int64, otype string, mid int64, state string) (err error) {
  44. var (
  45. key = assetRelationKey(mid)
  46. field = assetRelationField(oid, otype)
  47. conn = d.redis.Get(c)
  48. )
  49. defer conn.Close()
  50. if _, err = conn.Do("HSET", key, field, state); err != nil {
  51. err = errors.Wrapf(err, "conn.Do(HSET, %s, %s, %s)", key, field, state)
  52. return
  53. }
  54. if _, err = conn.Do("EXPIRE", key, conf.Conf.CacheTTL.AssetRelationTTL); err != nil {
  55. err = errors.Wrapf(err, "conn.Do(EXPIRE, %s, %d)", key, conf.Conf.CacheTTL.AssetRelationTTL)
  56. return
  57. }
  58. return
  59. }
  60. // DelCacheAssetRelationState delete asset relation state.
  61. func (d *Dao) DelCacheAssetRelationState(c context.Context, oid int64, otype string, mid int64) (err error) {
  62. var (
  63. key = assetRelationKey(mid)
  64. field = assetRelationField(oid, otype)
  65. conn = d.redis.Get(c)
  66. )
  67. defer conn.Close()
  68. if _, err = conn.Do("HDEL", key, field); err != nil {
  69. err = errors.Wrapf(err, "conn.Do(HDEL, %s, %s)", key, field)
  70. return
  71. }
  72. return
  73. }
  74. // DelCacheAssetRelation delete assetrelation.
  75. func (d *Dao) DelCacheAssetRelation(c context.Context, mid int64) (err error) {
  76. var (
  77. key = assetRelationKey(mid)
  78. conn = d.redis.Get(c)
  79. )
  80. defer conn.Close()
  81. if _, err = conn.Do("DEL", key); err != nil {
  82. err = errors.Wrapf(err, "conn.Do(DEL, %s)", key)
  83. return
  84. }
  85. return
  86. }