chore(source): cache source results for 3 hours

This commit is contained in:
WebStreamr 2025-08-31 19:14:43 +00:00
parent 9da7c07dc6
commit c82a7ab756
No known key found for this signature in database
2 changed files with 18 additions and 1 deletions

View file

@ -70,6 +70,7 @@ export class PrimeWire extends Source {
.map(async (redirectUrl) => {
let targetUrl = this.redirectUrlCache.get(redirectUrl.href);
/* istanbul ignore if */
if (!targetUrl) {
const linkFetchUrl = new URL(redirectUrl.href.replace('/gos/', '/go/'));
linkFetchUrl.searchParams.set('token', linksToken);

View file

@ -1,3 +1,4 @@
import TTLCache from '@isaacs/ttlcache';
import { ContentType } from 'stremio-addon-sdk';
import { Context, CountryCode } from '../types';
import { Id } from '../utils';
@ -13,15 +14,30 @@ export abstract class Source {
public abstract readonly label: string;
public readonly ttl: number = 10800000; // 3h
public abstract readonly contentTypes: ContentType[];
public abstract readonly countryCodes: CountryCode[];
public abstract readonly baseUrl: string;
private readonly sourceResultCache: TTLCache<string | number, SourceResult[]>;
public constructor() {
this.sourceResultCache = new TTLCache({ max: 1024, ttl: this.ttl });
}
protected abstract handleInternal(ctx: Context, type: ContentType, id: Id): Promise<(SourceResult[])>;
public async handle(ctx: Context, type: ContentType, id: Id): Promise<(SourceResult[])> {
return await this.handleInternal(ctx, type, id);
if (this.sourceResultCache.has(id.id)) {
return this.sourceResultCache.get(id.id) as SourceResult[];
}
const sourceResults = await this.handleInternal(ctx, type, id);
this.sourceResultCache.set(id.id, sourceResults);
return sourceResults;
}
}