@cachehub/core

中文文档
1.0.2发布于 2025/07/05TypeScriptES Module零依赖近 30 天下载 2.1万

概览 缓存库的核心包提供了创建缓存执行的基础界面和厂房模式. 它定义了所有缓存适配器必须遵守的合同,确保互动智慧的API一致

中文使用文档
文档目录10 节
  1. 概览
  2. 福利
  3. 接口
  4. 快取界面
  5. 快取 Factory 界面
  6. 使用量
  7. 示例
  8. 一般类型使用
  9. 文件夹结构
  10. 捐款

核心缓存库

概览

缓存库的核心包提供了创建缓存执行的基础界面和厂房模式. 它定义了所有缓存适配器必须遵守的合同,确保了与不同缓存策略互动的一致API.

福利

  • 统一接口:所有缓存执行都符合相同的接口,使其之间容易切换.
  • 工厂模式:工厂模式允许基于配置的动态创建缓存实例.

接口

快取界面

这个 CacheInterface 定义所有缓存执行必须执行的方法:

  • set(key: string, value: TypeValue, expiresAfter?: number): Promise<boolean>
  • get(key: string): Promise<TypeValue | undefined>
  • delete(key: string): Promise<boolean>
  • has(key: string): Promise<boolean>

快取 Factory 界面

这个 CacheFactoryInterface 定义创建缓存实例的方法 :

  • createCache(config: TypeConfig): Promise<CacheInterface>

使用量

为了使用这些核心职能,遵循这些步骤:

  1. 从核心包中导入必要的接口和工厂.
  2. 通过扩展自定义缓存适配器执行 CacheInterface。 。 。 .
  3. 创建执行 CacheFactoryInterface。 。 。 .

示例

import { CacheInterface, CacheFactoryInterface } from '@cachehub/core';

class MyCustomCache implements CacheInterface {
  // Implement the required methods: set, get, delete, has
}

class MyCustomCacheFactory implements CacheFactoryInterface {
  public async createCache(config: any): Promise<CacheInterface> {
    return new MyCustomCache();
  }
}

让你的包裹自我登记 确保你的包裹 与工厂登记 CacheFactoryRegistry 并导出工厂类作为默认导出单位 index.ts。例如:

import { CacheFactoryRegistry } from '@cachehub/core';
import { MyCustomCacheFactory } from './Factory';

CacheFactoryRegistry.registerCacheFactory('my-custom-cache', MyCustomCacheFactory);

// default export so that core factory can use it directly
export default MyCustomCacheFactory;

一般类型使用

CacheInterface 支持用于类型安全存储和取值的通用类型. 以下是使用缓存方法的通用名的例子:

// Using primitive types
await cache.set<string>('stringKey', 'value');
const stringValue = await cache.get<string>('stringKey');

await cache.set<number>('numberKey', 42);
const numberValue = await cache.get<number>('numberKey');

await cache.set<boolean>('booleanKey', true);
const booleanValue = await cache.get<boolean>('booleanKey');

// Using complex objects
interface User {
  id: number;
  name: string;
  email: string;
}

const user: User = {
  id: 1,
  name: 'John Doe',
  email: '[email protected]'
};

await cache.set<User>('userKey', user);
const userValue = await cache.get<User>('userKey');

// Using arrays
await cache.set<string[]>('arrayKey', ['a', 'b', 'c']);
const arrayValue = await cache.get<string[]>('arrayKey');

在执行自定义缓存适配器时,确保在执行中妥善处理通用类型,在整个应用程序中保持类型安全.

文件夹结构

每个软件包的文件夹结构通常包括:

  • src/:包含包的源代码,包括:
    • Factory/:包含用于创建缓存处理器的工厂类.
    • Cache/: 包含执行缓存对象的类.
    • Type/: 包含类型定义和接口.
  • tests/:包含包件的单位测试.
  • README.md: 包的文档.
  • .env: 配置的环境变量.

捐款

欢迎捐款! 随意叉出这个寄存器并提交拉取请求. 在提交之前,请确保您的代码通过所有起伏和单元测试.

您可以使用 :

pnpm format

您可以使用 :

pnpm test

版本与发布记录

@cachehub/core 共发布 3 个版本,首次发布于 2024/12/29;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.2latest2025/07/051.3 年前
1.0.12024/12/301.9 年前
1.0.02024/12/291.9 年前

关于 @cachehub/core 的常见问题

@cachehub/core 是什么?

概览 缓存库的核心包提供了创建缓存执行的基础界面和厂房模式. 它定义了所有缓存适配器必须遵守的合同,确保互动智慧的API一致

如何安装 @cachehub/core?

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

@cachehub/core 支持 TypeScript 吗?

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

@cachehub/core 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@cachehub/core 可以商用吗?

@cachehub/core 使用 未声明 许可。package.json 没有声明许可证。没有许可证的代码默认保留全部权利,商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括,不构成法律意见,请以许可原文为准。

@cachehub/core 的下载量有多少?

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

@cachehub/core 还在维护吗?

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