Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package redis
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "git.x2erp.com/qdy/go-base/config"
  7. "git.x2erp.com/qdy/go-base/config/subconfigs"
  8. "github.com/go-redis/redis/v8"
  9. )
  10. var ctxRedis = context.Background()
  11. // RedisFactory Redis客户端工厂
  12. type RedisFactory struct {
  13. redisConfig *subconfigs.RedisConfig
  14. }
  15. // NewRedisFactory 创建Redis客户端工厂
  16. func NewRedisFactory() (*RedisFactory, error) {
  17. cfg := config.GetRedisConfig()
  18. // 可以添加配置验证
  19. if cfg == nil || cfg.Host == "" {
  20. return nil, fmt.Errorf("redis configuration is incomplete")
  21. }
  22. return &RedisFactory{redisConfig: cfg}, nil
  23. }
  24. // CreateRedisClient 创建Redis客户端
  25. func (f *RedisFactory) CreateRedisClient() *RedisClient {
  26. redisConfig := f.redisConfig
  27. client := redis.NewClient(&redis.Options{
  28. Addr: fmt.Sprintf("%s:%d", redisConfig.Host, redisConfig.Port),
  29. Password: redisConfig.Password,
  30. DB: redisConfig.DB,
  31. PoolSize: redisConfig.PoolSize,
  32. })
  33. return &RedisClient{
  34. client: client,
  35. }
  36. }
  37. // RedisClient Redis客户端
  38. type RedisClient struct {
  39. client *redis.Client
  40. }
  41. // Set 写入字符串值
  42. func (c *RedisClient) Set(key string, value interface{}, expiration time.Duration) error {
  43. return c.client.Set(ctxRedis, key, value, expiration).Err()
  44. }
  45. // Get 读取字符串值
  46. func (c *RedisClient) Get(key string) (string, error) {
  47. return c.client.Get(ctxRedis, key).Result()
  48. }
  49. // HSet 写入哈希字段
  50. func (c *RedisClient) HSet(key string, values ...interface{}) error {
  51. return c.client.HSet(ctxRedis, key, values...).Err()
  52. }
  53. // HGet 读取哈希字段
  54. func (c *RedisClient) HGet(key, field string) (string, error) {
  55. return c.client.HGet(ctxRedis, key, field).Result()
  56. }
  57. // HGetAll 读取整个哈希
  58. func (c *RedisClient) HGetAll(key string) (map[string]string, error) {
  59. return c.client.HGetAll(ctxRedis, key).Result()
  60. }
  61. // LPush 列表左推入
  62. func (c *RedisClient) LPush(key string, values ...interface{}) error {
  63. return c.client.LPush(ctxRedis, key, values...).Err()
  64. }
  65. // RPop 列表右弹出
  66. func (c *RedisClient) RPop(key string) (string, error) {
  67. return c.client.RPop(ctxRedis, key).Result()
  68. }
  69. // Del 删除键
  70. func (c *RedisClient) Del(keys ...string) error {
  71. return c.client.Del(ctxRedis, keys...).Err()
  72. }
  73. // Exists 检查键是否存在
  74. func (c *RedisClient) Exists(keys ...string) (int64, error) {
  75. return c.client.Exists(ctxRedis, keys...).Result()
  76. }
  77. // Close 关闭连接
  78. func (c *RedisClient) Close() error {
  79. return c.client.Close()
  80. }