You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					41 lines
				
				1.0 KiB
			
		
		
			
		
	
	
					41 lines
				
				1.0 KiB
			| 
								 
											2 weeks ago
										 
									 | 
							
								// 文件: storage/redis.go
							 | 
						||
| 
								 | 
							
								package storage
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								import (
							 | 
						||
| 
								 | 
							
									"context"
							 | 
						||
| 
								 | 
							
									"fmt"
							 | 
						||
| 
								 | 
							
									"log"
							 | 
						||
| 
								 | 
							
									"memobus_relay_server/config"
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									"github.com/redis/go-redis/v9"
							 | 
						||
| 
								 | 
							
								)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								// 全局 Redis 客户端实例
							 | 
						||
| 
								 | 
							
								var RedisClient *redis.Client
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								// InitRedis 初始化全局的 Redis 客户端连接
							 | 
						||
| 
								 | 
							
								// 这个函数现在是唯一的 Redis 初始化入口
							 | 
						||
| 
								 | 
							
								func InitRedis() error {
							 | 
						||
| 
								 | 
							
									// 如果配置中未启用 Redis,则不进行任何操作
							 | 
						||
| 
								 | 
							
									if !config.Cfg.Redis.Enabled {
							 | 
						||
| 
								 | 
							
										log.Println("Redis is disabled in config. Skipping initialization.")
							 | 
						||
| 
								 | 
							
										return nil
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									// 创建一个新的 Redis 客户端实例
							 | 
						||
| 
								 | 
							
									RedisClient = redis.NewClient(&redis.Options{
							 | 
						||
| 
								 | 
							
										Addr:     config.Cfg.Redis.Addr,
							 | 
						||
| 
								 | 
							
										Password: config.Cfg.Redis.Password,
							 | 
						||
| 
								 | 
							
										DB:       config.Cfg.Redis.DB,
							 | 
						||
| 
								 | 
							
									})
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									// 测试连接,确保 Redis 服务可用
							 | 
						||
| 
								 | 
							
									if err := RedisClient.Ping(context.Background()).Err(); err != nil {
							 | 
						||
| 
								 | 
							
										// 将错误包装后返回,让 main 函数决定如何处理
							 | 
						||
| 
								 | 
							
										return fmt.Errorf("failed to connect to Redis: %w", err)
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									log.Println("Successfully connected to Redis.")
							 | 
						||
| 
								 | 
							
									return nil
							 | 
						||
| 
								 | 
							
								}
							 |