From f11c119f43e9a31f042a5f825bb1aaeb0cf85586 Mon Sep 17 00:00:00 2001 From: liangping <18786721@qq.com> Date: Fri, 28 Oct 2022 14:10:03 +0800 Subject: [PATCH] add autoclean storage --- src/libs/fetch.js | 15 +++++++++++++-- src/views/Uptime.vue | 8 ++++---- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/libs/fetch.js b/src/libs/fetch.js index 1a455426..5a51a28b 100644 --- a/src/libs/fetch.js +++ b/src/libs/fetch.js @@ -185,8 +185,19 @@ export default class ChainFetch { async getValidatorList(config = null) { return this.get('/cosmos/staking/v1beta1/validators?pagination.limit=200&status=BOND_STATUS_BONDED', config).then(data => { const vals = commonProcess(data.validators).map(i => new Validator().init(i)) - // disable cache - // localStorage.setItem(`validators-${this.config.chain_name}`, JSON.stringify(vals)) + try { + localStorage.setItem(`validators-${this.config.chain_name}`, JSON.stringify(vals)) + } catch (err) { + // clear cache + for (let i = 0; i < localStorage.length; i += 1) { + const key = localStorage.key(i) + if (key.startsWith('validators')) { + localStorage.removeItem(key) + } + } + // set again + localStorage.setItem(`validators-${this.config.chain_name}`, JSON.stringify(vals)) + } return vals }) } diff --git a/src/views/Uptime.vue b/src/views/Uptime.vue index 1d754f96..19e81374 100644 --- a/src/views/Uptime.vue +++ b/src/views/Uptime.vue @@ -279,10 +279,10 @@ export default { }, }, created() { - // const cached = JSON.parse(getCachedValidators(this.$route.params.chain)) - // if (cached) { - // this.validators = cached - // } + const cached = JSON.parse(getCachedValidators(this.$route.params.chain)) + if (cached) { + this.validators = cached + } this.$http.getValidatorList().then(res => { this.validators = res })