Keine Beschreibung
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

redis_factory.go 2.4KB

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