string.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package cache
  2. import (
  3. "time"
  4. "github.com/go-redis/redis"
  5. )
  6. func (p *RedisCache) Get(key string) (string, error) {
  7. if p.isCluster {
  8. return p.cluster.Get(key).Result()
  9. }
  10. return p.client.Get(key).Result()
  11. }
  12. func (p *RedisCache) Set(key string, value interface{}) (string, error) {
  13. if p.isCluster {
  14. return p.cluster.Set(key, value, 0).Result()
  15. }
  16. return p.client.Set(key, value, 0).Result()
  17. }
  18. func (p *RedisCache) SetEx(key string, seconds int64, value interface{}) (string, error) {
  19. if p.isCluster {
  20. return p.cluster.Set(key, value, time.Duration(seconds)*time.Second).Result()
  21. }
  22. return p.client.Set(key, value, time.Duration(seconds)*time.Second).Result()
  23. }
  24. func (p *RedisCache) MGet(keys ...string) ([]string, error) {
  25. if p.isCluster {
  26. results := make([]string, 0, len(keys))
  27. for _, key := range keys {
  28. v, err := p.cluster.Get(key).Result()
  29. if err != nil {
  30. if err != redis.Nil {
  31. return nil, err
  32. }
  33. }
  34. results = append(results, v)
  35. }
  36. if len(results) <= 0 {
  37. return nil, redis.Nil
  38. }
  39. return results, nil
  40. }
  41. args := []interface{}{"mget"}
  42. for _, key := range keys {
  43. args = append(args, key)
  44. }
  45. return p.commandReturnStringSlice(args...)
  46. }
  47. func (p *RedisCache) SetBit(key string, offset int64, value int) (int64, error) {
  48. if p.isCluster {
  49. return p.cluster.SetBit(key, offset, value).Result()
  50. }
  51. return p.client.SetBit(key, offset, value).Result()
  52. }
  53. func (p *RedisCache) GetBit(key string, offset int64) (int64, error) {
  54. if p.isCluster {
  55. return p.cluster.GetBit(key, offset).Result()
  56. }
  57. return p.client.GetBit(key, offset).Result()
  58. }
  59. func (p *RedisCache) SetNx(key string, value interface{}) (bool, error) {
  60. if p.isCluster {
  61. return p.cluster.SetNX(key, value, 0).Result()
  62. }
  63. return p.client.SetNX(key, value, 0).Result()
  64. }
  65. func (p *RedisCache) SetNxEx(key string, value interface{}, seconds int64) (bool, error) {
  66. if p.isCluster {
  67. return p.cluster.SetNX(key, value, time.Duration(seconds)*time.Second).Result()
  68. }
  69. return p.client.SetNX(key, value, time.Duration(seconds)*time.Second).Result()
  70. }
  71. func (p *RedisCache) Append(key, value string) (int64, error) {
  72. if p.isCluster {
  73. return p.cluster.Append(key, value).Result()
  74. }
  75. return p.client.Append(key, value).Result()
  76. }