-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from namecheap/perf/intl
perf: add TTL cache for Intl service
- Loading branch information
Showing
16 changed files
with
442 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,13 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"composite": true, | ||
"target": "es2015", | ||
"module": "commonjs", | ||
"declaration": true, | ||
"outDir": "../../dist/app", | ||
"strict": true, | ||
"types" : ["mocha"], | ||
"esModuleInterop": true | ||
}, | ||
"include": ["**/*.ts"], | ||
} | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"composite": true, | ||
"target": "es2015", | ||
"module": "commonjs", | ||
"declaration": true, | ||
"outDir": "../../dist/app", | ||
"strict": true, | ||
"esModuleInterop": true | ||
}, | ||
"include": ["**/*.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
type Options = { | ||
ttl?: number; | ||
cleanupInterval?: number; | ||
}; | ||
|
||
type CacheKeyFn<K, U extends unknown[]> = (...args: U) => K; | ||
|
||
type CacheItem<V> = { | ||
value: V; | ||
expiresAt: number; | ||
}; | ||
|
||
export class TtlCache<K, V> { | ||
private cache: Map<K, CacheItem<V>> = new Map(); | ||
private ttl: number; // Time-to-live in milliseconds | ||
private cleanupInterval: number; | ||
private timeoutId?: NodeJS.Timeout; | ||
|
||
constructor({ ttl = 10_000, cleanupInterval = 5000 }: Options = {}) { | ||
this.ttl = ttl; | ||
this.cleanupInterval = cleanupInterval; | ||
this.scheduleCleanup(); | ||
} | ||
|
||
// Set a value with expiration | ||
public set(key: K, value: V): void { | ||
this.cache.set(key, { value, expiresAt: Date.now() + this.ttl }); | ||
} | ||
|
||
public get(key: K): V | undefined { | ||
const entry = this.cache.get(key); | ||
if (!entry) return undefined; | ||
return entry.value; | ||
} | ||
|
||
public wrap<T extends (...args: any[]) => any>( | ||
fn: T, | ||
cacheKeyFn: CacheKeyFn<K, Parameters<T>>, | ||
): (...args: Parameters<T>) => ReturnType<T> { | ||
return (...args: Parameters<T>) => { | ||
const cacheKey = cacheKeyFn(...args); | ||
const cachedValue = this.get(cacheKey); | ||
if (cachedValue !== undefined) { | ||
return cachedValue; | ||
} else { | ||
const value = fn(...args); | ||
this.set(cacheKey, value); | ||
return value; | ||
} | ||
}; | ||
} | ||
|
||
private cleanup(): void { | ||
const now = Date.now(); | ||
for (const [key, entry] of this.cache) { | ||
if (entry.expiresAt <= now) { | ||
this.cache.delete(key); | ||
} | ||
} | ||
this.scheduleCleanup(); | ||
} | ||
|
||
private scheduleCleanup(): void { | ||
this.timeoutId = setTimeout(() => this.cleanup(), this.cleanupInterval); | ||
this.timeoutId.unref?.(); | ||
} | ||
|
||
// Clear the entire cache and stop the cleanup interval | ||
public clear(): void { | ||
this.cache.clear(); | ||
} | ||
|
||
public destroy(): void { | ||
this.clear(); | ||
clearTimeout(this.timeoutId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
export function getCanonicalLocale(locale = '', supportedLocales: string[]) { | ||
const supportedLangs = supportedLocales.map((v) => v.split('-')[0]).filter((v, i, a) => a.indexOf(v) === i); | ||
|
||
const locData = locale.split('-'); | ||
|
||
if (locData.length === 2) { | ||
locale = locData[0].toLowerCase() + '-' + locData[1].toUpperCase(); | ||
} else if (locData.length === 1) { | ||
locale = locData[0].toLowerCase(); | ||
} else { | ||
return null; | ||
} | ||
|
||
if (supportedLangs.indexOf(locale.toLowerCase()) !== -1) { | ||
for (const v of supportedLocales) { | ||
if (v.split('-')[0] === locale) { | ||
locale = v; | ||
break; | ||
} | ||
} | ||
} else if (supportedLocales.indexOf(locale) === -1) { | ||
return null; | ||
} | ||
|
||
return locale; | ||
} |
Oops, something went wrong.