Skip to content

Commit 07eedfa

Browse files
committed
improve variable names
1 parent f96dd2d commit 07eedfa

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

src/index.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@ import ExpiryMap from 'expiry-map'
44
import { getSemaphore } from '@henrygd/semaphore'
55

66
// set cache expiry to 30 min
7-
const cache = new ExpiryMap(30 * 60 * 1000)
7+
const cache_30m = new ExpiryMap(30 * 60 * 1000)
88

99
// 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],
2222
])
2323

2424
/** log message to console with timestamp */
@@ -48,18 +48,18 @@ export const app = new Elysia()
4848
}
4949
// check that resource is valid
5050
const basePath = path.split('/')[1]
51-
if (!validPaths.has(basePath)) {
51+
if (!validRoutes.has(basePath)) {
5252
return error(400, 'Invalid resource')
5353
}
5454
return {
5555
basePath,
56-
cache: validPaths.get(basePath) ?? cache,
56+
cache: validRoutes.get(basePath) ?? cache_1m,
5757
cacheKey: path + (page ?? ''),
5858
}
5959
})
6060
.onBeforeHandle(({ set, cache, cacheKey }) => {
6161
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}`
6363
if (cache.has(cacheKey)) {
6464
return cache.get(cacheKey)
6565
}
@@ -210,8 +210,8 @@ log(`Server is running at ${app.server?.url}`)
210210
async function getTodayUrl(sport: string, division: string): Promise<string> {
211211
// check cache
212212
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)
215215
}
216216
log(`Fetching today.json for ${sport} ${division}`)
217217
const req = await fetch(
@@ -221,7 +221,7 @@ async function getTodayUrl(sport: string, division: string): Promise<string> {
221221
throw new NotFoundError(JSON.stringify({ message: 'Resource not found' }))
222222
}
223223
const data = await req.json()
224-
cache.set(cacheKey, data.today)
224+
cache_30m.set(cacheKey, data.today)
225225
return data.today as string
226226
}
227227

0 commit comments

Comments
 (0)