mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-19 01:04:25 +08:00
support setting exchange rate cache expiration time
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import { ref, computed } from 'vue';
|
||||
import { defineStore } from 'pinia';
|
||||
|
||||
import { useSettingsStore } from './setting.ts';
|
||||
|
||||
import { type BeforeResolveFunction, itemAndIndex } from '@/core/base.ts';
|
||||
|
||||
import type {
|
||||
@@ -47,8 +49,12 @@ function clearExchangeRatesFromLocalStorage(): void {
|
||||
}
|
||||
|
||||
export const useExchangeRatesStore = defineStore('exchangeRates', () => {
|
||||
const settingsStore = useSettingsStore();
|
||||
|
||||
const latestExchangeRates = ref<LatestExchangeRates>(getExchangeRatesFromLocalStorage());
|
||||
|
||||
const exchangeRatesDataCacheEnabled = computed<boolean>(() => settingsStore.appSettings.exchangeRatesDataCacheExpiration >= 0);
|
||||
|
||||
const isUserCustomExchangeRates = computed((): boolean => {
|
||||
if (!latestExchangeRates.value || !latestExchangeRates.value.data) {
|
||||
return false;
|
||||
@@ -99,7 +105,7 @@ export const useExchangeRatesStore = defineStore('exchangeRates', () => {
|
||||
|
||||
latestExchangeRates.value.data.updateTime = updateTime;
|
||||
|
||||
if (changed) {
|
||||
if (exchangeRatesDataCacheEnabled.value && changed) {
|
||||
setExchangeRatesToLocalStorage(latestExchangeRates.value);
|
||||
}
|
||||
}
|
||||
@@ -120,7 +126,7 @@ export const useExchangeRatesStore = defineStore('exchangeRates', () => {
|
||||
}
|
||||
}
|
||||
|
||||
if (changed) {
|
||||
if (exchangeRatesDataCacheEnabled.value && changed) {
|
||||
setExchangeRatesToLocalStorage(latestExchangeRates.value);
|
||||
}
|
||||
}
|
||||
@@ -135,6 +141,29 @@ export const useExchangeRatesStore = defineStore('exchangeRates', () => {
|
||||
return new Blob([storageData]).size;
|
||||
}
|
||||
|
||||
function removeExpiredExchangeRates(removeDataInStore?: boolean): void {
|
||||
if (settingsStore.appSettings.exchangeRatesDataCacheExpiration > 0) {
|
||||
const currentExchangeRateData = latestExchangeRates.value;
|
||||
const now = getCurrentUnixTime();
|
||||
|
||||
if (currentExchangeRateData && currentExchangeRateData.time) {
|
||||
if (now - currentExchangeRateData.time >= settingsStore.appSettings.exchangeRatesDataCacheExpiration) {
|
||||
if (removeDataInStore) {
|
||||
resetLatestExchangeRates();
|
||||
} else {
|
||||
clearExchangeRatesFromLocalStorage();
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (settingsStore.appSettings.exchangeRatesDataCacheExpiration < 0) { // Disable Cache
|
||||
if (removeDataInStore) {
|
||||
resetLatestExchangeRates();
|
||||
} else {
|
||||
clearExchangeRatesFromLocalStorage();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function resetLatestExchangeRates(): void {
|
||||
latestExchangeRates.value = {};
|
||||
clearExchangeRatesFromLocalStorage();
|
||||
@@ -172,11 +201,13 @@ export const useExchangeRatesStore = defineStore('exchangeRates', () => {
|
||||
return;
|
||||
}
|
||||
|
||||
latestExchangeRates.value = {
|
||||
time: now,
|
||||
data: data.result
|
||||
};
|
||||
setExchangeRatesToLocalStorage(latestExchangeRates.value);
|
||||
if (exchangeRatesDataCacheEnabled.value) {
|
||||
latestExchangeRates.value = {
|
||||
time: now,
|
||||
data: data.result
|
||||
};
|
||||
setExchangeRatesToLocalStorage(latestExchangeRates.value);
|
||||
}
|
||||
|
||||
resolve(data.result);
|
||||
}).catch(error => {
|
||||
@@ -298,6 +329,7 @@ export const useExchangeRatesStore = defineStore('exchangeRates', () => {
|
||||
latestExchangeRateMap,
|
||||
// functions
|
||||
getExchangeRatesCacheSize,
|
||||
removeExpiredExchangeRates,
|
||||
resetLatestExchangeRates,
|
||||
getLatestExchangeRates,
|
||||
updateUserCustomExchangeRate,
|
||||
|
||||
@@ -316,6 +316,11 @@ export const useSettingsStore = defineStore('settings', () => {
|
||||
appSettings.value.mapCacheExpiration = value;
|
||||
}
|
||||
|
||||
function setExchangeRatesDataCacheExpiration(value: number): void {
|
||||
updateApplicationSettingsValue('exchangeRatesDataCacheExpiration', value);
|
||||
appSettings.value.exchangeRatesDataCacheExpiration = value;
|
||||
}
|
||||
|
||||
// Statistics Settings
|
||||
function setStatisticsDefaultChartDataType(value: number): void {
|
||||
updateApplicationSettingsSubValue('statistics', 'defaultChartDataType', value);
|
||||
@@ -539,6 +544,7 @@ export const useSettingsStore = defineStore('settings', () => {
|
||||
setCurrencySortByInExchangeRatesPage,
|
||||
// -- Browser Cache Settings
|
||||
setMapCacheExpiration,
|
||||
setExchangeRatesDataCacheExpiration,
|
||||
// -- Statistics Settings
|
||||
setStatisticsDefaultChartDataType,
|
||||
setStatisticsDefaultTimezoneType,
|
||||
|
||||
Reference in New Issue
Block a user