refactored di into more composable parts

This commit is contained in:
Boki 2025-06-22 21:47:39 -04:00
parent 177fe30586
commit 26ebc77fe6
22 changed files with 908 additions and 281 deletions

View 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);
}
}

View file

@ -0,0 +1 @@
export { CacheFactory } from './cache.factory';