# @cachehub/upstash-redis-cache 中文文档

规范地址：https://npmcn.com/package/%40cachehub/upstash-redis-cache/

版本：1\.0\.2
npm 元数据更新时间：2025-07-05T03:50:51.752Z
中文译文更新时间：2026-11-08T23:01:38.186Z
软件包许可：未声明

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

原始 npm 页面：https://www.npmjs.com/package/%40cachehub/upstash-redis-cache
原文对照：https://npmcn.com/package/%40cachehub/upstash-redis-cache/?lang=original

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

- 包体积（解压后）：53.1 kB，17 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- 运行依赖：2 个
- 首次发布：2024-12-29T02:57:39.816Z
- 最新版本发布：2025-07-05T03:50:51.533Z
- 近 30 天下载量：14309（2026-10-09 至 2026-11-07，npm 官方下载统计）
- 已发布版本数：3（近 12 个月 0 个）

## 中文简介

Upstash Redis 缓存执行 CacheHub 有混合字符串和 TypeCacheKey 支持 .

## 安装

```sh
npm install @cachehub/upstash-redis-cache
```

也可以使用：`pnpm add @cachehub/upstash-redis-cache`、`yarn add @cachehub/upstash-redis-cache`、`bun add @cachehub/upstash-redis-cache`

## README

# @cachehub/upstash-redis-cache (英语)

Upstash Redis 缓存执行 CacheHub 有混合字符串和 TypeCacheKey 支持 .

## 安装

```bash
pnpm add @cachehub/upstash-redis-cache @upstash/redis
```

## 使用量

要在您的应用程序中使用 Upstash Redis 缓存适配器, 请遵循这些步骤 :

1.  导入 Upstash Redis 缓存包到工厂注册 。 例如:
    
    ```typescript
    import '@cachehub/upstash-redis-cache';
    ```
    
2.  使用工厂创建缓存实例 .
3.  使用缓存实例设置、获取、删除并检查缓存条目.

```typescript
import '@cachehub/upstash-redis-cache';
import { UpstashRedisCacheFactory } from '@cachehub/upstash-redis-cache';

const factory = new UpstashRedisCacheFactory();
const cache = await factory.createCache({
  CACHE_TYPE: 'upstash-redis-cache',
  REDIS_URL: process.env.REDIS_URL!,
  REDIS_TOKEN: process.env.REDIS_TOKEN!,
});

// Set and get 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');

// Set and get complex objects
interface User {
  id: number;
  name: string;
  email: string;
}

const user: User = {
  id: 1,
  name: 'John Doe',
  email: 'john@example.com'
};

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

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

// Basic cache operations
await cache.delete('key');
const exists = await cache.has('key');
```

### 类型缓存Key 支持

适配器支持高级情景的结构缓存密钥 :

```typescript
import type { TypeCacheKey } from '@cachehub/core';

// Using structured cache keys
const licenseKey: TypeCacheKey = {
  namespace: 'license',
  id: 'ABC-123',
  context: {
    action: 'activation',
    domain: 'example.com',
    ipAddress: '192.168.1.1'
  }
};

// Store with structured key
await cache.set(licenseKey, { token: 'xyz789', expires: Date.now() + 3600000 });

// Retrieve with structured key
const licenseData = await cache.get(licenseKey);

// Pattern deletion - delete all cache entries for license ABC-123
await cache.delete({
  namespace: 'license',
  id: 'ABC-123'
  // Omitting context deletes ALL entries matching namespace and id
});

// Delete all license entries
await cache.delete({
  namespace: 'license'
  // Omitting id deletes ALL entries in the namespace
});
```

### 模式删除支持

Upstash RedisCache 提供 : **本地图案删除** 使用 Redis SCAN 操作 :

**支持全部删除模式 :**

```typescript
// Exact key deletion with TypeCacheKey
await cache.delete({ 
  namespace: 'license', 
  id: 'ABC-123',
  context: { action: 'activation', domain: 'example.com' }
});

// Namespace + ID deletion (deletes all contexts for that ID)
await cache.delete({ 
  namespace: 'license', 
  id: 'ABC-123' 
});

// Partial context deletion (deletes all entries matching partial context)
await cache.delete({ 
  namespace: 'license', 
  context: { action: 'activation' }  // Deletes all activations regardless of ID
});

// Namespace-only deletion (deletes all entries in namespace)
await cache.delete({ 
  namespace: 'license' 
});

// String key deletion
await cache.delete('exact-string-key');
```

**如何运作:**

-   **红发SCAN**: 使用具有 MATCH 模式的本地 Redis SCAN 操作
-   **即时一致性**: 删除立即使用 DEL 命令完成
-   **高效剪接**: 将按100分批处理的密钥用于最佳性能
-   **本地重做操作**: 液晶 Redis 集束安全操作

### 密钥序列化

TypeCacheKey 对象自动被序列化以决定字符串 :

-   `{ namespace: 'license', id: '123' }` → `'license|123'`
-   `{ namespace: 'license', id: '123', context: { action: 'activation' } }` → `'license|123|action:activation'`
-   为一致性按字母顺序排序上下文密钥

## 配置

Upstash Redis 缓存需要以下配置:

```typescript
interface TypeUpstashRedisCacheConfig {
  /**
   * The Redis URL
   */
  REDIS_URL: string;

  /**
   * The Redis token
   */
  REDIS_TOKEN: string;

  /**
   * Optional prefix for cache keys
   */
  keyPrefix?: string;
}
```

## 测试

要运行测试,创建 `.env` 包含您的 Upstash Redis 证书的文件 :

```env
REDIS_URL=your_redis_url
REDIS_TOKEN=your_redis_token
```

然后运行 :

```bash
pnpm test
```

## 常见问题

### @cachehub/upstash-redis-cache 是什么？

Upstash Redis 缓存执行 CacheHub 有混合字符串和 TypeCacheKey 支持 \.

### 如何安装 @cachehub/upstash-redis-cache？

在项目目录运行 npm install @cachehub/upstash-redis-cache，或使用 pnpm add / yarn add / bun add。

### @cachehub/upstash-redis-cache 支持 TypeScript 吗？

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

### @cachehub/upstash-redis-cache 支持 ESM 还是 CommonJS？

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

### @cachehub/upstash-redis-cache 可以商用吗？

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

### @cachehub/upstash-redis-cache 的下载量有多少？

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

### @cachehub/upstash-redis-cache 还在维护吗？

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

