refactor: split driver cache

This commit is contained in:
lilong.129
2025-05-26 00:35:56 +08:00
parent 2e17d9df16
commit a888022cbc
3 changed files with 599 additions and 594 deletions

View File

@@ -1 +1 @@
v5.0.0-beta-2505252353
v5.0.0-beta-2505260035

37
uixt/cache.go Normal file
View File

@@ -0,0 +1,37 @@
package uixt
import (
"context"
"sync"
"github.com/rs/zerolog/log"
)
var driverCache sync.Map // key is serial, value is *XTDriver
// setupXTDriver initializes an XTDriver based on the platform and serial.
func setupXTDriver(_ context.Context, args map[string]any) (*XTDriver, error) {
platform, _ := args["platform"].(string)
serial, _ := args["serial"].(string)
if platform == "" {
log.Warn().Msg("platform is not set, using android as default")
platform = "android"
}
// Check if driver exists in cache
cacheKey := serial
if cachedDriver, ok := driverCache.Load(cacheKey); ok {
if driverExt, ok := cachedDriver.(*XTDriver); ok {
log.Info().Str("platform", platform).Str("serial", serial).Msg("Using cached driver")
return driverExt, nil
}
}
driverExt, err := NewDriverExt(platform, serial)
if err != nil {
return nil, err
}
// store driver in cache
driverCache.Store(cacheKey, driverExt)
return driverExt, nil
}

File diff suppressed because it is too large Load Diff