import { LRUCache } from 'lru-cache'; import type * as Stuntman from '@stuntman/shared'; import sizeof from 'object-sizeof'; const DNS_CACHE_OPTIONS: any = { max: 1000, ttl: 1000 * 60 * 15, allowStale: false, updateAgeOnGet: false, updateAgeOnHas: false, }; const trafficStoreInstances: Record> = {}; const dnsResolutionCacheInstances: Record> = {}; export const getTrafficStore = (key: string, options?: Stuntman.StorageConfig) => { if (!(key in trafficStoreInstances)) { if (!options) { throw new Error('initialize with options first'); } trafficStoreInstances[key] = new LRUCache({ max: options.limitCount, maxSize: options.limitSize, ttl: options.ttl, allowStale: false, updateAgeOnGet: false, updateAgeOnHas: false, sizeCalculation: (value) => sizeof(value), }); } return trafficStoreInstances[key]!; }; export const getDnsResolutionCache = (key: string) => { if (!(key in dnsResolutionCacheInstances)) { dnsResolutionCacheInstances[key] = new LRUCache(DNS_CACHE_OPTIONS); } return dnsResolutionCacheInstances[key]!; };