Skip to content

Commit

Permalink
Merge pull request #18 from FAYStarNext/dev
Browse files Browse the repository at this point in the history
chore: Update search cache clearing in Manager.ts
EvarinDev authored Jul 24, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents ccac012 + ecafbef commit 0a8f339
Showing 4 changed files with 22 additions and 18 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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>

@@ -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);
@@ -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);
8 changes: 6 additions & 2 deletions example/src/index.ts
Original file line number Diff line number Diff line change
@@ -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);
@@ -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" , () => {
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
@@ -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",
16 changes: 5 additions & 11 deletions src/structures/Manager.ts
Original file line number Diff line number Diff line change
@@ -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);
}
@@ -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);
@@ -445,7 +439,7 @@ export interface ManagerOptions {
replaceYouTubeCredentials?: boolean;
cache?: {
/** Whether to enable cache. */
enable: boolean;
enabled: boolean;
/** Clear cache every second */
time: number;
}
@@ -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;

0 comments on commit 0a8f339

Please sign in to comment.