8
8
selfHostedCodecovURLStorageKey ,
9
9
selfHostedGitHubURLStorageKey ,
10
10
providers ,
11
+ cacheTtlMs ,
11
12
} from "src/constants" ;
12
13
13
14
export class Codecov {
@@ -137,10 +138,65 @@ export class Codecov {
137
138
} ;
138
139
}
139
140
141
+ async getCached (
142
+ type : "flags" | "components" ,
143
+ owner : string ,
144
+ repo : string
145
+ ) : Promise < any > {
146
+ const cacheKey = `${ owner } /${ repo } /${ type } ` ;
147
+ const cacheExpiryKey = `${ owner } /${ repo } /${ type } /expiry` ;
148
+
149
+ const storage = await browser . storage . local . get ( [ cacheKey , cacheExpiryKey ] ) ;
150
+
151
+ if ( ! storage [ cacheKey ] || ! storage [ cacheExpiryKey ] ) {
152
+ // Cache is not set
153
+ return null ;
154
+ }
155
+
156
+ const value = JSON . parse ( storage [ cacheKey ] ) ;
157
+ const expiry = storage [ cacheExpiryKey ] ;
158
+
159
+ if ( Date . now ( ) <= expiry ) {
160
+ // Cache is valid, return cached value
161
+ return value ;
162
+ }
163
+
164
+ // Cache is expired, clear cache
165
+ await browser . storage . local . remove ( [ cacheKey , cacheExpiryKey ] ) ;
166
+
167
+ return null ;
168
+ }
169
+
170
+ async setCached (
171
+ type : "flags" | "components" ,
172
+ owner : string ,
173
+ repo : string ,
174
+ data : any
175
+ ) : Promise < void > {
176
+ const cacheKey = `${ owner } /${ repo } /${ type } ` ;
177
+ const cacheExpiryKey = `${ owner } /${ repo } /${ type } /expiry` ;
178
+
179
+ await browser . storage . local . set ( {
180
+ [ cacheKey ] : JSON . stringify ( data ) ,
181
+ [ cacheExpiryKey ] : Date . now ( ) + cacheTtlMs ,
182
+ } ) ;
183
+
184
+ return ;
185
+ }
186
+
140
187
async listFlags ( payload : any , referrer : string ) : Promise < any > {
141
188
await this . init ( ) ;
142
189
const { owner, repo } = payload ;
143
190
191
+ const cachedFlags = await this . getCached ( "flags" , owner , repo ) ;
192
+
193
+ if ( cachedFlags != null ) {
194
+ return {
195
+ ok : true ,
196
+ data : cachedFlags ,
197
+ } ;
198
+ }
199
+
144
200
const url = new URL (
145
201
`/api/v2/${ this . provider } /${ owner } /repos/${ repo } /flags` ,
146
202
this . apiUrl
@@ -153,6 +209,8 @@ export class Codecov {
153
209
} ) ;
154
210
const data = await response . json ( ) ;
155
211
212
+ await this . setCached ( "flags" , owner , repo , data ) ;
213
+
156
214
return {
157
215
ok : response . ok ,
158
216
data,
@@ -163,6 +221,15 @@ export class Codecov {
163
221
await this . init ( ) ;
164
222
const { owner, repo } = payload ;
165
223
224
+ const cachedComponents = await this . getCached ( "components" , owner , repo ) ;
225
+
226
+ if ( cachedComponents != null ) {
227
+ return {
228
+ ok : true ,
229
+ data : cachedComponents ,
230
+ } ;
231
+ }
232
+
166
233
const url = new URL (
167
234
`/api/v2/${ this . provider } /${ owner } /repos/${ repo } /components` ,
168
235
this . apiUrl
@@ -175,6 +242,8 @@ export class Codecov {
175
242
} ) ;
176
243
const data = await response . json ( ) ;
177
244
245
+ await this . setCached ( "components" , owner , repo , data ) ;
246
+
178
247
return {
179
248
ok : response . ok ,
180
249
data,
0 commit comments