关于接口时间加载过长,数据单独后台预加载

为解决接口加载时间过长问题,本文提出一种预加载策略。页面加载时获取24小时内的数据,并在后台提前加载最近3天的全部数据。当用户查看前一天数据时,从缓存中获取并同时更新前一天的前一天数据,保持缓存内始终有3天完整数据。由于数据时间不精确,参数处理时进行了几分钟的修正。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

思路:
当前页面加载24小时数据,加载的时调用方法加载连续3天的数据,查看前一天的数据时,先去缓存中取数据,触发加载前一天的上一天的数据,同时把缓存中的昨天的数据清理掉,始终保持缓存中有3天的数据。由于我的数据时间比一定正好所以,参数时间两头减了几分钟。
创建js文件:

import {
  getDataByTime
} from '@/api/api'
import { getDateStr } from '@/utils/util'

var ChcheData = []
var m_jh = ''
export async function getChcheData(jh, starTime, endTime, selectedConfigureKey) {
  if (getPlusMinutesData(starTime, 1000 * 60 * 5) > new Date()) {
    return null
  }
  console.log('准备进缓存取数据')
  console.log( starTime, endTime,selectedConfigureKey)
  if (m_jh != '' && m_jh == jh) {
    let returnData=[]
    if(ChcheData.length>0){
      returnData= ChcheData.filter(result => new Date(result.time) >= new Date(starTime) && new Date(result.time) <= new Date(endTime))
    }
    if (returnData.length > 0) {
      if (new Date(ChcheData[0].time) <= getPlusMinutesData(starTime, 1000 * 60 * 10) && getCutMinutesData(endTime, 1000 * 60 * 10) <= new Date(getPlusDate(ChcheData[0].time, 24))) {
         fistData(jh, getCutData(ChcheData[0].time, 24), ChcheData[0].time, selectedConfigureKey)
      } else if (new Date(getCutData(ChcheData[ChcheData.length - 1].time,24)) <= getPlusMinutesData(starTime, 1000 * 60 * 10) && getCutMinutesData(endTime, 1000 * 60 * 10) <= new Date(getPlusDate(ChcheData[ChcheData.length - 1].time, 24))) {
        lastData(jh, ChcheData[ChcheData.length - 1].time, getPlusDate(ChcheData[ChcheData.length - 1].time, 24), selectedConfigureKey)
      }
      return returnData
    } else {
      if (ChcheData.length != 0) {
       getInitData(jh, starTime, endTime, selectedConfigureKey)
      }
      return null
    }
  } else {
    getInitData(jh, starTime, endTime, selectedConfigureKey)
    m_jh = jh
    return null
  }
}
function getPlusDate(date, number) {//加小时
  return getDateStr(new Date(new Date(date).getTime() + 1000 * 60 * 60 * number))
}

function getCutData(date, number) {//减小时
  return getDateStr(new Date(new Date(date).getTime() - 1000 * 60 * 60 * number))
}

function getPlusMinutesData(date, number) {//加分钟
  return new Date(new Date(date).getTime() + number)
}

function getCutMinutesData(date, number) {//减分钟
  return new Date(new Date(date).getTime() - number)
}
 async function  getInitData(jh, starTime, endTime, selectedConfigureKey) {
  let f_time=getCutData(starTime,24)
  let t_time=getPlusDate(endTime,24)
   let params = { }

   params = { jh: jh, time1:f_time, time2: getPlusDate(starTime,12), templateId: selectedConfigureKey }
   getDataByTime(params).then((res) => {
     if (res.success && res.result.length > 0) {
       ChcheData=[...res.result,...ChcheData]
     }
   })
   params = { jh: jh, time1:getCutData(endTime,12), time2: t_time, templateId: selectedConfigureKey }
   getDataByTime(params).then((res) => {
     if (res.success && res.result.length > 0) {
       ChcheData=[...ChcheData,...res.result]
     }
   })
}
 function fistData(jh, starTime,endTime, selectedConfigureKey) {
  let params = { jh: jh, time1: starTime, time2: endTime, templateId: selectedConfigureKey }
         getDataByTime(params).then((res) => {
    if (res.success && res.result.length > 0) {
      ChcheData = [...res.result, ...ChcheData].filter(res => new Date(res.time) <= getCutMinutesData(ChcheData[ChcheData.length - 1].time, 1000 * 60 * 60 * 24))
    }
  })
}

async function lastData(jh, starTime,endTime, selectedConfigureKey) {
  let params = { jh: jh, time1: starTime, time2: endTime, templateId: selectedConfigureKey }
  getDataByTime(params).then((res) => {
    if (res.success && res.result.length > 0) {
      ChcheData = [...ChcheData,...res.result].filter(res => new Date(res.time) >= getPlusMinutesData(ChcheData[0].time, 1000 * 60 * 60 * 24))
    }
  })
}


function getDateStr(date) {
  let year = date.getFullYear()
  let month = date.getMonth() + 1
  let day = date.getDate()
  let h = date.getHours()
  let m = date.getMinutes()
  let s = date.getSeconds()
  if (month < 10) {
    month = '0' + month
  }
  if (day < 10) {
    day = '0' + day
  }
  if (h < 10) {
    h = '0' + h
  }
  if (m < 10) {
    m = '0' + m
  }
  if (s < 10) {
    s = '0' + s
  }
  return year + '-' + month + '-' + day + ' ' + h + ':' + m + ':' + s
}
调用:
import {getChcheData } from './cacheData'
let  res=await getChcheData( this.jh,  this.is_back_boolean ? this.getCutData(this.jsonTimeLastTime,24) : this.jsonTimeLastTime,this.is_back_boolean ? this.jsonTimeLastTime : this.getPlusDate(this.jsonTimeLastTime,24),this.selectedConfigureKey,)
if(res && res.length>0){//缓存数据
  this.ChcheData(res)//取得数据

}else {
//没取得数据
  await this.getDataByTime( this.jh, this.selectedConfigureKey, this.time1 ,this.time2).then((res) => {
  
  })
  
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值