@cachehub/upstash-redis-cache (英语)
Upstash Redis 缓存执行 CacheHub 有混合字符串和 TypeCacheKey 支持 .
安装
pnpm add @cachehub/upstash-redis-cache @upstash/redis
使用量
要在您的应用程序中使用 Upstash Redis 缓存适配器, 请遵循这些步骤 :
- 导入 Upstash Redis 缓存包到工厂注册 。 例如:
import '@cachehub/upstash-redis-cache'; - 使用工厂创建缓存实例 .
- 使用缓存实例设置、获取、删除并检查缓存条目.
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: '[email protected]'
};
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 支持
适配器支持高级情景的结构缓存密钥 :
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 操作 :
支持全部删除模式 :
// 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 缓存需要以下配置:
interface TypeUpstashRedisCacheConfig {
/**
* The Redis URL
*/
REDIS_URL: string;
/**
* The Redis token
*/
REDIS_TOKEN: string;
/**
* Optional prefix for cache keys
*/
keyPrefix?: string;
}
测试
要运行测试,创建 .env 包含您的 Upstash Redis 证书的文件 :
REDIS_URL=your_redis_url
REDIS_TOKEN=your_redis_token
然后运行 :
pnpm test