Skip to content

Commit e5e94bc

Browse files
committed
Update Cache Strategy
1 parent 147d676 commit e5e94bc

1 file changed

Lines changed: 18 additions & 5 deletions

File tree

src/api.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ export async function getVTReleaseStats(req) {
175175
*/
176176
export async function getVTStats(hash) {
177177
const key = `/vt/id/${hash}`
178-
const timeout = 60 * 60 * 4
178+
let timeout = 60 * 60 * 4
179179
debug('key:', key)
180180
// NOTE: Duplicate Code - 5 lines
181181
const cached = await cacheGet(key)
@@ -186,21 +186,34 @@ export async function getVTStats(hash) {
186186
}
187187
debug(`-- CACHE MISS: ${key}`)
188188
const vt = new VTApi(process.env.VT_API_KEY)
189-
let stats
189+
let stats, epoch
190190
if (hash.endsWith('==')) {
191191
debug('DEPRECATED - getAnalysis') // TODO: Deprecated
192192
const data = await vt.getAnalysis(hash)
193-
// debug('data:', JSON.stringify(data, null, 2))
194-
// noinspection JSUnresolvedReference
193+
// debug('data:', JSON.stringify(data))
195194
stats = data?.data?.attributes?.stats
195+
epoch = data?.data?.attributes?.date
196196
} else {
197197
// debug('getReport')
198198
const data = await vt.getReport(hash)
199-
// debug('data:', JSON.stringify(data, null, 2))
199+
// debug('data:', JSON.stringify(data))
200200
// noinspection JSUnresolvedReference
201201
stats = data?.data?.attributes?.last_analysis_stats
202+
// noinspection JSUnresolvedReference
203+
epoch = data?.data?.attributes?.last_analysis_date
202204
}
203205
if (!stats) await cacheError(key, 'VT Stats Not Found')
206+
if (typeof epoch === 'number') {
207+
const date = new Date(0)
208+
date.setUTCSeconds(epoch)
209+
const now = new Date()
210+
const ms = now.getTime() - date.getTime()
211+
const hours = Math.floor(ms / 1000 / 60 / 60)
212+
debug('hours:', hours)
213+
if (hours > 4) timeout = 60 * 60 * 24
214+
if (hours > 48) timeout = 60 * 60 * 96
215+
}
216+
debug('timeout:', timeout)
204217
await cacheSet(key, stats, timeout)
205218
return stats
206219
}

0 commit comments

Comments
 (0)