Dynamic cache key for all data.

This commit is contained in:
James Cole
2023-10-01 07:17:56 +02:00
parent aa629523a0
commit 272c73054e
9 changed files with 75 additions and 202 deletions

View File

@@ -26,6 +26,7 @@ import Get from "../../api/v2/model/account/get.js";
import {Chart} from 'chart.js';
import {getDefaultChartSettings} from "../../support/default-chart-settings.js";
import {getColors} from "../../support/get-colors.js";
import {getCacheKey} from "../../support/get-cache-key.js";
// this is very ugly, but I have no better ideas at the moment to save the currency info
// for each series.
@@ -36,6 +37,7 @@ let afterPromises = false;
const CHART_CACHE_KEY = 'dashboard-accounts-chart';
const ACCOUNTS_CACHE_KEY = 'dashboard-accounts-data';
export default () => ({
loading: false,
loadingAccounts: false,
@@ -47,8 +49,12 @@ export default () => ({
setVariable('autoConversion', this.autoConversion);
},
getFreshData() {
const start = new Date(window.store.get('start'));
const end = new Date(window.store.get('end'));
const chartCacheKey = getCacheKey(CHART_CACHE_KEY, start, end)
const cacheValid = window.store.get('cacheValid');
let cachedData = window.store.get(CHART_CACHE_KEY);
let cachedData = window.store.get(chartCacheKey);
if (cacheValid && typeof cachedData !== 'undefined') {
this.drawChart(this.generateOptions(cachedData));
@@ -56,10 +62,10 @@ export default () => ({
return;
}
const dashboard = new Dashboard();
dashboard.dashboard(new Date(window.store.get('start')), new Date(window.store.get('end')), null).then((response) => {
dashboard.dashboard(start, end, null).then((response) => {
this.chartData = response.data;
// cache generated options:
window.store.set(CHART_CACHE_KEY, response.data);
window.store.set(chartCacheKey, response.data);
this.drawChart(this.generateOptions(this.chartData));
this.loading = false;
});
@@ -160,8 +166,12 @@ export default () => ({
this.loadingAccounts = false;
return;
}
const start = new Date(window.store.get('start'));
const end = new Date(window.store.get('end'));
const accountCacheKey = getCacheKey(ACCOUNTS_CACHE_KEY, start, end);
const cacheValid = window.store.get('cacheValid');
let cachedData = window.store.get(ACCOUNTS_CACHE_KEY);
let cachedData = window.store.get(accountCacheKey);
if (cacheValid && typeof cachedData !== 'undefined') {
this.accountList = cachedData;
@@ -238,7 +248,7 @@ export default () => ({
this.accountList = accounts;
this.loadingAccounts = false;
window.store.set(ACCOUNTS_CACHE_KEY, accounts);
window.store.set(accountCacheKey, accounts);
}
});
});