Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Update search cache clearing in Manager.ts #18

Merged
merged 2 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 3 additions & 3 deletions 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 All @@ -16,9 +16,9 @@
"devDependencies": {
"@eslint/js": "^9.7.0",
"@types/bun": "latest",
"eslint": "9.x",
"eslint": "^9.7.0",
"globals": "^15.8.0",
"typescript-eslint": "^7.16.1",
"typescript-eslint": "^7.17.0",
"@babel/cli": "^7.24.8",
"@babel/core": "^7.24.9",
"@babel/plugin-proposal-class-properties": "^7.18.6",
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