内存缓存适配器
概览
内存缓存适配器提供了缓存库的内存执行. 这个适配器适合开发环境,测试,或需要轻量级,过程-局部缓存的情景. 它将数据保存在内存中,使其在应用程序重启时速度极快但非持久性.
福利
- 快速访问:所有数据被存储在内存中,提供尽可能快的访问时间.
- 无依赖: 不需要外部服务或基础设施.
- 类型安全: 完整的类型脚本支持,并附有值的通用类型参数.
- 自动清理:已过期的条目在访问时被自动删除.
- 混合密钥支持: 支持字符串密钥和结构化的 TypeCacheKey 对象用于高级缓存管理.
- JavaScript 模式删除: 使用高效的JavaScript过滤方式,通过图案删除多个缓存条目.
- 即时一致性:所有业务都立即与即时缓存更新保持一致.
安装
要使用内存缓存适配器,请使用以下命令安装包: 1
pnpm add @cachehub/memory-cache
配置
要配置内存缓存适配器,请在配置中设置如下:
CACHE_TYPE: 设定为memory-cache以使用内存缓存适配器.
使用量
要在应用程序中使用内存缓存适配器,请遵循这些步骤:
- 导入内存缓存套件,以便在工厂注册. 例如:
import '@cachehub/memory-cache'; - 使用工厂创建缓存实例 .
- 使用缓存实例设置、获取、删除并检查缓存条目.
示例
import '@cachehub/memory-cache';
import { CacheFactoryFactory } from '@cachehub/core';
const factory = new CacheFactoryFactory();
const cache = await factory.createCache({
CACHE_TYPE: 'memory-cache'
});
// Using primitive types
await cache.set<string>('stringKey', 'myValue', 3600);
const stringValue = await cache.get<string>('stringKey');
await cache.set<number>('numberKey', 42, 3600);
const numberValue = await cache.get<number>('numberKey');
await cache.set<boolean>('booleanKey', true, 3600);
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');
// Basic cache operations
const exists = await cache.has('myKey');
const deleted = await cache.delete('myKey');
const existsAfterDelete = await cache.has('myKey');
// Type-safe cache creation
const userCache = await factory.createCache<User>({
CACHE_TYPE: 'memory-cache'
});
await userCache.set('user:1', { id: 1, name: 'John' });
const retrievedUser = await userCache.get('user:1');
console.log(retrievedUser?.name); // Output: 'John'
类型缓存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
});
模式删除支持
内存缓存提供 本地图案删除 使用 JavaScript 过滤以立即保持一致 :
支持全部删除模式 :
// 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');
如何运作:
- Java 脚本过滤: 使用地图迭代和图案匹配
- 立即处理: 所有操作立即完成内存
- 即时一致性: 所有删除都是即时的(无自动操作)
- 高效匹配: 优化模拟数据匹配模式
删除模式 :
- ✅ 仅命名空格数字 :
{ namespace: 'license' }- 删除命名空间中的所有条目 - ✅ 命名空间 + ID数字 :
{ namespace: 'license', id: 'ABC-123' }- 删除所有标识上下文 - ✅ 部分内容数字 :
{ namespace: 'license', context: { action: 'activation' } }- 删除匹配的上下文 - ✅ 精确删除: 完整上下文的完整类型CacheKey
- ✅ 字符串删除: 直接字符串密钥
密钥序列化
TypeCacheKey 对象自动被序列化以决定字符串 :
{ namespace: 'license', id: '123' }→'license|123'{ namespace: 'license', id: '123', context: { action: 'activation' } }→'license|123|action:activation'- 为一致性按字母顺序排序上下文密钥
重要说明
- 该缓存被存储在内存中,并在应用程序重启时被清除.
- 每个缓存实例都维持自己的独立存储.
- 内存用量随着缓存项的数量而增加.
- 过期的项目仅在访问时被删除,而不是在背景中自动删除.
文件夹结构
内存缓存包的文件夹结构包括:
src/:包含包的源代码,包括:Factory/:包含用于创建缓存处理器的工厂类.Cache/: 包含执行缓存对象的类.Type/: 包含类型定义和接口.
tests/:包含包件的单位测试.README.md: 包的文档.
捐款
欢迎捐款! 随意叉出这个寄存器并提交拉取请求. 在提交之前,请确保您的代码通过所有起伏和单元测试.
您可以使用 :
pnpm format
您可以使用 :
pnpm test