-
-
Notifications
You must be signed in to change notification settings - Fork 815
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b4aa73d
commit 3fefea6
Showing
6 changed files
with
119 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
version: "3" | ||
services: | ||
redis: | ||
container_name: trophy-redis | ||
image: redis:latest | ||
ports: | ||
- "6379:6379" |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
GITHUB_TOKEN1= | ||
GITHUB_TOKEN2= | ||
GITHUB_API=https://api.github.com/graphql | ||
ENABLE_REDIS= | ||
REDIS_PORT=10709 | ||
REDIS_HOST= | ||
REDIS_USERNAME= | ||
REDIS_PASSWORD= |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { Bulk, connect, Redis } from "../../deps.ts"; | ||
import { Logger } from "../Helpers/Logger.ts"; | ||
|
||
const enableCache = Deno.env.get("ENABLE_REDIS") || false; | ||
|
||
class CacheProvider { | ||
private static instance: CacheProvider; | ||
public client: Redis | null = null; | ||
|
||
private constructor() {} | ||
|
||
static getInstance(): CacheProvider { | ||
if (!CacheProvider.instance) { | ||
CacheProvider.instance = new CacheProvider(); | ||
} | ||
return CacheProvider.instance; | ||
} | ||
|
||
async connect(): Promise<void> { | ||
if (!enableCache) return; | ||
this.client = await connect({ | ||
hostname: Deno.env.get("REDIS_HOST") || "", | ||
port: Number(Deno.env.get("REDIS_PORT")) || 6379, | ||
username: Deno.env.get("REDIS_USERNAME") || "", | ||
password: Deno.env.get("REDIS_PASSWORD") || "", | ||
}); | ||
} | ||
|
||
async get(key: string): Promise<Bulk | undefined> { | ||
if (!enableCache) return undefined; | ||
|
||
try { | ||
if (!this.client) { | ||
await this.connect(); | ||
} | ||
|
||
return await this.client?.get(key); | ||
} catch { | ||
return undefined; | ||
} | ||
} | ||
|
||
async set(key: string, value: string): Promise<void> { | ||
if (!enableCache) return; | ||
|
||
try { | ||
if (!this.client) { | ||
await this.connect(); | ||
} | ||
await this.client?.set(key, value); | ||
} catch (e) { | ||
Logger.error(`Failed to set cache: ${e.message}`); | ||
} | ||
} | ||
|
||
async del(key: string): Promise<void> { | ||
if (!enableCache) return; | ||
|
||
try { | ||
if (!this.client) { | ||
await this.connect(); | ||
} | ||
await this.client?.del(key); | ||
} catch (e) { | ||
Logger.error(`Failed to delete cache: ${e.message}`); | ||
} | ||
} | ||
} | ||
|
||
export const cacheProvider = CacheProvider.getInstance(); |