Created
March 10, 2021 22:42
-
-
Save basyusuf/589773a9736284a476b3563669a03cb0 to your computer and use it in GitHub Desktop.
Good Singleton
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Redis { | |
private static instance: Redis; | |
private id: number; | |
private base_url: string; | |
private port: number; | |
private constructor(base_url: string, port: number = 6379) { | |
this.base_url = base_url; | |
this.port = port; | |
this.id = new Date().getTime(); | |
} | |
static getInstance(base_url: string, port: number = 6379) { | |
if (!this.instance) { | |
this.instance = new Redis(base_url, port); | |
} | |
return this.instance; | |
} | |
connect() { | |
console.log(`${this.base_url}:${this.port}`); | |
console.log("Connected Redis, instance id:", this.id) | |
} | |
execQuery() { | |
console.log("Executed query!") | |
} | |
//Getter and setters.. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment