key.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package cache
  2. import (
  3. "time"
  4. )
  5. func (p *RedisCache) Keys(key string) ([]string, error) {
  6. if p.isCluster {
  7. return p.cluster.Keys(key).Result()
  8. }
  9. return p.client.Keys(key).Result()
  10. }
  11. func (p *RedisCache) Expire(key string, seconds int64) (bool, error) {
  12. if p.isCluster {
  13. return p.cluster.Expire(key, time.Duration(seconds)*time.Second).Result()
  14. }
  15. return p.client.Expire(key, time.Duration(seconds)*time.Second).Result()
  16. }
  17. func (p *RedisCache) Exists(keys ...string) (int64, error) {
  18. if p.isCluster {
  19. n := int64(0)
  20. for _, key := range keys {
  21. v, err := p.cluster.Exists(key).Result()
  22. if err != nil {
  23. return n, err
  24. }
  25. n += v
  26. }
  27. return n, nil
  28. }
  29. return p.client.Exists(keys...).Result()
  30. }
  31. func (p *RedisCache) Del(keys ...string) (int64, error) {
  32. if p.isCluster {
  33. n := int64(0)
  34. for _, key := range keys {
  35. v, err := p.cluster.Del(key).Result()
  36. if err != nil {
  37. return n, err
  38. }
  39. n += v
  40. }
  41. return n, nil
  42. }
  43. return p.client.Del(keys...).Result()
  44. }
  45. func (p *RedisCache) IncrBy(key string, value int64) (int64, error) {
  46. if p.isCluster {
  47. return p.cluster.IncrBy(key, value).Result()
  48. }
  49. return p.client.IncrBy(key, value).Result()
  50. }
  51. func (p *RedisCache) Rename(key string, newkey string) (string, error) {
  52. if p.isCluster {
  53. return p.cluster.Rename(key, newkey).Result()
  54. }
  55. return p.client.Rename(key, newkey).Result()
  56. }