@@ -8,15 +8,22 @@ import { globalKey } from '../globalState'
88import { getLogger } from '../logger/logger'
99import { waitUntil } from '../utilities/timeoutUtils'
1010
11+ /**
12+ * result: the actual resource type callers want to use
13+ * locked: readWriteLock, while the lock is acquired by one process, the other can't access to it until it's released by the previous
14+ * timestamp: used for determining the resource is stale or not
15+ */
1116interface Resource < V > {
1217 result : V | undefined
1318 locked : boolean
1419 timestamp : number
1520}
1621
17- // GlobalStates schema, which is used for vscode global states deserialization
18- // [globals.globalState.tryGet<T>]
19- interface GlobalStateSchema < V > {
22+ /**
23+ * GlobalStates schema, which is used for vscode global states deserialization, [globals.globalState#tryGet<T>] etc.
24+ * The purpose of it is to allow devs to overload the resource into existing global key and no need to create a specific key for only this purpose.
25+ */
26+ export interface GlobalStateSchema < V > {
2027 resource : Resource < V >
2128}
2229
@@ -53,16 +60,21 @@ export abstract class CachedResource<V> {
5360
5461 // If cache is still fresh, return cached result, otherwise pull latest from the service
5562 if ( cachedValue && resource && resource . result ) {
56- if ( now ( ) - resource . timestamp < this . expirationInMilli ) {
57- logger . info ( `cache hit` )
63+ const duration = now ( ) - resource . timestamp
64+ if ( duration < this . expirationInMilli ) {
65+ logger . info ( `cache hit, duration(%sms) is less than expiration(%sms)` , duration , this . expirationInMilli )
5866 // release the lock
5967 await this . updateCache ( cachedValue , {
6068 ...resource ,
6169 locked : false ,
6270 } )
6371 return resource . result
6472 } else {
65- logger . info ( `cache hit but cached value is stale, invoking service API to pull the latest response` )
73+ logger . info (
74+ `cached value is stale, duration(%sms) is older than expiration(%sms), invoking service API to pull the latest response` ,
75+ duration ,
76+ this . expirationInMilli
77+ )
6678 }
6779 }
6880
0 commit comments