From e22bb111cabef9e05174b864d551574b5b75f7d4 Mon Sep 17 00:00:00 2001 From: Dick Wolff Date: Fri, 9 Feb 2024 10:50:04 +0100 Subject: [PATCH] Preload cache on converter create --- src/converter.ts | 10 ++++--- src/yahooFinanceService.ts | 54 ++++++++++++++++++++------------------ 2 files changed, 34 insertions(+), 30 deletions(-) diff --git a/src/converter.ts b/src/converter.ts index 428a77a..c05aa13 100644 --- a/src/converter.ts +++ b/src/converter.ts @@ -11,8 +11,7 @@ import { SwissquoteConverter } from "./converters/swissquoteConverter"; import { FinpensionConverter } from "./converters/finpensionConverter"; import { YahooFinanceService } from "./yahooFinanceService"; - -export function createAndRunConverter(converterType: string, inputFilePath: string, outputFilePath: string, completionCallback: CallableFunction, errorCallback: CallableFunction) { +export async function createAndRunConverter(converterType: string, inputFilePath: string, outputFilePath: string, completionCallback: CallableFunction, errorCallback: CallableFunction) { // Verify if Ghostolio account ID is set (because without it there can be no valid output). if (!process.env.GHOSTFOLIO_ACCOUNT_ID) { @@ -22,7 +21,7 @@ export function createAndRunConverter(converterType: string, inputFilePath: stri const converterTypeLc = converterType.toLocaleLowerCase(); // Determine convertor type. - const converter = createConverter(converterTypeLc); + const converter = await createConverter(converterTypeLc); // Map the file to a Ghostfolio import. converter.readAndProcessFile(inputFilePath, (result: GhostfolioExport) => { @@ -41,10 +40,13 @@ export function createAndRunConverter(converterType: string, inputFilePath: stri }, (error) => errorCallback(error)); } -function createConverter(converterType: string): AbstractConverter { +async function createConverter(converterType: string): Promise { const yahooFinanceService = new YahooFinanceService(); + const cacheSize = await yahooFinanceService.loadCache(); + console.log(`[i] Restored ${cacheSize[0]} ISIN-symbol pairs and ${cacheSize[1]} symbols from cache..`); + let converter: AbstractConverter; switch (converterType) { diff --git a/src/yahooFinanceService.ts b/src/yahooFinanceService.ts index 0b316f1..78828c5 100644 --- a/src/yahooFinanceService.ts +++ b/src/yahooFinanceService.ts @@ -29,9 +29,6 @@ export class YahooFinanceService { // Retrieve prefered exchange postfix if set in .env this.preferedExchangePostfix = process.env.DEGIRO_PREFERED_EXCHANGE_POSTFIX; - - // Preload the cache from disk. - this.preloadCache().then((cacheSize) => console.log(`\n[i] Restored ${cacheSize[0]} ISIN-symbol pairs and ${cacheSize[1]} symbols from cache..`)); } /** @@ -53,7 +50,7 @@ export class YahooFinanceService { if (symbol) { const symbolMatch = this.symbolCache.has(symbol); - + // If a match was found, return the security. if (symbolMatch) { this.logDebug(`Retrieved symbol ${symbol} from cache!`, progress); @@ -134,6 +131,33 @@ export class YahooFinanceService { return null; } + /** + * Load the cache with ISIN and symbols. + * + * @returns The size of the loaded cache + */ + public async loadCache(): Promise<[number, number]> { + + // Verify if there is data in the ISIN-Symbol cache. If so, restore to the local variable. + const isinSymbolCacheExist = await cacache.get.info(cachePath, "isinSymbolCache"); + if (isinSymbolCacheExist) { + const cache = await cacache.get(cachePath, "isinSymbolCache"); + const cacheAsJson = JSON.parse(cache.data.toString(), this.mapReviver); + this.isinSymbolCache = cacheAsJson; + } + + // Verify if there is data in the Symbol cache. If so, restore to the local variable. + const symbolCacheExists = await cacache.get.info(cachePath, "symbolCache"); + if (symbolCacheExists) { + const cache = await cacache.get(cachePath, "symbolCache"); + const cacheAsJson = JSON.parse(cache.data.toString(), this.mapReviver); + this.symbolCache = cacheAsJson; + } + + // Return cache sizes. + return [this.isinSymbolCache.size, this.symbolCache.size]; + } + /** * Get symbols for a security by a given key. * @@ -237,28 +261,6 @@ export class YahooFinanceService { return symbolMatch; } - private async preloadCache(): Promise<[number, number]> { - - // Verify if there is data in the ISIN-Symbol cache. If so, restore to the local variable. - const isinSymbolCacheExist = await cacache.get.info(cachePath, "isinSymbolCache"); - if (isinSymbolCacheExist) { - const cache = await cacache.get(cachePath, "isinSymbolCache"); - const cacheAsJson = JSON.parse(cache.data.toString(), this.mapReviver); - this.isinSymbolCache = cacheAsJson; - } - - // Verify if there is data in the Symbol cache. If so, restore to the local variable. - const symbolCacheExists = await cacache.get.info(cachePath, "symbolCache"); - if (symbolCacheExists) { - const cache = await cacache.get(cachePath, "symbolCache"); - const cacheAsJson = JSON.parse(cache.data.toString(), this.mapReviver); - this.symbolCache = cacheAsJson; - } - - // Return cache sizes. - return [this.isinSymbolCache.size, this.symbolCache.size]; - } - private async saveInCache(isin?: string, symbol?: string, value?: any) { // Save ISIN-value combination to cache if given.