Skip to content

Commit

Permalink
Merge pull request knightcrawler-stremio#138 from Gabisonfire/hotfix/…
Browse files Browse the repository at this point in the history
…fix_tracker_ttl

Fix Trackers TTL
  • Loading branch information
purple-emily authored Mar 9, 2024
2 parents 7a9e949 + 61ec8d7 commit cee1eeb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ import {BooleanHelpers} from "@helpers/boolean_helpers";

export const trackerConfig = {
TRACKERS_URL: process.env.TRACKERS_URL || 'https://ngosang.github.io/trackerslist/trackers_all.txt',
GITHUB_PAT: process.env.GITHUB_PAT,
UDP_ENABLED: BooleanHelpers.parseBool(process.env.UDP_TRACKERS_ENABLED, false)
};
9 changes: 7 additions & 2 deletions src/node/consumer/src/lib/services/cache_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class CacheService implements ICacheService {
}

cacheTrackers(method: CacheMethod): Promise<CacheMethod> {
return this.cacheWrap(CacheType.Memory, `${TRACKERS_KEY_PREFIX}`, method, {ttl: TRACKERS_TTL});
return this.cacheWrap(CacheType.MongoDb, `${TRACKERS_KEY_PREFIX}`, method, {ttl: TRACKERS_TTL});
}

private initiateMemoryCache = (): MemoryCache =>
Expand Down Expand Up @@ -102,10 +102,15 @@ export class CacheService implements ICacheService {
return method();
}

const expirationTime = new Date(Date.now() + options.ttl * 1000);

this.logger.debug(`Cache type: ${cacheType}`);
this.logger.debug(`Cache key: ${key}`);
this.logger.debug(`Cache options: ${JSON.stringify(options)}`);
this.logger.debug(`Cache item will expire at: ${expirationTime.toISOString()}`);

return cache.wrap(key, method, options.ttl);
// Memory Cache is Milliseconds, Mongo Cache converts to Seconds internally.
const ttl : number = cacheType === CacheType.Memory ? options.ttl * 1000 : options.ttl;
return cache.wrap(key, method, ttl);
};
}
8 changes: 7 additions & 1 deletion src/node/consumer/src/lib/services/tracker_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ export class TrackerService implements ITrackerService {
}

private downloadTrackers = async (): Promise<string[]> => {
const response: AxiosResponse<string> = await axios.get(configurationService.trackerConfig.TRACKERS_URL);
const headers = {};

if (configurationService.trackerConfig.GITHUB_PAT) {
headers['Authorization'] = `Bearer ${configurationService.trackerConfig.GITHUB_PAT}`;
}

const response: AxiosResponse<string> = await axios.get(configurationService.trackerConfig.TRACKERS_URL, { headers });
const trackersListText: string = response.data;
// Trackers are separated by a newline character
let urlTrackers = trackersListText.split("\n");
Expand Down

0 comments on commit cee1eeb

Please sign in to comment.