From 61b88c580753e8ae6842647c0482ab430ed51ed9 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Sun, 7 Jan 2024 00:39:11 +0800 Subject: [PATCH] Perf: adjust cache TTL --- Build/lib/cache-filesystem.ts | 16 ++++++++-------- Build/lib/get-phishing-domains.ts | 5 +++-- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/Build/lib/cache-filesystem.ts b/Build/lib/cache-filesystem.ts index 1686d364..397b29d1 100644 --- a/Build/lib/cache-filesystem.ts +++ b/Build/lib/cache-filesystem.ts @@ -100,7 +100,7 @@ export class Cache { if (temporaryBypass) { return fn(); } - if (ttl === null) { + if (ttl == null) { this.del(key); return fn(); } @@ -108,7 +108,7 @@ export class Cache { const cached = this.get(key); let value: T; if (cached == null) { - console.log(picocolors.yellow('[cache] miss'), picocolors.gray(key)); + console.log(picocolors.yellow('[cache] miss'), picocolors.gray(key), picocolors.gray(`ttl: ${ttl / 60 * 60 * 1000}h`)); value = await fn(); const serializer = 'serializer' in opt ? opt.serializer : identity; @@ -136,13 +136,13 @@ const randomInt = (min: number, max: number) => Math.floor(Math.random() * (max // Add some randomness to the cache ttl to avoid thundering herd export const TTL = { - THREE_HOURS: () => randomInt(2, 4) * 60 * 60 * 1000, - TWLVE_HOURS: () => randomInt(9, 14) * 60 * 60 * 1000, + THREE_HOURS: () => randomInt(1, 3) * 60 * 60 * 1000, + TWLVE_HOURS: () => randomInt(8, 12) * 60 * 60 * 1000, ONE_DAY: () => randomInt(23, 25) * 60 * 60 * 1000, - THREE_DAYS: () => randomInt(2, 4) * 24 * 60 * 60 * 1000, - ONE_WEEK: () => randomInt(5, 8) * 24 * 60 * 60 * 1000, - TWO_WEEKS: () => randomInt(12, 16) * 24 * 60 * 60 * 1000, - TEN_DAYS: () => randomInt(9, 11) * 24 * 60 * 60 * 1000 + THREE_DAYS: () => randomInt(1, 3) * 24 * 60 * 60 * 1000, + ONE_WEEK: () => randomInt(5, 7) * 24 * 60 * 60 * 1000, + TWO_WEEKS: () => randomInt(10, 14) * 24 * 60 * 60 * 1000, + TEN_DAYS: () => randomInt(7, 10) * 24 * 60 * 60 * 1000 }; const separator = '\u0000'; diff --git a/Build/lib/get-phishing-domains.ts b/Build/lib/get-phishing-domains.ts index 3d4aa720..f960bbe2 100644 --- a/Build/lib/get-phishing-domains.ts +++ b/Build/lib/get-phishing-domains.ts @@ -5,6 +5,7 @@ import * as tldts from 'tldts'; import { createTrie } from './trie'; import { createCachedGorhillGetDomain } from './cached-tld-parse'; import { processLine } from './process-line'; +import { TTL } from './cache-filesystem'; const WHITELIST_DOMAIN = new Set([ 'w3s.link', @@ -84,8 +85,8 @@ const BLACK_TLD = new Set([ export const getPhishingDomains = () => traceAsync('get phishing domains', async () => { const [domainSet, domainSet2, gorhill] = await Promise.all([ - processHosts('https://curbengh.github.io/phishing-filter/phishing-filter-hosts.txt', true, true), - processDomainLists('https://phishing.army/download/phishing_army_blocklist.txt', true), + processHosts('https://curbengh.github.io/phishing-filter/phishing-filter-hosts.txt', true, true, TTL.THREE_HOURS()), + processDomainLists('https://phishing.army/download/phishing_army_blocklist.txt', true, TTL.THREE_HOURS()), getGorhillPublicSuffixPromise() ]); domainSet2.forEach((domain) => domainSet.add(domain));