11import { Icon , Ranking , Root , Solved , TotalSolved , Username } from "./elements" ;
22import { Item } from "./item" ;
33import query from "./query" ;
4- import { Cache , Config , Extension , FetchedData } from "./types" ;
4+ import { Config , Extension , FetchedData } from "./types" ;
55
66export class Generator {
77 public verbose = false ;
@@ -13,11 +13,11 @@ export class Generator {
1313 css : [ ] ,
1414 extensions : [ ] ,
1515 } ;
16- public cache : Cache ;
16+ public cache ? : Cache ;
1717 public headers : Record < string , string > ;
18- public count = 0 ;
18+ public fetches : Record < string , Promise < FetchedData > > = { } ;
1919
20- constructor ( cache : Cache , headers ?: Record < string , string > ) {
20+ constructor ( cache ? : Cache , headers ?: Record < string , string > ) {
2121 this . cache = cache ;
2222 this . headers = headers ?? { } ;
2323 }
@@ -38,7 +38,7 @@ export class Generator {
3838 const data = ( async ( ) => {
3939 const start = Date . now ( ) ;
4040 const data = await this . fetch ( config . username , config . site , this . headers ) ;
41- this . log ( `user data fetched in ${ Date . now ( ) - start } ms` , data ) ;
41+ this . log ( `user data fetched in ${ Date . now ( ) - start } ms` , data . profile ) ;
4242 return data ;
4343 } ) ( ) ;
4444 const body = this . body ( ) ;
@@ -54,26 +54,56 @@ export class Generator {
5454 headers : Record < string , string > ,
5555 ) : Promise < FetchedData > {
5656 this . log ( "fetching" , username , site ) ;
57- const cache_key = `data-${ username . toLowerCase ( ) } -${ site } ` ;
57+ const cache_key = `https://leetcode-stats-card.local/ data-${ username . toLowerCase ( ) } -${ site } ` ;
5858 console . log ( "cache_key" , cache_key ) ;
5959
60- await new Promise ( ( resolve ) => setTimeout ( resolve , 200 * ( this . count ++ % 5 ) ) ) ;
61- const cached : FetchedData | null = await this . cache . get ( cache_key ) ;
60+ if ( cache_key in this . fetches ) {
61+ return this . fetches [ cache_key ] ;
62+ }
63+ this . fetches [ cache_key ] = this . _fetch ( username , site , headers , cache_key ) ;
64+ this . fetches [ cache_key ] . finally ( ( ) => {
65+ delete this . fetches [ cache_key ] ;
66+ } ) ;
67+ return this . fetches [ cache_key ] ;
68+ }
69+
70+ protected async _fetch (
71+ username : string ,
72+ site : "us" | "cn" ,
73+ headers : Record < string , string > ,
74+ cache_key : string ,
75+ ) : Promise < FetchedData > {
76+ this . log ( "fetching" , username , site ) ;
77+ const cached = await this . cache ?. match ( cache_key ) ;
6278 if ( cached ) {
6379 this . log ( "fetch cache hit" ) ;
64- return cached ;
80+ return cached . json ( ) ;
6581 } else {
6682 this . log ( "fetch cache miss" ) ;
6783 }
6884
6985 try {
7086 if ( site === "us" ) {
7187 const data = await query . us ( username , headers ) ;
72- this . cache . put ( cache_key , data ) . catch ( console . error ) ;
88+ await this . cache
89+ ?. put (
90+ cache_key ,
91+ new Response ( JSON . stringify ( data ) , {
92+ headers : { "cache-control" : "max-age=300" } ,
93+ } ) ,
94+ )
95+ . catch ( console . error ) ;
7396 return data ;
7497 } else {
7598 const data = await query . cn ( username , headers ) ;
76- this . cache . put ( cache_key , data ) . catch ( console . error ) ;
99+ await this . cache
100+ ?. put (
101+ cache_key ,
102+ new Response ( JSON . stringify ( data ) , {
103+ headers : { "cache-control" : "max-age=300" } ,
104+ } ) ,
105+ )
106+ . catch ( console . error ) ;
77107 return data ;
78108 }
79109 } catch ( err ) {
0 commit comments