-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add RedisWriteClient and RedisReadClient
- Loading branch information
Showing
4 changed files
with
84 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { RedisClientType, createClient } from 'redis'; | ||
|
||
const REDIS_URL_READ = process.env.REDIS_URL_WRITE || 'redis://127.0.0.1:6379'; | ||
|
||
export default class RedisReadClient { | ||
private static client: RedisClientType; | ||
|
||
private constructor() {} | ||
|
||
static async start() { | ||
if (!RedisReadClient.client) { | ||
RedisReadClient.client = createClient({ url: REDIS_URL_READ }); | ||
|
||
try { | ||
await RedisReadClient.client.connect(); | ||
} catch (e) { | ||
console.error('[REDIS WRITE CONNECT ERROR]', e); | ||
process.exit(1); | ||
} | ||
} | ||
} | ||
|
||
static async get(key: string): Promise<string> { | ||
try { | ||
return await RedisReadClient.client.get(key); | ||
} catch (e) { | ||
console.error('[CACHE_SESSIONS_GET_ERROR]', e, key); | ||
} | ||
} | ||
} |
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,38 @@ | ||
import { RedisClientType, createClient } from 'redis'; | ||
|
||
const REDIS_URL_WRITE = process.env.REDIS_URL_WRITE || 'redis://127.0.0.1:6379'; | ||
|
||
export default class RedisWriteClient { | ||
private static client: RedisClientType; | ||
|
||
private constructor() {} | ||
|
||
static async start() { | ||
if (!RedisWriteClient.client) { | ||
RedisWriteClient.client = createClient({ url: REDIS_URL_WRITE }); | ||
|
||
try { | ||
await RedisWriteClient.client.connect(); | ||
} catch (e) { | ||
console.error('[REDIS WRITE CONNECT ERROR]', e); | ||
process.exit(1); | ||
} | ||
} | ||
} | ||
|
||
static async set(key: string, value: string) { | ||
try { | ||
await RedisWriteClient.client.set(key, value); | ||
} catch (e) { | ||
console.error('[CACHE_SESSIONS_ADD_ERROR]', e, key, value); | ||
} | ||
} | ||
|
||
static async del(keys: string[]) { | ||
try { | ||
await RedisWriteClient.client.del(keys); | ||
} catch (e) { | ||
console.error('[CACHE_SESSIONS_REMOVE_ERROR]', e, keys); | ||
} | ||
} | ||
} |