refactor: better names for the cache properties

This commit is contained in:
WebStreamr 2025-05-11 21:39:36 +00:00
parent 3b00036ff2
commit 0a57114c7d
No known key found for this signature in database
2 changed files with 8 additions and 8 deletions

View file

@ -5,15 +5,15 @@ import { Context, UrlResult } from '../types';
export class EmbedExtractors {
private readonly embedExtractors: EmbedExtractor[];
private readonly cache: TTLCache<string, UrlResult>;
private readonly urlResultCache: TTLCache<string, UrlResult>;
constructor(embedExtractors: EmbedExtractor[]) {
this.embedExtractors = embedExtractors;
this.cache = new TTLCache({ max: 1024, ttl: 900000 }); // 15m
this.urlResultCache = new TTLCache({ max: 1024, ttl: 900000 }); // 15m
}
readonly handle = async (ctx: Context, url: URL, language: string): Promise<UrlResult> => {
let urlResult = this.cache.get(url.href);
let urlResult = this.urlResultCache.get(url.href);
if (urlResult) {
return urlResult;
}
@ -24,7 +24,7 @@ export class EmbedExtractors {
}
urlResult = await embedExtractor.extract(ctx, url, language);
this.cache.set(url.href, urlResult);
this.urlResultCache.set(url.href, urlResult);
return urlResult;
};

View file

@ -7,11 +7,11 @@ import { Context } from '../types';
export class Fetcher {
private readonly fetch: FetchInterface;
private readonly cache: TTLCache<string, string>;
private readonly ipUserAgentCache: TTLCache<string, string>;
constructor(fetch: FetchInterface) {
this.fetch = fetch;
this.cache = new TTLCache({ max: 1024, ttl: 86400000 }); // 24h
this.ipUserAgentCache = new TTLCache({ max: 1024, ttl: 86400000 }); // 24h
}
readonly text = async (ctx: Context, uriOrRequest: string | Request, opts?: FetchOptions): Promise<string> => {
@ -37,14 +37,14 @@ export class Fetcher {
};
private readonly createUserAgentForIp = (ip: string): string => {
let userAgent = this.cache.get(ip);
let userAgent = this.ipUserAgentCache.get(ip);
if (userAgent) {
return userAgent;
}
userAgent = (new UserAgent()).toString();
this.cache.set(ip, userAgent);
this.ipUserAgentCache.set(ip, userAgent);
return userAgent;
};