@@ -6,13 +6,15 @@ import (
66 "math"
77 "strconv"
88 "strings"
9+ "time"
910
1011 "github.com/device-management-toolkit/go-wsman-messages/v2/pkg/wsman/amt/boot"
1112 cimBoot "github.com/device-management-toolkit/go-wsman-messages/v2/pkg/wsman/cim/boot"
1213 "github.com/device-management-toolkit/go-wsman-messages/v2/pkg/wsman/cim/power"
1314 "github.com/device-management-toolkit/go-wsman-messages/v2/pkg/wsman/cim/software"
1415 ipsPower "github.com/device-management-toolkit/go-wsman-messages/v2/pkg/wsman/ips/power"
1516
17+ "github.com/device-management-toolkit/console/internal/cache"
1618 "github.com/device-management-toolkit/console/internal/entity/dto/v1"
1719 "github.com/device-management-toolkit/console/internal/usecase/devices/wsman"
1820 "github.com/device-management-toolkit/console/pkg/consoleerrors"
@@ -63,6 +65,9 @@ func (uc *UseCase) SendPowerAction(c context.Context, guid string, action int) (
6365 return power.PowerActionResponse {}, err
6466 }
6567
68+ // Invalidate power state cache after action
69+ uc .cache .Delete (cache .MakePowerStateKey (guid ))
70+
6671 return response , nil
6772 }
6873
@@ -78,6 +83,9 @@ func (uc *UseCase) SendPowerAction(c context.Context, guid string, action int) (
7883 return power.PowerActionResponse {}, err
7984 }
8085
86+ // Invalidate power state cache after action
87+ uc .cache .Delete (cache .MakePowerStateKey (guid ))
88+
8189 return response , nil
8290}
8391
@@ -121,6 +129,17 @@ func ensureFullPowerBeforeReset(device wsman.Management) (power.PowerActionRespo
121129}
122130
123131func (uc * UseCase ) GetPowerState (c context.Context , guid string ) (dto.PowerState , error ) {
132+ // Check cache first - use short TTL since power state changes frequently
133+ cacheKey := cache .MakePowerStateKey (guid )
134+ if cached , found := uc .cache .Get (cacheKey ); found {
135+ if state , ok := cached .(dto.PowerState ); ok {
136+ uc .log .Info ("Cache hit for power state" , "guid" , guid )
137+ return state , nil
138+ }
139+ }
140+
141+ uc .log .Info ("Cache miss for power state, fetching from AMT" , "guid" , guid )
142+
124143 item , err := uc .repo .GetByID (c , guid , "" )
125144 if err != nil {
126145 return dto.PowerState {}, err
@@ -142,16 +161,25 @@ func (uc *UseCase) GetPowerState(c context.Context, guid string) (dto.PowerState
142161
143162 stateOS , err := device .GetOSPowerSavingState ()
144163 if err != nil {
145- return dto.PowerState {
164+ powerState := dto.PowerState {
146165 PowerState : int (state [0 ].PowerState ),
147166 OSPowerSavingState : 0 , // UNKNOWN
148- }, err
167+ }
168+ // Still cache partial result
169+ uc .cache .Set (cacheKey , powerState , cache .PowerStateTTL )
170+
171+ return powerState , err
149172 }
150173
151- return dto.PowerState {
174+ powerState := dto.PowerState {
152175 PowerState : int (state [0 ].PowerState ),
153176 OSPowerSavingState : int (stateOS ),
154- }, nil
177+ }
178+
179+ // Cache power state
180+ uc .cache .Set (cacheKey , powerState , cache .PowerStateTTL )
181+
182+ return powerState , nil
155183}
156184
157185func (uc * UseCase ) GetPowerCapabilities (c context.Context , guid string ) (dto.PowerCapabilities , error ) {
0 commit comments