support setting exchange rate cache expiration time

This commit is contained in:
MaysWind
2026-02-28 21:36:00 +08:00
parent 247181830c
commit 8192a48bc5
27 changed files with 303 additions and 30 deletions
+39 -7
View File
@@ -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,
+6
View File
@@ -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,