# @cryptotaxcalculator/axios-cached-dns-resolve 中文文档

规范地址：https://npmcn.com/package/%40cryptotaxcalculator/axios-cached-dns-resolve/

版本：4\.0\.0
npm 元数据更新时间：2024-05-23T02:55:19.873Z
中文译文更新时间：2026-11-11T03:03:09.556Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40cryptotaxcalculator/axios-cached-dns-resolve
原文对照：https://npmcn.com/package/%40cryptotaxcalculator/axios-cached-dns-resolve/?lang=original
项目主页：https://github.com/cryptotaxcalculator/axios-cached-dns-resolve#readme
源码仓库：https://github.com/cryptotaxcalculator/axios-cached-dns-resolve

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：28 kB，11 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- Node.js 要求：\>=14\.0\.0
- 运行依赖：5 个
- 首次发布：2024-05-01T07:13:49.816Z
- 最新版本发布：2024-05-23T02:55:19.684Z
- 近 30 天下载量：4391（2026-10-11 至 2026-11-09，npm 官方下载统计）
- 已发布版本数：6（近 12 个月 0 个）

## 中文简介

在CJS中以Axios-cached-dns-resolve为叉并带有类型安全. Caches dns 分辨率用 Async dns.resolve 而不是默认同步 dns. lookup, 在背景中刷新

## 安装

```sh
npm install @cryptotaxcalculator/axios-cached-dns-resolve
```

也可以使用：`pnpm add @cryptotaxcalculator/axios-cached-dns-resolve`、`yarn add @cryptotaxcalculator/axios-cached-dns-resolve`、`bun add @cryptotaxcalculator/axios-cached-dns-resolve`

## README

# @cryptotax计算器/xios-cached-dns-resolve

这个图书馆是原作的叉子 `axios-cached-dns-resolve` 软件包,但添加/更改如下:

-   添加类型安全
-   将模块分辨率改为CJS(从无害环境管理)
-   更改测试跑道从 `ava` 改为 `jest`
-   添加对 Redis 的支持作为缓存

