JS实现并发控制、请求合并、数据缓存

引言

在现代前端开发中,随着应用复杂度增加,接口请求管理成为性能优化的关键点。本文将深入探讨如何通过并发控制、数据缓存和请求合并等技术手段,构建高效、稳定的大规模请求解决方案。

并发控制

1.1 问题背景

当页面同时发起大量请求时,可能导致:

  • 浏览器TCP连接数达到上限(Chrome同一域名下默认6个)
  • 服务器压力剧增
  • 低优先级请求阻塞关键请求

1.2 解决方案

使用请求队列实现控制并发

js 复制代码
class RequestQ {
  private _running;
  private max;
  private queue: ((value: unknown) => void)[];
  constructor(max: number) {
    this.max = max;
    this.queue = [];
    this._running = 0;
  }

  async add(fn: () => Promise<any>) {
    if (this._running >= this.max) {
      await new Promise((resolve) => this.queue.push(resolve));
    }
    this._running++;

    try {
      await fn();
    } finally {
      this._running--;
      if (this.queue.length > 0) {
        this.queue.shift()!();
      }
    }
  }
}

这里主要使用Promise来控制任务的并发,当当前运行的个数大于max时,会挂起一个Promise等待它的resolve,这里是实现并发控制的关键。

请求合并

js 复制代码
class RequestMerge {
  private pending;

  constructor() {
    this.pending = new Map();
  }

  async request(key: string, fn: Function) {
    if (this.pending.get(key)) {
      return this.pending.get(key);
    }
    const promise = fn().finally(() => {
      this.pending.delete(key);
    });
    this.pending.set(key, promise);
    return promise;
  }
}

使用一个Map用于储存正在请求的网络请求,当相同请求来的时候,先在Map里去找是否有相同key的值,如果有直接返回,没有在去请求。

数据缓存

js 复制代码
class CacheRequest {
  private apiCache: Map<string, any>;

  constructor() {
    this.apiCache = new Map<string, any>();
    setInterval(() => {
      this.apiCache.forEach((value, key) => {
        if (Date.now() > value.expireTime) {
          this.apiCache.delete(key);
        }
      });
    }, 60000);
  }

  async request(key: string, fn: () => Promise<any>) {
    if (this.apiCache.has(key)) {
      return this.apiCache.get(key);
    }
    const expireTime = Date.now() + 300000;

    // eslint-disable-next-line no-useless-catch
    try {
      const result = await fn();
      this.apiCache.set(key, { result, expireTime });
      return result;
    } catch (error) {
      throw error;
    }
  }
}

类似与合并请求,不同的是这里是启动一个定时器,定期的去清理过期的key

相关推荐
Hello--_--World9 分钟前
React:useRef 超详细教程、forwardRef 详解、useImperativeHandle详解
前端·javascript·react.js
xuankuxiaoyao14 分钟前
vue.js 课程自己编写小游戏
前端·javascript·vue.js
兔子零102417 分钟前
当 Codex 成为主力,软件工程的重心已经变了
前端·后端·架构
牛奶27 分钟前
网关是怎么当"门卫"的?
前端·后端·负载均衡
上海合宙LuatOS31 分钟前
合宙TCP/UDP web测试工具简介
前端·物联网·tcp/ip·udp·luatos
Apifox.1 小时前
Apifox 近期更新|AI Agent Debugger、A2A Debugger、Postman API 导入、Ask AI 侧边栏对话
前端·人工智能·后端·测试工具·测试用例·postman
invicinble1 小时前
前端框架使用vue-cli (第五层:构建打包层--babel.config.js介绍)
前端·vue.js·前端框架
张元清1 小时前
React 浏览器标签页 UX:用标题、Favicon 和通知把用户拉回来
前端·javascript·面试
前端老石人1 小时前
与 CSS 的一次美丽邂逅
前端·css
lovemiss1 小时前
拒绝手动艾特!我用 50 行 JS 实现 npm publish 后钉钉全自动“战报”推送
前端