diff --git a/src/backend/index.ts b/src/backend/index.ts index c2a0369d..aa85c734 100644 --- a/src/backend/index.ts +++ b/src/backend/index.ts @@ -103,7 +103,7 @@ const configDir = process.env.CONFIG_DIR || path.resolve(projectDir, `./config`) version: root.get('version') }, root.get('logger')); - await root.get('cache').init(); + await root.items.cache().init(); initServer(logger, appLoggerStream, output, scrobbleSources, scrobbleClients); diff --git a/src/backend/ioc.ts b/src/backend/ioc.ts index 5cb149f8..f1a14c45 100644 --- a/src/backend/ioc.ts +++ b/src/backend/ioc.ts @@ -1,5 +1,5 @@ import { getVersion } from "@foxxmd/get-version"; -import { Logger, LogOptions } from "@foxxmd/logging"; +import { Logger, loggerDebug, LogOptions } from "@foxxmd/logging"; import { EventEmitter } from "events"; import { createContainer } from "iti"; import path from "path"; @@ -26,16 +26,18 @@ export interface RootOptions { disableWeb?: boolean loggerStream?: PassThrough loggingConfig?: LogOptions - cache?: CacheConfigOptions + cache?: CacheConfigOptions | MSCache | (() => MSCache) } -const createRoot = (options?: RootOptions) => { +const createRoot = (options: RootOptions = {logger: loggerDebug}) => { const { port = 9078, baseUrl = process.env.BASE_URL, disableWeb: dw, loggerStream, loggingConfig, + logger, + cache } = options || {}; const configDir = process.env.CONFIG_DIR || path.resolve(projectDir, `./config`); let disableWeb = dw; @@ -43,6 +45,18 @@ const createRoot = (options?: RootOptions) => { disableWeb = process.env.DISABLE_WEB === 'true'; } + let cacheFunc: () => MSCache; + let maybeSingletonCache: MSCache; + + if(cache instanceof MSCache) { + maybeSingletonCache = cache; + } else if(typeof cache === 'function') { + cacheFunc = cache; + } else { + maybeSingletonCache = new MSCache(logger, cache); + } + + const cEmitter = new WildcardEmitter(); // do nothing, just catch cEmitter.on('error', (e) => null); @@ -65,8 +79,8 @@ const createRoot = (options?: RootOptions) => { notifierEmitter: () => new EventEmitter(), loggerStream, loggingConfig, - logger: options.logger, - cache: new MSCache(options.logger, options.cache) + logger: logger, + cache: () => maybeSingletonCache !== undefined ? () => maybeSingletonCache : cacheFunc }).add((items) => { const localUrl = generateBaseURL(baseUrl, items.port) return { diff --git a/src/backend/scrobblers/AbstractScrobbleClient.ts b/src/backend/scrobblers/AbstractScrobbleClient.ts index 1e3aa3dd..01383025 100644 --- a/src/backend/scrobblers/AbstractScrobbleClient.ts +++ b/src/backend/scrobblers/AbstractScrobbleClient.ts @@ -114,7 +114,7 @@ export default abstract class AbstractScrobbleClient extends AbstractComponent i this.npLogger = childLogger(this.logger, 'Now Playing'); this.notifier = notifier; this.emitter = emitter; - this.cache = getRoot().get('cache'); + this.cache = getRoot().items.cache(); this.scrobbledPlayObjs = new FixedSizeList(this.MAX_STORED_SCROBBLES);