Browse Source

Perf: adjust cache TTL

SukkaW 2 years ago
parent
commit
61b88c5807
2 changed files with 11 additions and 10 deletions
  1. 8 8
      Build/lib/cache-filesystem.ts
  2. 3 2
      Build/lib/get-phishing-domains.ts

+ 8 - 8
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';

+ 3 - 2
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));