11import { subgraphRequest } from '../utils' ;
22
3- let cache = { } ;
3+ let cache : Record < string , any > = { } ;
4+ let expirationTime = 0 ;
5+
46export async function getSnapshots ( network , snapshot , provider , networks ) {
5- const cacheKey = `${ network } -${ snapshot } -${ networks . join ( '-' ) } ` ;
6- if ( cache [ cacheKey ] ) return cache [ cacheKey ] ;
7+ // If snapshot is latest, return all latest
78 const snapshots = { } ;
89 networks . forEach ( ( n ) => ( snapshots [ n ] = 'latest' ) ) ;
910 if ( snapshot === 'latest' ) return snapshots ;
11+
12+ // Check if cache is valid
13+ const cacheKey = `${ network } -${ snapshot } -${ networks . join ( '-' ) } ` ;
14+ const cachedEntry = cache [ cacheKey ] ;
15+ const now = Date . now ( ) ;
16+ if ( cachedEntry && expirationTime > now ) {
17+ return cachedEntry ;
18+ }
19+ // Reset cache every hour
20+ if ( expirationTime < now ) {
21+ cache = { } ;
22+ // Set expiration time to next hour
23+ expirationTime = now + 60 * 60 * 1000 - ( now % ( 60 * 60 * 1000 ) ) ;
24+ }
25+
1026 snapshots [ network ] = snapshot ;
1127 const networkIn = Object . keys ( snapshots ) . filter ( ( s ) => network !== s ) ;
1228 if ( networkIn . length === 0 ) return snapshots ;
@@ -29,5 +45,3 @@ export async function getSnapshots(network, snapshot, provider, networks) {
2945 cache [ cacheKey ] = snapshots ;
3046 return snapshots ;
3147}
32-
33- setInterval ( ( ) => ( cache = { } ) , 1000 * 60 * 60 * 1 ) ; // Clear cache every 1 hour
0 commit comments