package speechNlu //var redisClient *redis.Client // //// 初始化 Redis 连接 //func InitRedis(addr, password string, db int) { // redisClient = redis.NewClient(&redis.Options{ // Addr: addr, // Password: password, // DB: db, // }) // // if err := redisClient.Ping(context.Background()).Err(); err != nil { // log.Fatalf("Redis连接失败: %v", err) // } //} // //// 更新会话(首次创建或续期) //func UpdateSession(mac, sessionId string, expiration time.Duration) { // ctx := context.Background() // key := getSessionKey(mac) // // // 直接设置键值并指定过期时间 // err := redisClient.Set(ctx, key, sessionId, expiration).Err() // if err != nil { // log.Printf("会话更新失败 (MAC: %s): %v", mac, err) // } //} // //// 获取会话并自动续期 //func GetAndRefreshSession(mac string, expiration time.Duration) (string, bool) { // ctx := context.Background() // key := getSessionKey(mac) // // // 使用 Lua 脚本保证原子性:获取值 + 刷新过期时间 // script := redis.NewScript(` // local key = KEYS[1] // local expire = tonumber(ARGV[1]) // // local value = redis.call("GET", key) // if not value then // return nil // end // // redis.call("EXPIRE", key, expire) // return value // `) // // result, err := script.Run(ctx, redisClient, []string{key}, expiration.Seconds()).Result() // if err != nil { // if err == redis.Nil { // return "", false // } // log.Printf("会话获取失败 (MAC: %s): %v", mac, err) // return "", false // } // // return result.(string), true //} // //func getSessionKey(mac string) string { // return fmt.Sprintf("session:%s", mac) //}