Skip to content

Commit

Permalink
Merge pull request #79 from clober-dex/perf/cache
Browse files Browse the repository at this point in the history
perf: cache chart log
  • Loading branch information
graykode authored Jul 11, 2024
2 parents 77a8155 + c842a3c commit 082d513
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion src/apis/chart-logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,54 @@ export async function fetchLatestChartLog(
}
}

const buildChartCacheKey = (
chainId: CHAIN_IDS,
marketCode: string,
intervalType: CHART_LOG_INTERVALS,
from: number,
to: number,
) => `${chainId}:${marketCode}:${intervalType}:${from}:${to}`
const chartLogsCache = new Map<string, ChartLog[]>()
const getChartLogsFromCache = (
chainId: CHAIN_IDS,
marketCode: string,
intervalType: CHART_LOG_INTERVALS,
from: number,
to: number,
): ChartLog[] | undefined =>
chartLogsCache.get(
buildChartCacheKey(chainId, marketCode, intervalType, from, to),
)
const setChartLogsToCache = (
chainId: CHAIN_IDS,
marketCode: string,
intervalType: CHART_LOG_INTERVALS,
from: number,
to: number,
chartLogs: ChartLog[],
) =>
chartLogsCache.set(
buildChartCacheKey(chainId, marketCode, intervalType, from, to),
chartLogs,
)

export async function fetchChartLogs(
chainId: CHAIN_IDS,
marketCode: string,
intervalType: CHART_LOG_INTERVALS,
from: number,
to: number,
): Promise<ChartLog[]> {
const cachedChartLogs = getChartLogsFromCache(
chainId,
marketCode,
intervalType,
from,
to,
)
if (cachedChartLogs !== undefined) {
return cachedChartLogs
}
const chartLogsBetweenFromAndTo: ChartLog[] = []
let skip = 0
// eslint-disable-next-line no-constant-condition
Expand Down Expand Up @@ -220,6 +261,6 @@ export async function fetchChartLogs(

timestampForAcc += intervalInNumber
}

setChartLogsToCache(chainId, marketCode, intervalType, from, to, result)
return result
}

0 comments on commit 082d513

Please sign in to comment.