update lastUsed
This commit is contained in:
parent
4e81d0e27d
commit
5607c2fe37
1 changed files with 10 additions and 5 deletions
|
|
@ -21,13 +21,18 @@ export default defineEventHandler(async (event) => {
|
||||||
const { url } = inMemoryCache.get(cardUrl)!
|
const { url } = inMemoryCache.get(cardUrl)!
|
||||||
await send(event, url)
|
await send(event, url)
|
||||||
|
|
||||||
// Remove oldest entry if cache is too big
|
inMemoryCache.set(cardUrl, { url, lastUsed: Date.now() })
|
||||||
|
|
||||||
|
// Remove some oldest entries if cache gets to big
|
||||||
if (inMemoryCache.size > 5000) {
|
if (inMemoryCache.size > 5000) {
|
||||||
const oldestEntry = [...inMemoryCache.entries()].reduce(
|
// Remove 10% of the oldest entries
|
||||||
(acc, [key, { lastUsed }]) => (lastUsed < acc.lastUsed ? { key, lastUsed } : acc),
|
const entries = Array.from(inMemoryCache.entries()).sort(
|
||||||
{ key: '', lastUsed: Infinity },
|
(a, b) => a[1].lastUsed - b[1].lastUsed,
|
||||||
)
|
)
|
||||||
inMemoryCache.delete(oldestEntry.key)
|
const entriesToRemove = Math.floor(entries.length * 0.1)
|
||||||
|
|
||||||
|
for (let i = 0; i < entriesToRemove; i++)
|
||||||
|
inMemoryCache.delete(entries[i][0])
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue