Skip to content

Commit 891643b

Browse files
committed
cache
1 parent 9a39537 commit 891643b

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

packages/core/src/codewhisperer/region/regionProfileManager.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export type ProfileSwitchIntent = 'user' | 'auth' | 'update' | 'reload'
5454
type CachedApiResult = {
5555
state: 'pending' | 'valid' | 'stale'
5656
profiles: RegionProfile[] | undefined
57+
timestamp: number
5758
}
5859

5960
export class RegionProfileManager {
@@ -110,21 +111,27 @@ export class RegionProfileManager {
110111

111112
constructor(private readonly connectionProvider: () => Connection | undefined) {}
112113

113-
async listRegionProfile(loadCache: boolean = false): Promise<RegionProfile[]> {
114+
async listRegionProfile(): Promise<RegionProfile[]> {
114115
this._profiles = []
115116

116117
const conn = this.connectionProvider()
117118
if (conn === undefined || !isSsoConnection(conn)) {
118119
return []
119120
}
120121
const availableProfiles: RegionProfile[] = []
121-
if (loadCache) {
122-
const cachedValue = this.loadApiResultFromCache()
123-
if (cachedValue.state === 'pending') {
124-
waitUntil(async function () {})
125-
} else if (cachedValue.state === 'valid') {
126-
return cachedValue.profiles as RegionProfile[]
122+
const cachedValue = this.loadApiResultFromCache()
123+
if (cachedValue.state === 'pending') {
124+
const r = await waitUntil(
125+
async () => {
126+
return this.loadApiResultFromCache()
127+
},
128+
{ timeout: 15000, interval: 1000, truthy: false }
129+
)
130+
if (r && r.state === 'valid') {
131+
return r.profiles as RegionProfile[]
127132
}
133+
} else if (cachedValue.state === 'valid') {
134+
return cachedValue.profiles as RegionProfile[]
128135
}
129136

130137
await this.cacheApiResult(undefined)
@@ -317,6 +324,7 @@ export class RegionProfileManager {
317324
const pojo: CachedApiResult = {
318325
state: state,
319326
profiles: r,
327+
timestamp: state === 'valid' ? globals.clock.Date.now() : 0,
320328
}
321329
await globals.globalState.update('aws.amazonq.regionProfiles.cachedResult', pojo)
322330
}
@@ -326,9 +334,17 @@ export class RegionProfileManager {
326334
const cachedValue = globals.globalState.tryGet<CachedApiResult>(
327335
'aws.amazonq.regionProfiles.cachedResult',
328336
Object,
329-
{ state: 'stale', profiles: undefined }
337+
{ state: 'stale', profiles: undefined, timestamp: 0 }
330338
)
331339

340+
const now = globals.clock.Date.now()
341+
// Only use cache if the duration has not exceeded 60s
342+
if (now - cachedValue.timestamp > 60000) {
343+
const s: CachedApiResult = { state: 'stale', profiles: undefined, timestamp: 0 }
344+
globals.globalState.update('aws.amazonq.regionProfiles.cachedResult', s).catch((e) => {})
345+
return s
346+
}
347+
332348
return cachedValue
333349
}
334350

0 commit comments

Comments
 (0)