redis.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package store
  2. import (
  3. "encoding/hex"
  4. "time"
  5. "github.com/go-redis/redis"
  6. )
  7. // NewRedisStore create an instance of a redis store
  8. func NewRedisStore(opts *redis.Options, expiration time.Duration, out Logger, prefix ...string) Store {
  9. if opts == nil {
  10. panic("options cannot be nil")
  11. }
  12. return NewRedisStoreWithCli(
  13. redis.NewClient(opts),
  14. expiration,
  15. out,
  16. prefix...,
  17. )
  18. }
  19. // NewRedisStoreWithCli create an instance of a redis store
  20. func NewRedisStoreWithCli(cli *redis.Client, expiration time.Duration, out Logger, prefix ...string) Store {
  21. store := &redisStore{
  22. cli: cli,
  23. expiration: expiration,
  24. out: out,
  25. }
  26. if len(prefix) > 0 {
  27. store.prefix = prefix[0]
  28. }
  29. return store
  30. }
  31. // NewRedisClusterStore create an instance of a redis cluster store
  32. func NewRedisClusterStore(opts *redis.ClusterOptions, expiration time.Duration, out Logger, prefix ...string) Store {
  33. if opts == nil {
  34. panic("options cannot be nil")
  35. }
  36. return NewRedisClusterStoreWithCli(
  37. redis.NewClusterClient(opts),
  38. expiration,
  39. out,
  40. prefix...,
  41. )
  42. }
  43. // NewRedisClusterStoreWithCli create an instance of a redis cluster store
  44. func NewRedisClusterStoreWithCli(cli *redis.ClusterClient, expiration time.Duration, out Logger, prefix ...string) Store {
  45. store := &redisStore{
  46. cli: cli,
  47. expiration: expiration,
  48. out: out,
  49. }
  50. if len(prefix) > 0 {
  51. store.prefix = prefix[0]
  52. }
  53. return store
  54. }
  55. type clienter interface {
  56. Get(key string) *redis.StringCmd
  57. Set(key string, value interface{}, expiration time.Duration) *redis.StatusCmd
  58. Del(keys ...string) *redis.IntCmd
  59. }
  60. type redisStore struct {
  61. cli clienter
  62. prefix string
  63. out Logger
  64. expiration time.Duration
  65. }
  66. func (s *redisStore) getKey(id string) string {
  67. return s.prefix + id
  68. }
  69. func (s *redisStore) printf(format string, args ...interface{}) {
  70. if s.out != nil {
  71. s.out.Printf(format, args...)
  72. }
  73. }
  74. func (s *redisStore) Set(id string, digits []byte) {
  75. cmd := s.cli.Set(s.getKey(id), hex.EncodeToString(digits), s.expiration)
  76. if err := cmd.Err(); err != nil {
  77. s.printf("redis execution set command error: %s", err.Error())
  78. }
  79. return
  80. }
  81. func (s *redisStore) Get(id string, clear bool) []byte {
  82. key := s.getKey(id)
  83. cmd := s.cli.Get(key)
  84. if err := cmd.Err(); err != nil {
  85. if err == redis.Nil {
  86. return nil
  87. }
  88. s.printf("redis execution get command error: %s", err.Error())
  89. return nil
  90. }
  91. b, err := hex.DecodeString(cmd.Val())
  92. if err != nil {
  93. s.printf("hex decoding error: %s", err.Error())
  94. return nil
  95. }
  96. if clear {
  97. cmd := s.cli.Del(key)
  98. if err := cmd.Err(); err != nil {
  99. s.printf("redis execution del command error: %s", err.Error())
  100. return nil
  101. }
  102. }
  103. return b
  104. }