This repository has been archived by the owner on Apr 23, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 947
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #180 from mgilangjanuar/staging
Release v1.6.0
- Loading branch information
Showing
18 changed files
with
863 additions
and
130 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,4 +24,6 @@ MIDTRANS_MERCHANT_ID= | |
MIDTRANS_CLIENT_KEY= | ||
MIDTRANS_SERVER_KEY= | ||
|
||
REDIS_URI= | ||
|
||
IS_MAINTENANCE= |
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
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
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,55 @@ | ||
import IORedis, { Redis as IOredis } from 'ioredis' | ||
|
||
export class Redis { | ||
private static client: Redis | ||
private redis: IOredis | ||
|
||
private constructor() { | ||
if (!process.env.REDIS_URI) { | ||
throw new Error('REDIS_URI is not defined') | ||
} | ||
this.redis = new IORedis(process.env.REDIS_URI) | ||
} | ||
|
||
public static connect(): Redis { | ||
if (!this.client) { | ||
this.client = new Redis() | ||
} | ||
return this.client | ||
} | ||
|
||
public async get(key: string): Promise<any> { | ||
const result = await this.redis.get(key) | ||
if (!result) return null | ||
try { | ||
return JSON.parse(result) | ||
} catch (error) { | ||
return result | ||
} | ||
} | ||
|
||
public async set(key: string, data: unknown, ex?: number): Promise<boolean> { | ||
try { | ||
if (ex) { | ||
return await this.redis.set(key, JSON.stringify(data), 'EX', ex) === 'OK' | ||
} else { | ||
return await this.redis.set(key, JSON.stringify(data)) === 'OK' | ||
} | ||
} catch (error) { | ||
if (ex) { | ||
return await this.redis.set(key, data as any, 'EX', ex) === 'OK' | ||
} else { | ||
return await this.redis.set(key, data as any) === 'OK' | ||
} | ||
} | ||
} | ||
|
||
public async getFromCacheFirst<T>(key: string, fn: () => T | Promise<T>, ex?: number): Promise<T> { | ||
const result = await this.get(key) | ||
if (result) return result | ||
|
||
const data = await fn() | ||
await this.set(key, data, ex) | ||
return data | ||
} | ||
} |
Oops, something went wrong.