Skip to content

Commit

Permalink
chore: Update search cache clearing in Manager.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
EvarinDev committed Jul 24, 2024
1 parent 978baba commit 8c999e4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 16 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ To install Sunday.ts, follow these steps:
That's it! You have successfully installed Sunday.ts and are ready to start using it in your Node.js project.

## Features
- [ ] Multi version
- [ ] Plugin
- [x] SearchCache

## 🎈 Usage <a name="usage"></a>

Expand All @@ -83,6 +82,10 @@ let manager = new Manager({
resumeStatus: true,
},
],
cache: {
enabled: true,
time: 60000,
},
clientId: "1234567890",
send(guild_id, payload) {
const guild = client.guilds.cache.get(guild_id);
Expand Down Expand Up @@ -146,6 +149,9 @@ client.on("messageCreate", async (message) => {
return message.reply(`enqueuing ${res.tracks[0].title}. ${end}`);
}
});
manager.on("SearchCacheClear" , (key: string, values) => {
console.log(`Cache cleared for ${key} with values: ${values}`);
});
client.on("raw", (data) => manager.updateVoiceState(data));
client.on("ready" , () => {
manager.init(client.user?.id as string);
Expand Down
8 changes: 6 additions & 2 deletions example/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ let manager = new Manager({
resumeStatus: true,
},
],
cache: {
enabled: true,
time: 60000,
},
clientId: "1234567890",
send(guild_id, payload) {
const guild = client.guilds.cache.get(guild_id);
Expand Down Expand Up @@ -83,8 +87,8 @@ client.on("messageCreate", async (message) => {
return message.reply(`enqueuing ${res.tracks[0].title}. ${end}`);
}
});
manager.on("SearchCacheClear" , (key: string, values) => {
console.log(`Cache cleared for ${key} with values: ${values}`);
manager.on("SearchCacheClear" , (data) => {
console.log(`Cache cleared: ${data}`);
});
client.on("raw", (data) => manager.updateVoiceState(data));
client.on("ready" , () => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "sunday.ts",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"version": "1.0.11-indev",
"version": "1.0.12-indev",
"description": "Sunday a lavalink wrapper",
"license": "MIT",
"author": "FAYStarNext",
Expand Down
16 changes: 5 additions & 11 deletions src/structures/Manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,8 @@ export class Manager extends TypedEmitter<ManagerEvents> {

if (this.options.nodes) this.options.nodes.forEach((nodeOptions) => { return new (Structure.get("Node"))(nodeOptions); });
setInterval(() => {
const searchCacheKeys = this.search_cache.keys();
const searchCacheValues = this.search_cache.values();
const firstKey = searchCacheKeys.next().value;
const firstValue = searchCacheValues.next().value;
if (firstKey && firstValue) {
const searchResultString = JSON.stringify(firstValue);
this.emit("SearchCacheClear", firstKey, searchResultString);
}
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);
}
Expand Down Expand Up @@ -240,7 +234,7 @@ export class Manager extends TypedEmitter<ManagerEvents> {
}
}
}
if (res.loadType === "search" || "track") this.search_cache.set(code, result);
if (options.cache !== false && this.options.cache.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 @@ -445,7 +439,7 @@ export interface ManagerOptions {
replaceYouTubeCredentials?: boolean;
cache?: {
/** Whether to enable cache. */
enable: boolean;
enabled: boolean;
/** Clear cache every second */
time: number;
}
Expand Down Expand Up @@ -506,7 +500,7 @@ export interface PlaylistData {
}

export interface ManagerEvents {
SearchCacheClear: (key: string, values: SearchResult | unknown) => void;
SearchCacheClear: (data: string) => void;
NodeCreate: (node: Node) => void;
NodeDestroy: (node: Node) => void;
NodeConnect: (node: Node) => void;
Expand Down

0 comments on commit 8c999e4

Please sign in to comment.