From 508b5c0f4e86fc3113f6d70891877af3e9bb7412 Mon Sep 17 00:00:00 2001 From: softsimon Date: Sat, 19 Sep 2020 23:02:02 +0700 Subject: [PATCH] Markets API: Adding cache for general /ticker requests --- backend/src/api/bisq/markets-api.ts | 10 ++++++++++ backend/src/api/bisq/markets.ts | 1 + 2 files changed, 11 insertions(+) diff --git a/backend/src/api/bisq/markets-api.ts b/backend/src/api/bisq/markets-api.ts index 6d4e74cec..c3bac5a38 100644 --- a/backend/src/api/bisq/markets-api.ts +++ b/backend/src/api/bisq/markets-api.ts @@ -11,6 +11,7 @@ class BisqMarketsApi { private fiatCurrenciesIndexed: { [code: string]: true } = {}; private allCurrenciesIndexed: { [code: string]: Currency } = {}; private tradeDataByMarket: { [market: string]: TradesData[] } = {}; + private tickersCache: Ticker | Tickers | null = null; constructor() { } @@ -49,6 +50,11 @@ class BisqMarketsApi { }); } + updateCache() { + this.tickersCache = null; + this.tickersCache = this.getTicker(); + } + getCurrencies( type: 'crypto' | 'fiat' | 'all' = 'all', ): Currencies { @@ -281,6 +287,10 @@ class BisqMarketsApi { return this.getTickerFromMarket(market); } + if (this.tickersCache) { + return this.tickersCache; + } + const allMarkets = this.getMarkets(); const tickers = {}; for (const m in allMarkets) { diff --git a/backend/src/api/bisq/markets.ts b/backend/src/api/bisq/markets.ts index 1bf13976e..d66e56c9a 100644 --- a/backend/src/api/bisq/markets.ts +++ b/backend/src/api/bisq/markets.ts @@ -93,6 +93,7 @@ class Bisq { } const time = new Date().getTime() - start; if (marketsDataUpdated) { + bisqMarket.updateCache(); console.log('Bisq market data updated in ' + time + ' ms'); } } catch (e) {