Skip to content

Commit

Permalink
Refactor search cache handling in Manager.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
EvarinDev committed Jul 30, 2024
1 parent 5421baf commit 5491bde
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion example/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ let manager = new Manager({
resumeStatus: true,
},
],
cache: {
caches: {
enabled: true,
time: 60000,
},
Expand Down
10 changes: 5 additions & 5 deletions src/structures/Manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,12 @@ export class Manager extends TypedEmitter<ManagerEvents> {
}

if (this.options.nodes) this.options.nodes.forEach((nodeOptions) => { return new (Structure.get("Node"))(nodeOptions); });
if (this.options.cache.enabled){
if (this.options.caches.enabled){
setInterval(() => {
if (this.search_cache.clear() === undefined) return;
this.emit("SearchCacheClear", this.search_cache.values().next().value);
this.search_cache.clear();
}, this.options.cache?.time || 10000);
}, this.options.caches?.time || 10000);
} else return;
}

Expand Down Expand Up @@ -180,7 +180,7 @@ export class Manager extends TypedEmitter<ManagerEvents> {
let search = _query.query;
let code = this.CheckURL(options.query);
if (!/^https?:\/\//.test(search)) search = `${_source}:${search}`;
if (options.cache !== false && this.options.cache.enabled !== false) {
if (options.cache !== false && this.options.caches.enabled !== false) {
if (this.search_cache.get(code)) return this.search_cache.get(code);
}
try {
Expand Down Expand Up @@ -238,7 +238,7 @@ export class Manager extends TypedEmitter<ManagerEvents> {
}
}
}
if (options.cache !== false && this.options.cache.enabled !== false) if (res.loadType === "search" || "track") this.search_cache.set(code, result);
if (options.cache !== false && this.options.caches.enabled !== false) if (res.loadType === "search" || "track") this.search_cache.set(code, result);
return result;
} catch (err) {
throw new Error(err);
Expand Down Expand Up @@ -441,7 +441,7 @@ export interface ManagerOptions {
defaultSearchPlatform?: SearchPlatform;
/** Whether the YouTube video titles should be replaced if the Author does not exactly match. */
replaceYouTubeCredentials?: boolean;
cache?: {
caches: {
/** Whether to enable cache. */
enabled: boolean;
/** Clear cache every second */
Expand Down
4 changes: 4 additions & 0 deletions test/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ let client = new Manager({
},
],
clientId: "1234567890",
caches: {
enabled: true,
time: 60000,
},
send(guild_id, payload) {
console.log(`Sending payload to guild ${guild_id}: ${JSON.stringify(payload)}`);
},
Expand Down

0 comments on commit 5491bde

Please sign in to comment.