关于完整的更改清单,见 [变化](https://npmcn.com/package/%40cryptotaxcalculator/axios-cached-dns-resolve/CHANGELOG.md)。 。 。 .

* * *

Axios 使用 node.js dns. lookup 来解决主机名称. dns. lookup是同步的,在有限的libuv线程池上执行. 每个轴请求将解析 kubernetes 中的 dns 名称, 开班, 以及云环境, 有意将 TTL 设置为低或为 0 以快速动态更新 . Dns 解析器可以被负载所淹没. DNS 分辨率中存在/ 存在一个错误, 它显示为非常长 dns. lookups in node.js.

从库伯内特 [文档](https://kubernetes.io/docs/concepts/services-networking/service/#why-not-use-round-robin-dns)

```
Even if apps and libraries did proper re-resolution, the load of every client re-resolving DNS over and over would be difficult to manage.
```

这个库使用dns.resolve,并且可以在地址中可选择性地缓存分辨率和圆旋. 缓存大小可配置. 如果启用缓存, 背景线程会用 dns.resolve 而不是每个请求来定期刷新分辨率 . 有一种闲置的 TTL 如果一个地址不再使用, 则驱逐背景更新 . 这个自由代理通过OS解析机制,可以提供进一步的缓存.

## 目标

-   Async 请求 - dns 解析 vs 查找
-   快速 - 本地内存缓存搜索
-   新鲜 - 定期(经常)更新
-   恒定 DNS 负载/纬度与随机负载/可变纬度
-   提供统计和回顾

## 所需资源

节点14+

## 开始

```console
npm i -S @cryptotaxcalculator/axios-cached-dns-resolve
```

# 使用量

```javascript
import { registerInterceptor } from "@cryptotaxcalculator/axios-cached-dns-resolve";

const axiosClient = axios.create(config);

registerInterceptor(axiosClient);
```

使用 arxiosclient 为普通

## 配置

```javascript
export const config = {
  disabled: process.env.AXIOS_DNS_CACHE_DISABLE === "true",
  dnsTtlMs: parseInt(process.env.AXIOS_DNS_CACHE_TTL_MS || "5000"), // when to refresh actively used dns entries (5 sec)
  cacheGraceExpireMultiplier: parseInt(
    process.env.AXIOS_DNS_CACHE_EXPIRE_MULTIPLIER || "2"
  ), // maximum grace to use entry beyond TTL
  dnsIdleTtlMs:
    parseInt(process.env.AXIOS_DNS_CACHE_IDLE_TTL_MS || "1000") * 60 * 60, // when to remove entry entirely if not being used (1 hour)
  backgroundScanMs: parseInt(
    process.env.AXIOS_DNS_CACHE_BACKGROUND_SCAN_MS || "2400"
  ), // how frequently to scan for expired TTL and refresh (2.4 sec)
  dnsCacheSize: parseInt(process.env.AXIOS_DNS_CACHE_SIZE || "100"), // maximum number of entries to keep in cache
  // pino logging options
  logging: {
    name: "axios-cache-dns-resolve",
    // enabled: true,
    level: process.env.AXIOS_DNS_CACHE_LOG_LEVEL || "info", // default 'info' others trace, debug, info, warn, error, and fatal
    // timestamp: true,
    prettyPrint: process.env.NODE_ENV === "DEBUG" || false,
    formatters: {
      level: (label: string) => {
        return { level: label };
      },
    },
  },
  redisConfig:
    process.env.AXIOS_DNS_CACHE_USE_REDIS === "true"
      ? {
          url: process.env.AXIOS_DNS_CACHE_REDIS_URL || "localhost:6379",
          password: process.env.AXIOS_DNS_CACHE_REDIS_PASSWORD,
          ttl: parseInt(process.env.AXIOS_DNS_CACHE_REDIS_TTL || "5"), // default 5 seconds
        }
      : undefined,
  cache: undefined as LRUCache<string, DnsEntry> | undefined,
} as Config;

config.lruCacheConfig = {
  max: config.dnsCacheSize,
  ttl: config.dnsTtlMs * config.cacheGraceExpireMultiplier, // grace for refresh
} as LRUCacheConfig;
```

## 统计

可通过下列渠道获得统计数据:

```javascript
getStats()

{
  "dnsEntries": 4,
  "refreshed": 375679,
  "hits": 128689,
  "misses": 393,
  "idleExpired": 279,
  "errors": 0,
  "lastError": 0,
  "lastErrorTs": 0
}

AND

getDnsCacheEntries()

[
  {
    "host": "foo-service.domain.com",
    "ips": [
      "51.210.235.165",
      "181.73.135.40"
    ],
    "nextIdx": 1,
    "lastUsedTs": 1604151366910,
    "updatedTs": 1604152691039
  },
  ...
]
```

### 特快统计

```javascript
import {
  getStats,
  getDnsCacheEntries,
} from "@cryptotaxcalculator/axios-cached-dns-resolve";

router.get("/axios-dns-cache-statistics", getAxiosDnsCacheStatistics);

function getAxiosDnsCacheStatistics(req, resp) {
  resp.json(getStats());
}

router.get("/axios-dns-cache-entries", getAxiosDnsCacheEntries);

function getAxiosDnsCacheEntries(req, resp) {
  resp.json(getDnsCacheEntries());
}
```

## 常见问题

### @cryptotaxcalculator/axios-cached-dns-resolve 是什么？

在CJS中以Axios-cached-dns-resolve为叉并带有类型安全\. Caches dns 分辨率用 Async dns\.resolve 而不是默认同步 dns\. lookup, 在背景中刷新

### 如何安装 @cryptotaxcalculator/axios-cached-dns-resolve？

在项目目录运行 npm install @cryptotaxcalculator/axios-cached-dns-resolve，或使用 pnpm add / yarn add / bun add。

### @cryptotaxcalculator/axios-cached-dns-resolve 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @cryptotaxcalculator/axios-cached-dns-resolve 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### @cryptotaxcalculator/axios-cached-dns-resolve 需要什么版本的 Node\.js？

package\.json 的 engines 字段要求 Node\.js \>=14\.0\.0。版本不符时 npm 默认只给出警告，开启 engine-strict 后会拒绝安装。

### @cryptotaxcalculator/axios-cached-dns-resolve 可以商用吗？

@cryptotaxcalculator/axios-cached-dns-resolve 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @cryptotaxcalculator/axios-cached-dns-resolve 的下载量有多少？

根据 npm 官方下载统计，@cryptotaxcalculator/axios-cached-dns-resolve 在 2026/10/11 – 2026/11/09 这 30 天内被下载 4,391 次，日均约 146 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @cryptotaxcalculator/axios-cached-dns-resolve 还在维护吗？

最新版本 4\.0\.0 发布于 2024/05/23（2 年前）；至今共发布 6 个版本，其中过去 12 个月发布了 0 个；首次发布于 2024/05/01。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

