@@ -4,21 +4,21 @@ import ExpiryMap from 'expiry-map'
4
4
import { getSemaphore } from '@henrygd/semaphore'
5
5
6
6
// set cache expiry to 30 min
7
- const cache = new ExpiryMap ( 30 * 60 * 1000 )
7
+ const cache_30m = new ExpiryMap ( 30 * 60 * 1000 )
8
8
9
9
// set scores cache expiry to 1 min
10
- const scoresCache = new ExpiryMap ( 1 * 60 * 1000 )
11
-
12
- // valid paths for the app with their respective caches
13
- const validPaths = new Map ( [
14
- [ 'stats' , cache ] ,
15
- [ 'rankings' , cache ] ,
16
- [ 'standings' , cache ] ,
17
- [ 'history' , cache ] ,
18
- [ 'schedule' , cache ] ,
19
- [ 'schools-index' , cache ] ,
20
- [ 'game' , scoresCache ] ,
21
- [ 'scoreboard' , scoresCache ] ,
10
+ const cache_1m = new ExpiryMap ( 1 * 60 * 1000 )
11
+
12
+ // valid routes for the app with their respective caches
13
+ const validRoutes = new Map ( [
14
+ [ 'stats' , cache_30m ] ,
15
+ [ 'rankings' , cache_30m ] ,
16
+ [ 'standings' , cache_30m ] ,
17
+ [ 'history' , cache_30m ] ,
18
+ [ 'schedule' , cache_30m ] ,
19
+ [ 'schools-index' , cache_30m ] ,
20
+ [ 'game' , cache_1m ] ,
21
+ [ 'scoreboard' , cache_1m ] ,
22
22
] )
23
23
24
24
/** log message to console with timestamp */
@@ -48,18 +48,18 @@ export const app = new Elysia()
48
48
}
49
49
// check that resource is valid
50
50
const basePath = path . split ( '/' ) [ 1 ]
51
- if ( ! validPaths . has ( basePath ) ) {
51
+ if ( ! validRoutes . has ( basePath ) ) {
52
52
return error ( 400 , 'Invalid resource' )
53
53
}
54
54
return {
55
55
basePath,
56
- cache : validPaths . get ( basePath ) ?? cache ,
56
+ cache : validRoutes . get ( basePath ) ?? cache_1m ,
57
57
cacheKey : path + ( page ?? '' ) ,
58
58
}
59
59
} )
60
60
. onBeforeHandle ( ( { set, cache, cacheKey } ) => {
61
61
set . headers [ 'Content-Type' ] = 'application/json'
62
- set . headers [ 'Cache-Control' ] = `public, max-age=${ cache === scoresCache ? 60 : 1800 } `
62
+ set . headers [ 'Cache-Control' ] = `public, max-age=${ cache === cache_1m ? 60 : 1800 } `
63
63
if ( cache . has ( cacheKey ) ) {
64
64
return cache . get ( cacheKey )
65
65
}
@@ -210,8 +210,8 @@ log(`Server is running at ${app.server?.url}`)
210
210
async function getTodayUrl ( sport : string , division : string ) : Promise < string > {
211
211
// check cache
212
212
const cacheKey = `today-${ sport } -${ division } `
213
- if ( cache . has ( cacheKey ) ) {
214
- return cache . get ( cacheKey )
213
+ if ( cache_30m . has ( cacheKey ) ) {
214
+ return cache_30m . get ( cacheKey )
215
215
}
216
216
log ( `Fetching today.json for ${ sport } ${ division } ` )
217
217
const req = await fetch (
@@ -221,7 +221,7 @@ async function getTodayUrl(sport: string, division: string): Promise<string> {
221
221
throw new NotFoundError ( JSON . stringify ( { message : 'Resource not found' } ) )
222
222
}
223
223
const data = await req . json ( )
224
- cache . set ( cacheKey , data . today )
224
+ cache_30m . set ( cacheKey , data . today )
225
225
return data . today as string
226
226
}
227
227
0 commit comments