🎨 Optimize code structure
This commit is contained in:
106
frontend/src/common/cache/README.md
vendored
Normal file
106
frontend/src/common/cache/README.md
vendored
Normal file
@@ -0,0 +1,106 @@
|
||||
# 缓存系统
|
||||
|
||||
简洁高效的 LRU 缓存实现,支持泛型和自动清理。
|
||||
|
||||
## 特性
|
||||
|
||||
- 🚀 高性能 LRU 缓存算法
|
||||
- 🔧 TypeScript 泛型支持
|
||||
- 🧹 自动资源清理
|
||||
- 📊 缓存统计信息
|
||||
- ⏰ TTL 过期支持
|
||||
- 🎯 简洁易用的 API
|
||||
|
||||
## 基础用法
|
||||
|
||||
### 创建缓存
|
||||
|
||||
```typescript
|
||||
import { LRUCache, CacheManager, createCacheItem } from '@/common/cache';
|
||||
|
||||
// 直接创建缓存
|
||||
const cache = new LRUCache({
|
||||
maxSize: 100,
|
||||
ttl: 5 * 60 * 1000, // 5 分钟
|
||||
onEvict: (item) => console.log('Evicted:', item)
|
||||
});
|
||||
|
||||
// 使用缓存管理器
|
||||
const manager = new CacheManager();
|
||||
const myCache = manager.createCache('myCache', { maxSize: 50 });
|
||||
```
|
||||
|
||||
### 缓存操作
|
||||
|
||||
```typescript
|
||||
// 创建缓存项
|
||||
const item = createCacheItem('key1', {
|
||||
name: 'example',
|
||||
data: { foo: 'bar' }
|
||||
});
|
||||
|
||||
// 设置缓存
|
||||
cache.set('key1', item);
|
||||
|
||||
// 获取缓存
|
||||
const cached = cache.get('key1');
|
||||
|
||||
// 检查存在
|
||||
if (cache.has('key1')) {
|
||||
// 处理逻辑
|
||||
}
|
||||
|
||||
// 移除缓存
|
||||
cache.remove('key1');
|
||||
```
|
||||
|
||||
### 自动清理资源
|
||||
|
||||
```typescript
|
||||
interface MyItem extends CacheItem, DisposableCacheItem {
|
||||
resource: SomeResource;
|
||||
dispose(): void;
|
||||
}
|
||||
|
||||
const item: MyItem = {
|
||||
id: 'resource1',
|
||||
lastAccessed: new Date(),
|
||||
createdAt: new Date(),
|
||||
resource: new SomeResource(),
|
||||
dispose() {
|
||||
this.resource.cleanup();
|
||||
}
|
||||
};
|
||||
|
||||
// 当项被驱逐或移除时,dispose 方法会自动调用
|
||||
cache.set('resource1', item);
|
||||
```
|
||||
|
||||
## API 参考
|
||||
|
||||
### LRUCache
|
||||
|
||||
- `get(id)` - 获取缓存项
|
||||
- `set(id, item)` - 设置缓存项
|
||||
- `remove(id)` - 移除缓存项
|
||||
- `has(id)` - 检查是否存在
|
||||
- `clear()` - 清空缓存
|
||||
- `size()` - 获取缓存大小
|
||||
- `getStats()` - 获取统计信息
|
||||
- `cleanup()` - 清理过期项
|
||||
|
||||
### CacheManager
|
||||
|
||||
- `getCache(name, config?)` - 获取或创建缓存
|
||||
- `createCache(name, config)` - 创建新缓存
|
||||
- `removeCache(name)` - 删除缓存
|
||||
- `clearAll()` - 清空所有缓存
|
||||
- `getAllStats()` - 获取所有统计信息
|
||||
|
||||
## 工具函数
|
||||
|
||||
- `generateCacheKey(...parts)` - 生成缓存键
|
||||
- `createCacheItem(id, data)` - 创建缓存项
|
||||
- `createContentHash(content)` - 创建内容哈希
|
||||
- `debounce(func, wait)` - 防抖函数
|
||||
- `throttle(func, limit)` - 节流函数
|
||||
Reference in New Issue
Block a user