1 Star 0 Fork 0

会功夫的李白/origin-request

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Origin Request

一个轻量级的请求缓存和归源请求工具,用于确保同一时间内相同的请求只执行一次,并提供灵活的缓存管理功能。

特性

  • 🚀 归源请求:确保同一时间内相同的请求只执行一次
  • 💾 灵活的缓存管理:
    • 支持设置全局默认缓存时间
    • 支持为单个请求设置特定缓存时间
    • 支持设置永久缓存
  • 🧹 智能的缓存清理:
    • 自动清理过期缓存
    • 最小化资源占用
  • 🔒 类型安全:完整的 TypeScript 类型支持
  • 🎯 轻量级:零依赖,体积小巧

安装

npm install origin-request
# 或
yarn add origin-request

使用示例

基本使用

import { oReq } from 'origin-request';

// 模拟API请求
async function fetchUserData(userId: string) {
  return oReq(
    async () => {
      const response = await fetch(`/api/users/${userId}`);
      return response.json();
    },
    {
      key: `user-${userId}`, // 可选:指定唯一键
      expireTime: 5 * 60 * 1000 // 可选:设置缓存时间(5分钟)
    }
  );
}

// 多次调用同一请求,实际只会执行一次
const user1 = await fetchUserData('123');
const user2 = await fetchUserData('123'); // 从缓存中获取,不会重复请求

按需使用

async function fetchUserData(userId: string) {
  // 请求...
}

// 多次调用同一请求,实际只会执行一次
const user1 = await oReq(fetchUserData)
const user2 = await oReq(fetchUserData) // 从缓存中获取,不会重复请求

设置全局缓存时间

import { OriginRequest } from 'origin-request';

// 设置全局默认缓存时间为10分钟
OriginRequest.setDefaultCacheExpireTime(10 * 60 * 1000);

// 设置永久缓存
OriginRequest.setDefaultCacheExpireTime(null);

手动管理缓存

import { OriginRequest } from 'origin-request';

// 清除特定请求的缓存
OriginRequest.clearRequestCache('user-123');

// 清除所有请求缓存
OriginRequest.clearAllRequestCache();

API 文档

originRequest

归源请求函数,确保同一时间内相同的请求只执行一次。

function originRequest<T>(
  requestFn: () => Promise<T>,
  options?: OriginRequestOptions
): Promise<T>

参数:

  • requestFn: 要执行的请求函数
  • options: 配置选项
    • key: 请求的唯一键,用于标识相同请求
    • expireTime: 缓存有效期(毫秒),null 表示永不过期

setDefaultCacheExpireTime

设置全局默认缓存有效期。

function setDefaultCacheExpireTime(expireTime: number | null): void

参数:

  • expireTime: 缓存有效期(毫秒),null 表示永不过期

clearRequestCache

清除特定请求的缓存。

function clearRequestCache(key: string | number | symbol): boolean

参数:

  • key: 要清除的缓存键

clearAllRequestCache

清除所有请求缓存。

function clearAllRequestCache(): void

缓存清理策略

该库使用智能的缓存清理机制,具有以下特点:

  1. 按需清理

    • 在访问缓存项时检查过期
    • 设置新缓存项时检查是否已过期
    • 避免不必要的清理操作
  2. 智能调度

    • 记录最近的过期时间
    • 只在必要时设置清理定时器
    • 避免频繁的定时器操作
  3. 资源优化

    • 最小化定时器使用
    • 避免内存泄漏
    • 自动清理过期资源

注意事项

  1. 如果不提供 key,将使用请求函数本身或函数签名作为键
  2. 对于匿名函数,建议提供 key 以确保正确识别相同请求
  3. 缓存时间从请求执行完成时开始计算
  4. 清理操作是异步的,不会阻塞主线程

贡献

欢迎提交 Issue 和 Pull Request!

许可证

MIT

MIT License Copyright (c) 2025 会功夫的李白 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

一个前端请求增强工具 展开 收起
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/jl15988/origin-request.git
git@gitee.com:jl15988/origin-request.git
jl15988
origin-request
origin-request
master

搜索帮助