-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: initially separate on pers and non-pers redis dbs
- Loading branch information
1 parent
6c42d17
commit b5217d2
Showing
9 changed files
with
74 additions
and
21 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
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,47 @@ | ||
import config from 'config'; | ||
import { | ||
createClient, | ||
type RedisClientType, | ||
type RedisDefaultModules, | ||
type RedisClientOptions, | ||
type RedisFunctions, | ||
} from 'redis'; | ||
import { scopedLogger } from '../logger.js'; | ||
import { count, recordResult, markFinished, type RedisScripts } from './scripts.js'; | ||
|
||
const logger = scopedLogger('persistent-redis-client'); | ||
|
||
export type PersistentRedisClient = RedisClientType<RedisDefaultModules, RedisFunctions, RedisScripts>; | ||
|
||
let redis: PersistentRedisClient; | ||
|
||
export const initPersistentRedisClient = async () => { | ||
redis = await createPersistentRedisClient(); | ||
}; | ||
|
||
export const createPersistentRedisClient = async (options?: RedisClientOptions): Promise<PersistentRedisClient> => { | ||
const client = createClient({ | ||
...config.util.toObject(config.get('redis')) as RedisClientOptions, | ||
...options, | ||
database: 1, | ||
name: 'persistent', | ||
scripts: { count, recordResult, markFinished }, | ||
}); | ||
|
||
client | ||
.on('error', (error: Error) => logger.error('connection error', error)) | ||
.on('ready', () => logger.info('connection ready')) | ||
.on('reconnecting', () => logger.info('reconnecting')); | ||
|
||
await client.connect(); | ||
|
||
return client; | ||
}; | ||
|
||
export const getPersistentRedisClient = (): PersistentRedisClient => { | ||
if (!redis) { | ||
throw new Error('redis connection to persistent db is not initialized yet'); | ||
} | ||
|
||
return redis; | ||
}; |
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