@bondsports/cache

中文文档
1.1.10发布于 2025/08/24ISCTypeScriptCommonJS近 30 天下载 2039

Cache module for Bond-Sports

原始使用文档

Cache

Cache is a module to encapsulate generic caching functionality under simple interface

It provides two interfaces BaseCache and SimpleCache

Both were designed to be used to cache plain javascript objects containing id:number field for identification

BaseCache is a low level one to have more control over the process.

SimpleCache is better suited if you want to have separate cache holders for different kind of objects

Usage

BaseCache

import { BaseCache, MemoryStore } from '@bondsports/cache';

const cache = new BaseCache(new MemoryStore(), console.log, true);

const program = cache.getOrFetchOne('programs', 12, async missingId => ({ id: 12, name: 'program 12' }));

const programs = cache.getOrFetch('programs', [12, 13], async missingIds => [
    { id: 12, name: 'program 12' },
    { id: 13, name: 'program 13' },
]);

cache.invalidateOne('programs', 12);
cache.invalidate('programs', [13]);

SimpleCache

import { BaseCache, MemoryStore } from '@bondsports/cache';

const cache = new SimpleCache(new MemoryStore(), 'programs', async missingIds => [
    { id: 12, name: 'program 12' },
    { id: 13, name: 'program 13' },
]);

const program = cache.getOrFetchOne(12);

const programs = cache.getOrFetch([12, 13]);

cache.invalidateOne(12);
cache.invalidateAll();

SimpleCache with Redis as a store

import { RedisClientType, createClient } from '@redis/client';
import { RedisStore } from '@bondsports/cache';

const client = createClient({ url: 'redis://localhost:6379' });
const store = new RedisStore(client, 1000);
const cache = new SimpleCache(store, 'programs', async missingIds => [
    { id: 12, name: 'program 12' },
    { id: 13, name: 'program 13' },
]);

const program = cache.getOrFetchOne(12);

const programs = cache.getOrFetch([12, 13]);

cache.invalidateAll();

SimpleCache with Redis as a store

import { RedisClientType, createClient } from '@redis/client';
import { RedisStore } from '@bondsports/cache';

const client = createClient({ url: 'redis://localhost:6379' });
const store = new RedisStore(client, 1000);
const cache = new SimpleCache(store, 'programs', async missingIds => [
    { id: 12, name: 'program 12' },
    { id: 13, name: 'program 13' },
]);

const program = cache.getOrFetchOne(12);

const programs = cache.getOrFetch([12, 13]);

版本与发布记录

@bondsports/cache 共发布 25 个版本,首次发布于 2022/11/17;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.1.10latest2025/08/241.2 年前
1.1.92025/08/201.2 年前
1.1.82025/08/181.2 年前
1.1.72024/10/102 年前
1.1.62024/09/302 年前
1.1.4-ido-test2024/09/302 年前

其他发布通道:ido → 1.0.4-ido。

关于 @bondsports/cache 的常见问题

@bondsports/cache 是什么?

Cache module for Bond-Sports

如何安装 @bondsports/cache?

在项目目录运行 npm install @bondsports/cache,或使用 pnpm add / yarn add / bun add。

@bondsports/cache 支持 TypeScript 吗?

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

@bondsports/cache 支持 ESM 还是 CommonJS?

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

@bondsports/cache 可以商用吗?

@bondsports/cache 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@bondsports/cache 的下载量有多少?

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

@bondsports/cache 还在维护吗?

最新版本 1.1.10 发布于 2025/08/24(1.2 年前);至今共发布 25 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/11/17。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。