refactored di into more composable parts
This commit is contained in:
parent
177fe30586
commit
26ebc77fe6
22 changed files with 908 additions and 281 deletions
44
libs/core/di/src/factories/cache.factory.ts
Normal file
44
libs/core/di/src/factories/cache.factory.ts
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
import type { AwilixContainer } from 'awilix';
|
||||
import { NamespacedCache, type CacheProvider } from '@stock-bot/cache';
|
||||
import type { ServiceDefinitions } from '../container/types';
|
||||
|
||||
export class CacheFactory {
|
||||
static createNamespacedCache(
|
||||
baseCache: CacheProvider,
|
||||
namespace: string
|
||||
): NamespacedCache {
|
||||
return new NamespacedCache(baseCache, namespace);
|
||||
}
|
||||
|
||||
static createCacheForService(
|
||||
container: AwilixContainer<ServiceDefinitions>,
|
||||
serviceName: string
|
||||
): CacheProvider | null {
|
||||
const baseCache = container.cradle.cache;
|
||||
if (!baseCache) return null;
|
||||
|
||||
return this.createNamespacedCache(baseCache, serviceName);
|
||||
}
|
||||
|
||||
static createCacheForHandler(
|
||||
container: AwilixContainer<ServiceDefinitions>,
|
||||
handlerName: string
|
||||
): CacheProvider | null {
|
||||
const baseCache = container.cradle.cache;
|
||||
if (!baseCache) return null;
|
||||
|
||||
return this.createNamespacedCache(baseCache, `handler:${handlerName}`);
|
||||
}
|
||||
|
||||
static createCacheWithPrefix(
|
||||
container: AwilixContainer<ServiceDefinitions>,
|
||||
prefix: string
|
||||
): CacheProvider | null {
|
||||
const baseCache = container.cradle.cache;
|
||||
if (!baseCache) return null;
|
||||
|
||||
// Remove 'cache:' prefix if already included
|
||||
const cleanPrefix = prefix.replace(/^cache:/, '');
|
||||
return this.createNamespacedCache(baseCache, cleanPrefix);
|
||||
}
|
||||
}
|
||||
1
libs/core/di/src/factories/index.ts
Normal file
1
libs/core/di/src/factories/index.ts
Normal file
|
|
@ -0,0 +1 @@
|
|||
export { CacheFactory } from './cache.factory';
|
||||
Loading…
Add table
Add a link
Reference in a new issue