pdd-skills 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1478 -0
- package/bin/pdd.js +354 -0
- package/config/bpmn-rules.yaml +166 -0
- package/config/checkstyle.xml +105 -0
- package/config/eslint.config.js +48 -0
- package/config/pmd.xml +91 -0
- package/config/prd-rules.yaml +113 -0
- package/config/ruff.toml +45 -0
- package/config/sqlfluff.cfg +82 -0
- package/hooks/hook-executor.js +332 -0
- package/index.js +43 -0
- package/lib/api-routes.js +750 -0
- package/lib/api-server.js +408 -0
- package/lib/cache/cache-config.js +209 -0
- package/lib/cache/system-cache.js +852 -0
- package/lib/config-manager.js +373 -0
- package/lib/generate.js +528 -0
- package/lib/grpc/grpc-routes.js +1134 -0
- package/lib/grpc/grpc-server.js +912 -0
- package/lib/grpc/proto-definitions.js +1033 -0
- package/lib/init.js +172 -0
- package/lib/iteration/auto-fixer.js +1025 -0
- package/lib/iteration/auto-reviewer.js +923 -0
- package/lib/iteration/controller.js +577 -0
- package/lib/list.js +130 -0
- package/lib/mcp-server.js +548 -0
- package/lib/openclaw/api-integration.js +535 -0
- package/lib/openclaw/cli-integration.js +567 -0
- package/lib/openclaw/data-sync.js +845 -0
- package/lib/openclaw/openclaw-adapter.js +783 -0
- package/lib/plugin/example-plugins/code-stats/index.js +332 -0
- package/lib/plugin/example-plugins/code-stats/plugin.json +1 -0
- package/lib/plugin/example-plugins/custom-linter/index.js +472 -0
- package/lib/plugin/example-plugins/custom-linter/plugin.json +1 -0
- package/lib/plugin/example-plugins/hello-world/index.js +86 -0
- package/lib/plugin/example-plugins/hello-world/plugin.json +1 -0
- package/lib/plugin/plugin-manager.js +655 -0
- package/lib/plugin/plugin-sdk.js +565 -0
- package/lib/plugin/sandbox.js +627 -0
- package/lib/quality/rules/maintainability.js +418 -0
- package/lib/quality/rules/performance.js +498 -0
- package/lib/quality/rules/readability.js +441 -0
- package/lib/quality/rules/robustness.js +504 -0
- package/lib/quality/rules/security.js +444 -0
- package/lib/quality/scorer.js +576 -0
- package/lib/report.js +669 -0
- package/lib/sdk-base.js +301 -0
- package/lib/sdk-js.js +446 -0
- package/lib/sdk-python/README.md +546 -0
- package/lib/sdk-python/examples/basic_usage.py +450 -0
- package/lib/sdk-python/pdd_sdk/__init__.py +180 -0
- package/lib/sdk-python/pdd_sdk/client.py +1170 -0
- package/lib/sdk-python/pdd_sdk/events.py +423 -0
- package/lib/sdk-python/pdd_sdk/exceptions.py +158 -0
- package/lib/sdk-python/pdd_sdk/models.py +518 -0
- package/lib/sdk-python/pdd_sdk/utils.py +759 -0
- package/lib/token/budget-alert.js +367 -0
- package/lib/token/budget-manager.js +485 -0
- package/lib/update.js +54 -0
- package/lib/utils/logger.js +88 -0
- package/lib/verify.js +741 -0
- package/lib/version.js +52 -0
- package/lib/vm/README.md +102 -0
- package/lib/vm/dashboard/api-routes.js +669 -0
- package/lib/vm/dashboard/server.js +391 -0
- package/lib/vm/dashboard/sse.js +358 -0
- package/lib/vm/dashboard/static/css/dashboard.css +1378 -0
- package/lib/vm/dashboard/static/index.html +118 -0
- package/lib/vm/dashboard/static/js/app.js +949 -0
- package/lib/vm/dashboard/static/js/charts.js +913 -0
- package/lib/vm/dashboard/static/js/kanban-view.js +1053 -0
- package/lib/vm/dashboard/static/js/pipeline-view.js +463 -0
- package/lib/vm/dashboard/static/js/quality-view.js +598 -0
- package/lib/vm/dashboard/static/js/system-view.js +1021 -0
- package/lib/vm/data-provider.js +1191 -0
- package/lib/vm/event-bus.js +402 -0
- package/lib/vm/hooks/extract-hook.js +307 -0
- package/lib/vm/hooks/generate-hook.js +374 -0
- package/lib/vm/hooks/hook-interface.js +458 -0
- package/lib/vm/hooks/report-hook.js +331 -0
- package/lib/vm/hooks/verify-hook.js +454 -0
- package/lib/vm/models.js +1003 -0
- package/lib/vm/reconciler.js +855 -0
- package/lib/vm/scanner.js +988 -0
- package/lib/vm/state-schema.js +955 -0
- package/lib/vm/state-store.js +733 -0
- package/lib/vm/tui/components/card.js +339 -0
- package/lib/vm/tui/components/progress-bar.js +368 -0
- package/lib/vm/tui/components/sparkline.js +327 -0
- package/lib/vm/tui/components/status-light.js +294 -0
- package/lib/vm/tui/components/table.js +370 -0
- package/lib/vm/tui/input.js +335 -0
- package/lib/vm/tui/renderer.js +548 -0
- package/lib/vm/tui/screens/kanban-screen.js +397 -0
- package/lib/vm/tui/screens/overview-screen.js +357 -0
- package/lib/vm/tui/screens/quality-screen.js +336 -0
- package/lib/vm/tui/screens/system-screen.js +379 -0
- package/lib/vm/tui/tui.js +805 -0
- package/package.json +1 -0
- package/scripts/cso-analyzer.js +198 -0
- package/scripts/eval-runner.js +359 -0
- package/scripts/i18n-checker.js +109 -0
- package/scripts/linter/activiti-linter.js +272 -0
- package/scripts/linter/prd-linter.js +162 -0
- package/scripts/linter/report-generator.js +207 -0
- package/scripts/linter/run-linters.js +285 -0
- package/scripts/linter/sql-linter.js +166 -0
- package/scripts/token-analyzer.js +162 -0
- package/scripts/vm-test.js +180 -0
- package/skills/core/official-doc-writer/LICENSE +21 -0
- package/skills/core/official-doc-writer/README.md +232 -0
- package/skills/core/official-doc-writer/SKILL.md +475 -0
- package/skills/core/official-doc-writer/_meta.json +1 -0
- package/skills/core/official-doc-writer/document_generator.py +580 -0
- package/skills/core/official-doc-writer/evals/default-evals.json +1 -0
- package/skills/core/official-doc-writer/examples.md +150 -0
- package/skills/core/official-doc-writer/fonts/FONTS_LIST.md +45 -0
- package/skills/core/official-doc-writer/fonts/README.md +141 -0
- package/skills/core/official-doc-writer/fonts/SIMFANG.TTF +0 -0
- package/skills/core/official-doc-writer/fonts/SIMHEI.TTF +0 -0
- package/skills/core/official-doc-writer/fonts/SIMKAI.TTF +0 -0
- package/skills/core/official-doc-writer/fonts/SIMSUN.TTC +0 -0
- package/skills/core/official-doc-writer/fonts//346/226/271/346/255/243/345/260/217/346/240/207/345/256/213GBK.TTF +0 -0
- package/skills/core/official-doc-writer/references/GBT_9704-2012_/345/205/232/346/224/277/346/234/272/345/205/263/345/205/254/346/226/207/346/240/274/345/274/217.md +422 -0
- package/skills/core/official-doc-writer/scripts/__pycache__/generate_official_doc.cpython-313.pyc +0 -0
- package/skills/core/official-doc-writer/scripts/dialog_manager.py +564 -0
- package/skills/core/official-doc-writer/scripts/generate_official_doc.py +252 -0
- package/skills/core/official-doc-writer/scripts/install_fonts.py +390 -0
- package/skills/core/official-doc-writer/scripts/smart_prompts.py +363 -0
- package/skills/core/pdd-ba/SKILL.md +305 -0
- package/skills/core/pdd-ba/_meta.json +1 -0
- package/skills/core/pdd-ba/evals/default-evals.json +1 -0
- package/skills/core/pdd-code-reviewer/SKILL.md +378 -0
- package/skills/core/pdd-code-reviewer/_meta.json +1 -0
- package/skills/core/pdd-code-reviewer/evals/default-evals.json +1 -0
- package/skills/core/pdd-doc-change/SKILL.md +350 -0
- package/skills/core/pdd-doc-change/_meta.json +1 -0
- package/skills/core/pdd-doc-change/evals/default-evals.json +1 -0
- package/skills/core/pdd-doc-gardener/SKILL.md +248 -0
- package/skills/core/pdd-doc-gardener/_meta.json +1 -0
- package/skills/core/pdd-doc-gardener/evals/default-evals.json +1 -0
- package/skills/core/pdd-entropy-reduction/SKILL.md +360 -0
- package/skills/core/pdd-entropy-reduction/_meta.json +1 -0
- package/skills/core/pdd-entropy-reduction/evals/default-evals.json +1 -0
- package/skills/core/pdd-entropy-reduction/references/entropy-report-template.md +287 -0
- package/skills/core/pdd-entropy-reduction/references/golden-principles.md +573 -0
- package/skills/core/pdd-entropy-reduction/scripts/entropy_scan.py +712 -0
- package/skills/core/pdd-extract-features/SKILL.md +320 -0
- package/skills/core/pdd-extract-features/_meta.json +1 -0
- package/skills/core/pdd-extract-features/evals/default-evals.json +1 -0
- package/skills/core/pdd-generate-spec/SKILL.md +418 -0
- package/skills/core/pdd-generate-spec/_meta.json +1 -0
- package/skills/core/pdd-generate-spec/evals/default-evals.json +1 -0
- package/skills/core/pdd-implement-feature/SKILL.md +332 -0
- package/skills/core/pdd-implement-feature/_meta.json +1 -0
- package/skills/core/pdd-implement-feature/evals/default-evals.json +1 -0
- package/skills/core/pdd-main/SKILL.md +540 -0
- package/skills/core/pdd-main/_meta.json +1 -0
- package/skills/core/pdd-main/evals/default-evals.json +1 -0
- package/skills/core/pdd-main/evals/evals.json +215 -0
- package/skills/core/pdd-verify-feature/SKILL.md +474 -0
- package/skills/core/pdd-verify-feature/_meta.json +1 -0
- package/skills/core/pdd-verify-feature/evals/default-evals.json +1 -0
- package/skills/core/pdd-vm/evals/default-evals.json +1 -0
- package/skills/core/traffic-accident-assessor/LICENSE +29 -0
- package/skills/core/traffic-accident-assessor/SKILL.md +439 -0
- package/skills/core/traffic-accident-assessor/evals/evals.json +1 -0
- package/skills/core/traffic-accident-assessor/references/accident-types.md +369 -0
- package/skills/core/traffic-accident-assessor/references/liability-rules.md +287 -0
- package/skills/core/traffic-accident-assessor/references/traffic-laws.md +226 -0
- package/skills/core/traffic-accident-assessor/references//351/253/230/345/260/224/345/244/253/350/257/264/346/230/216/344/271/246.pdf +32576 -106
- package/skills/core/traffic-accident-assessor/scripts/generate_official_statement.py +588 -0
- package/skills/core/traffic-accident-assessor/scripts/generate_report.py +495 -0
- package/skills/core/traffic-accident-assessor/scripts/generate_statement.py +528 -0
- package/skills/core/traffic-accident-assessor.zip +0 -0
- package/skills/entropy/expert-arch-enforcer/SKILL.md +292 -0
- package/skills/entropy/expert-arch-enforcer/_meta.json +1 -0
- package/skills/entropy/expert-arch-enforcer/evals/default-evals.json +1 -0
- package/skills/entropy/expert-auto-refactor/SKILL.md +327 -0
- package/skills/entropy/expert-auto-refactor/_meta.json +1 -0
- package/skills/entropy/expert-auto-refactor/evals/default-evals.json +1 -0
- package/skills/entropy/expert-code-quality/SKILL.md +468 -0
- package/skills/entropy/expert-code-quality/_meta.json +1 -0
- package/skills/entropy/expert-code-quality/evals/default-evals.json +1 -0
- package/skills/entropy/expert-code-quality/evals/evals.json +109 -0
- package/skills/entropy/expert-code-quality/references/code-smells.md +605 -0
- package/skills/entropy/expert-code-quality/references/design-patterns.md +1111 -0
- package/skills/entropy/expert-code-quality/references/refactoring-catalog.md +1281 -0
- package/skills/entropy/expert-code-quality/references/solid-principles.md +524 -0
- package/skills/entropy/expert-entropy-auditor/SKILL.md +276 -0
- package/skills/entropy/expert-entropy-auditor/_meta.json +1 -0
- package/skills/entropy/expert-entropy-auditor/evals/default-evals.json +1 -0
- package/skills/expert/expert-activiti/SKILL.md +497 -0
- package/skills/expert/expert-activiti/_meta.json +1 -0
- package/skills/expert/expert-mysql/SKILL.md +832 -0
- package/skills/expert/expert-mysql/_meta.json +1 -0
- package/skills/expert/expert-performance/SKILL.md +379 -0
- package/skills/expert/expert-performance/_meta.json +1 -0
- package/skills/expert/expert-performance/evals/default-evals.json +1 -0
- package/skills/expert/expert-ruoyi/SKILL.md +472 -0
- package/skills/expert/expert-ruoyi/_meta.json +1 -0
- package/skills/expert/expert-security/SKILL.md +1341 -0
- package/skills/expert/expert-security/_meta.json +1 -0
- package/skills/expert/expert-security/evals/default-evals.json +1 -0
- package/skills/expert/software-architect/SKILL.md +350 -0
- package/skills/expert/software-architect/_meta.json +1 -0
- package/skills/expert/software-engineer/SKILL.md +437 -0
- package/skills/expert/software-engineer/_meta.json +1 -0
- package/skills/expert/software-engineer/architecture.md +130 -0
- package/skills/expert/software-engineer/patterns.md +151 -0
- package/skills/expert/software-engineer/testing.md +135 -0
- package/skills/expert/system-architect/SKILL.md +628 -0
- package/skills/expert/system-architect/_meta.json +1 -0
- package/skills/expert/system-architect/assets/templates/ARCHITECTURE.md +25 -0
- package/skills/expert/system-architect/assets/templates/README.md +44 -0
- package/skills/expert/system-architect/references/js-ts-standards.md +18 -0
- package/skills/expert/system-architect/references/python-standards.md +19 -0
- package/skills/expert/system-architect/references/scaffolding.md +61 -0
- package/skills/expert/system-architect/references/security-checklist.md +21 -0
- package/skills/openspec/openspec-apply-change/SKILL.md +156 -0
- package/skills/openspec/openspec-apply-change/_meta.json +1 -0
- package/skills/openspec/openspec-archive-change/SKILL.md +114 -0
- package/skills/openspec/openspec-archive-change/_meta.json +1 -0
- package/skills/openspec/openspec-bulk-archive-change/SKILL.md +246 -0
- package/skills/openspec/openspec-bulk-archive-change/_meta.json +1 -0
- package/skills/openspec/openspec-continue-change/SKILL.md +118 -0
- package/skills/openspec/openspec-continue-change/_meta.json +1 -0
- package/skills/openspec/openspec-explore/SKILL.md +288 -0
- package/skills/openspec/openspec-explore/_meta.json +1 -0
- package/skills/openspec/openspec-ff-change/SKILL.md +101 -0
- package/skills/openspec/openspec-ff-change/_meta.json +1 -0
- package/skills/openspec/openspec-new-change/SKILL.md +74 -0
- package/skills/openspec/openspec-new-change/_meta.json +1 -0
- package/skills/openspec/openspec-onboard/SKILL.md +554 -0
- package/skills/openspec/openspec-onboard/_meta.json +1 -0
- package/skills/openspec/openspec-sync-specs/SKILL.md +138 -0
- package/skills/openspec/openspec-sync-specs/_meta.json +1 -0
- package/skills/openspec/openspec-verify-change/SKILL.md +168 -0
- package/skills/openspec/openspec-verify-change/_meta.json +1 -0
- package/skills/pr/pdd-multi-review/SKILL.md +534 -0
- package/skills/pr/pdd-multi-review/_meta.json +1 -0
- package/skills/pr/pdd-pr-batch/SKILL.md +303 -0
- package/skills/pr/pdd-pr-batch/_meta.json +1 -0
- package/skills/pr/pdd-pr-create/SKILL.md +344 -0
- package/skills/pr/pdd-pr-create/_meta.json +1 -0
- package/skills/pr/pdd-pr-merge/SKILL.md +286 -0
- package/skills/pr/pdd-pr-merge/_meta.json +1 -0
- package/skills/pr/pdd-pr-review/SKILL.md +217 -0
- package/skills/pr/pdd-pr-review/_meta.json +1 -0
- package/skills/pr/pdd-task-manager/SKILL.md +636 -0
- package/skills/pr/pdd-task-manager/_meta.json +1 -0
- package/skills/pr/pdd-template-engine/SKILL.md +306 -0
- package/skills/pr/pdd-template-engine/_meta.json +1 -0
- package/templates/behavior-shaping/iron-law-template.md +87 -0
- package/templates/behavior-shaping/rationalization-template.md +62 -0
- package/templates/behavior-shaping/red-flags-template.md +70 -0
- package/templates/bilingual-template.md +139 -0
- package/templates/config/default.yaml +47 -0
- package/templates/project/default/README.md +31 -0
- package/templates/project/frontend/README.md +46 -0
- package/templates/project/java/README.md +48 -0
|
@@ -0,0 +1,852 @@
|
|
|
1
|
+
// lib/cache/system-cache.js - PDD Three-Level Cache System
|
|
2
|
+
// 实现L1(会话) -> L2(项目) -> L3(全局/磁盘)三级缓存架构
|
|
3
|
+
// 支持TTL过期、LRU/LFU淘汰、命中统计、自动清理
|
|
4
|
+
|
|
5
|
+
import fs from 'node:fs';
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
import crypto from 'node:crypto';
|
|
8
|
+
import { log } from '../utils/logger.js';
|
|
9
|
+
import {
|
|
10
|
+
CacheLevel,
|
|
11
|
+
DEFAULT_CACHE_CONFIG,
|
|
12
|
+
CACHE_KEY_PREFIXES,
|
|
13
|
+
getRecommendedStrategy,
|
|
14
|
+
validateCacheConfig
|
|
15
|
+
} from './cache-config.js';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* LRU (Least Recently Used) 缓存实现
|
|
19
|
+
* @private
|
|
20
|
+
*/
|
|
21
|
+
class LRUCache {
|
|
22
|
+
/**
|
|
23
|
+
* @param {number} maxSize - 最大容量
|
|
24
|
+
*/
|
|
25
|
+
constructor(maxSize = 100) {
|
|
26
|
+
this.maxSize = maxSize;
|
|
27
|
+
this.cache = new Map(); // Map保持插入顺序,用于LRU
|
|
28
|
+
this.hits = 0;
|
|
29
|
+
this.misses = 0;
|
|
30
|
+
this.evictions = 0;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* 获取缓存值
|
|
35
|
+
* @param {string} key - 缓存键
|
|
36
|
+
* @returns {*|null} 缓存值或null
|
|
37
|
+
*/
|
|
38
|
+
get(key) {
|
|
39
|
+
const item = this.cache.get(key);
|
|
40
|
+
|
|
41
|
+
if (!item) {
|
|
42
|
+
this.misses++;
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// 检查TTL是否过期
|
|
47
|
+
if (item.ttl && Date.now() - item.timestamp > item.ttl) {
|
|
48
|
+
this.cache.delete(key);
|
|
49
|
+
this.misses++;
|
|
50
|
+
this.evictions++;
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// LRU:将访问的项移到最新位置(Map末尾)
|
|
55
|
+
this.cache.delete(key);
|
|
56
|
+
this.cache.set(key, item);
|
|
57
|
+
this.hits++;
|
|
58
|
+
|
|
59
|
+
return item.value;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 设置缓存值
|
|
64
|
+
* @param {string} key - 缓存键
|
|
65
|
+
* @param {*} value - 缓存值
|
|
66
|
+
* @param {Object} options - 选项
|
|
67
|
+
* @param {number} options.ttl - TTL(毫秒)
|
|
68
|
+
*/
|
|
69
|
+
set(key, value, options = {}) {
|
|
70
|
+
// 如果键已存在,先删除(为了更新顺序)
|
|
71
|
+
if (this.cache.has(key)) {
|
|
72
|
+
this.cache.delete(key);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// 检查容量,执行LRU淘汰
|
|
76
|
+
while (this.cache.size >= this.maxSize) {
|
|
77
|
+
const oldestKey = this.cache.keys().next().value;
|
|
78
|
+
this.cache.delete(oldestKey);
|
|
79
|
+
this.evictions++;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
this.cache.set(key, {
|
|
83
|
+
value,
|
|
84
|
+
timestamp: Date.now(),
|
|
85
|
+
ttl: options.ttl || null
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* 删除指定键
|
|
91
|
+
* @param {string} key - 缓存键
|
|
92
|
+
* @returns {boolean} 是否删除成功
|
|
93
|
+
*/
|
|
94
|
+
delete(key) {
|
|
95
|
+
return this.cache.delete(key);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* 检查键是否存在
|
|
100
|
+
* @param {string} key - 缓存键
|
|
101
|
+
* @returns {boolean}
|
|
102
|
+
*/
|
|
103
|
+
has(key) {
|
|
104
|
+
const item = this.cache.get(key);
|
|
105
|
+
if (!item) return false;
|
|
106
|
+
|
|
107
|
+
if (item.ttl && Date.now() - item.timestamp > item.ttl) {
|
|
108
|
+
this.cache.delete(key);
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* 清空缓存
|
|
117
|
+
*/
|
|
118
|
+
clear() {
|
|
119
|
+
this.cache.clear();
|
|
120
|
+
this.hits = 0;
|
|
121
|
+
this.misses = 0;
|
|
122
|
+
this.evictions = 0;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* 获取当前大小
|
|
127
|
+
* @returns {number}
|
|
128
|
+
*/
|
|
129
|
+
get size() {
|
|
130
|
+
return this.cache.size;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* 获取统计信息
|
|
135
|
+
* @returns {Object}
|
|
136
|
+
*/
|
|
137
|
+
getStats() {
|
|
138
|
+
const total = this.hits + this.misses;
|
|
139
|
+
return {
|
|
140
|
+
size: this.size,
|
|
141
|
+
maxSize: this.maxSize,
|
|
142
|
+
hits: this.hits,
|
|
143
|
+
misses: this.misses,
|
|
144
|
+
hitRate: total > 0 ? ((this.hits / total) * 100).toFixed(2) + '%' : 'N/A',
|
|
145
|
+
evictions: this.evictions
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* LFU (Least Frequently Used) 磁盘缓存实现
|
|
152
|
+
* @private
|
|
153
|
+
*/
|
|
154
|
+
class DiskCache {
|
|
155
|
+
/**
|
|
156
|
+
* @param {Object} config - 配置
|
|
157
|
+
*/
|
|
158
|
+
constructor(config = {}) {
|
|
159
|
+
this.cacheDir = path.resolve(config.diskPath || '.pdd-cache');
|
|
160
|
+
this.maxSize = config.maxSize || 2000;
|
|
161
|
+
this.defaultTTL = config.ttl || 24 * 60 * 60 * 1000; // 默认24小时
|
|
162
|
+
this.compression = config.compression || false;
|
|
163
|
+
|
|
164
|
+
this.stats = {
|
|
165
|
+
hits: 0,
|
|
166
|
+
misses: 0,
|
|
167
|
+
evictions: 0,
|
|
168
|
+
writes: 0
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
this._ensureCacheDir();
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* 确保缓存目录存在
|
|
176
|
+
* @private
|
|
177
|
+
*/
|
|
178
|
+
_ensureCacheDir() {
|
|
179
|
+
try {
|
|
180
|
+
if (!fs.existsSync(this.cacheDir)) {
|
|
181
|
+
fs.mkdirSync(this.cacheDir, { recursive: true });
|
|
182
|
+
log('info', `[Cache] Created disk cache directory: ${this.cacheDir}`);
|
|
183
|
+
}
|
|
184
|
+
} catch (error) {
|
|
185
|
+
log('error', '[Cache] Failed to create cache directory', error.message);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* 将键转换为安全的文件名
|
|
191
|
+
* @private
|
|
192
|
+
* @param {string} key - 原始键
|
|
193
|
+
* @returns {string} 文件路径
|
|
194
|
+
*/
|
|
195
|
+
_keyToPath(key) {
|
|
196
|
+
// 使用hash避免文件名过长或包含非法字符
|
|
197
|
+
const hash = crypto.createHash('md5').update(key).digest('hex').substring(0, 16);
|
|
198
|
+
return path.join(this.cacheDir, `${hash}.json`);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* 从磁盘读取缓存元数据
|
|
203
|
+
* @private
|
|
204
|
+
* @param {string} filePath - 文件路径
|
|
205
|
+
* @returns {Object|null} 元数据对象或null
|
|
206
|
+
*/
|
|
207
|
+
_readMeta(filePath) {
|
|
208
|
+
try {
|
|
209
|
+
const content = fs.readFileSync(filePath, 'utf-8');
|
|
210
|
+
return JSON.parse(content);
|
|
211
|
+
} catch (error) {
|
|
212
|
+
return null;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* 写入缓存到磁盘
|
|
218
|
+
* @private
|
|
219
|
+
* @param {string} filePath - 文件路径
|
|
220
|
+
* @param {Object} data - 数据对象
|
|
221
|
+
*/
|
|
222
|
+
_writeToDisk(filePath, data) {
|
|
223
|
+
try {
|
|
224
|
+
fs.writeFileSync(filePath, JSON.stringify(data, null, 2), 'utf-8');
|
|
225
|
+
this.stats.writes++;
|
|
226
|
+
} catch (error) {
|
|
227
|
+
log('error', '[Cache] Failed to write to disk', error.message);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* 获取缓存值
|
|
233
|
+
* @param {string} key - 缓存键
|
|
234
|
+
* @returns {*|null} 缓存值或null
|
|
235
|
+
*/
|
|
236
|
+
get(key) {
|
|
237
|
+
const filePath = this._keyToPath(key);
|
|
238
|
+
const meta = this._readMeta(filePath);
|
|
239
|
+
|
|
240
|
+
if (!meta) {
|
|
241
|
+
this.stats.misses++;
|
|
242
|
+
return null;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// 检查TTL
|
|
246
|
+
if (Date.now() - meta.timestamp > (meta.ttl || this.defaultTTL)) {
|
|
247
|
+
this._evict(filePath);
|
|
248
|
+
this.stats.misses++;
|
|
249
|
+
return null;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
meta.accessCount = (meta.accessCount || 0) + 1;
|
|
253
|
+
meta.lastAccess = Date.now();
|
|
254
|
+
this._writeToDisk(filePath, meta);
|
|
255
|
+
|
|
256
|
+
this.stats.hits++;
|
|
257
|
+
return meta.data;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* 设置缓存值
|
|
262
|
+
* @param {string} key - 缓存键
|
|
263
|
+
* @param {*} value - 缓存值
|
|
264
|
+
* @param {Object} options - 选项
|
|
265
|
+
*/
|
|
266
|
+
set(key, value, options = {}) {
|
|
267
|
+
// LFU淘汰检查
|
|
268
|
+
this._enforceLFUPolicy();
|
|
269
|
+
|
|
270
|
+
const filePath = this._keyToPath(key);
|
|
271
|
+
const meta = {
|
|
272
|
+
key,
|
|
273
|
+
data: value,
|
|
274
|
+
timestamp: Date.now(),
|
|
275
|
+
ttl: options.ttl || this.defaultTTL,
|
|
276
|
+
accessCount: 1,
|
|
277
|
+
lastAccess: Date.now(),
|
|
278
|
+
priority: options.priority || 'medium'
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
this._writeToDisk(filePath, meta);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* 执行LFU淘汰策略
|
|
286
|
+
* @private
|
|
287
|
+
*/
|
|
288
|
+
_enforceLFUPolicy() {
|
|
289
|
+
try {
|
|
290
|
+
const files = fs.readdirSync(this.cacheDir)
|
|
291
|
+
.filter(f => f.endsWith('.json'))
|
|
292
|
+
.map(f => ({
|
|
293
|
+
name: f,
|
|
294
|
+
path: path.join(this.cacheDir, f),
|
|
295
|
+
meta: this._readMeta(path.join(this.cacheDir, f))
|
|
296
|
+
}))
|
|
297
|
+
.filter(f => f.meta !== null)
|
|
298
|
+
.sort((a, b) => (a.meta.accessCount || 0) - (b.meta.accessCount || 0));
|
|
299
|
+
|
|
300
|
+
// 当文件数超过最大容量时,淘汰最少使用的
|
|
301
|
+
while (files.length > this.maxSize) {
|
|
302
|
+
const victim = files.shift();
|
|
303
|
+
this._evict(victim.path);
|
|
304
|
+
}
|
|
305
|
+
} catch (error) {
|
|
306
|
+
log('warn', '[Cache] LFU eviction check failed', error.message);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* 淘汰指定的缓存文件
|
|
312
|
+
* @private
|
|
313
|
+
* @param {string} filePath - 文件路径
|
|
314
|
+
*/
|
|
315
|
+
_evict(filePath) {
|
|
316
|
+
try {
|
|
317
|
+
if (fs.existsSync(filePath)) {
|
|
318
|
+
fs.unlinkSync(filePath);
|
|
319
|
+
this.stats.evictions++;
|
|
320
|
+
}
|
|
321
|
+
} catch (error) {
|
|
322
|
+
log('warn', '[Cache] Failed to evict cache file', error.message);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* 删除指定键
|
|
328
|
+
* @param {string} key - 缓存键
|
|
329
|
+
* @returns {boolean}
|
|
330
|
+
*/
|
|
331
|
+
delete(key) {
|
|
332
|
+
const filePath = this._keyToPath(key);
|
|
333
|
+
|
|
334
|
+
if (fs.existsSync(filePath)) {
|
|
335
|
+
this._evict(filePath);
|
|
336
|
+
return true;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
return false;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* 清空所有缓存
|
|
344
|
+
*/
|
|
345
|
+
clear() {
|
|
346
|
+
try {
|
|
347
|
+
const files = fs.readdirSync(this.cacheDir).filter(f => f.endsWith('.json'));
|
|
348
|
+
files.forEach(f => {
|
|
349
|
+
this._evict(path.join(this.cacheDir, f));
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
this.stats = { hits: 0, misses: 0, evictions: 0, writes: 0 };
|
|
353
|
+
log('info', '[Cache] Disk cache cleared');
|
|
354
|
+
} catch (error) {
|
|
355
|
+
log('error', '[Cache] Failed to clear disk cache', error.message);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* 清理过期缓存
|
|
361
|
+
* @returns {number} 清理的条目数
|
|
362
|
+
*/
|
|
363
|
+
cleanupExpired() {
|
|
364
|
+
let cleaned = 0;
|
|
365
|
+
|
|
366
|
+
try {
|
|
367
|
+
const files = fs.readdirSync(this.cacheDir).filter(f => f.endsWith('.json'));
|
|
368
|
+
|
|
369
|
+
files.forEach(f => {
|
|
370
|
+
const filePath = path.join(this.cacheDir, f);
|
|
371
|
+
const meta = this._readMeta(filePath);
|
|
372
|
+
|
|
373
|
+
if (meta && Date.now() - meta.timestamp > (meta.ttl || this.defaultTTL)) {
|
|
374
|
+
this._evict(filePath);
|
|
375
|
+
cleaned++;
|
|
376
|
+
}
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
if (cleaned > 0) {
|
|
380
|
+
log('info', `[Cache] Cleaned up ${cleaned} expired entries`);
|
|
381
|
+
}
|
|
382
|
+
} catch (error) {
|
|
383
|
+
log('error', '[Cache] Cleanup failed', error.message);
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
return cleaned;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* 获取统计信息
|
|
391
|
+
* @returns {Object}
|
|
392
|
+
*/
|
|
393
|
+
getStats() {
|
|
394
|
+
let fileCount = 0;
|
|
395
|
+
let totalSize = 0;
|
|
396
|
+
|
|
397
|
+
try {
|
|
398
|
+
const files = fs.readdirSync(this.cacheDir).filter(f => f.endsWith('.json'));
|
|
399
|
+
fileCount = files.length;
|
|
400
|
+
|
|
401
|
+
files.forEach(f => {
|
|
402
|
+
try {
|
|
403
|
+
const stat = fs.statSync(path.join(this.cacheDir, f));
|
|
404
|
+
totalSize += stat.size;
|
|
405
|
+
} catch (e) { /* ignore */ }
|
|
406
|
+
});
|
|
407
|
+
} catch (e) { /* ignore */ }
|
|
408
|
+
|
|
409
|
+
const total = this.stats.hits + this.stats.misses;
|
|
410
|
+
|
|
411
|
+
return {
|
|
412
|
+
size: fileCount,
|
|
413
|
+
maxSize: this.maxSize,
|
|
414
|
+
hits: this.stats.hits,
|
|
415
|
+
misses: this.stats.misses,
|
|
416
|
+
hitRate: total > 0 ? ((this.stats.hits / total) * 100).toFixed(2) + '%' : 'N/A',
|
|
417
|
+
evictions: this.stats.evictions,
|
|
418
|
+
writes: this.stats.writes,
|
|
419
|
+
diskUsage: `${(totalSize / 1024).toFixed(2)} KB`,
|
|
420
|
+
cacheDir: this.cacheDir
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/**
|
|
426
|
+
* SystemCache - PDD三级缓存系统主类
|
|
427
|
+
*
|
|
428
|
+
* 架构说明:
|
|
429
|
+
* - L1 (Session): 会话级内存缓存,TTL=会话时长,容量最小,速度最快
|
|
430
|
+
* - L2 (Project): 项目级内存缓存,TTL=30min,容量中等,跨操作共享
|
|
431
|
+
* - L3 (Global): 全局磁盘缓存,TTL=24h,容量最大,持久化存储
|
|
432
|
+
*
|
|
433
|
+
* 查找策略:L1 -> L2 -> L3 逐级查找,找到后回填上级缓存
|
|
434
|
+
* 写入策略:根据数据类型和配置决定写入级别
|
|
435
|
+
*/
|
|
436
|
+
export class SystemCache {
|
|
437
|
+
/**
|
|
438
|
+
* 构造函数
|
|
439
|
+
* @param {Object} config - 缓存配置(可选)
|
|
440
|
+
*/
|
|
441
|
+
constructor(config = {}) {
|
|
442
|
+
// 验证并合并配置
|
|
443
|
+
const validation = validateCacheConfig(config);
|
|
444
|
+
if (!validation.valid) {
|
|
445
|
+
log('warn', '[Cache] Invalid configuration, using defaults', validation.errors);
|
|
446
|
+
config = {};
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
this.config = {
|
|
450
|
+
...DEFAULT_CACHE_CONFIG,
|
|
451
|
+
...config,
|
|
452
|
+
session: { ...DEFAULT_CACHE_CONFIG.session, ...(config.session || {}) },
|
|
453
|
+
project: { ...DEFAULT_CACHE_CONFIG.project, ...(config.project || {}) },
|
|
454
|
+
global: { ...DEFAULT_CACHE_CONFIG.global, ...(config.global || {}) }
|
|
455
|
+
};
|
|
456
|
+
|
|
457
|
+
// 初始化三级缓存
|
|
458
|
+
this.L1 = new LRUCache(this.config.session.maxSize); // 会话缓存
|
|
459
|
+
this.L2 = new LRUCache(this.config.project.maxSize); // 项目缓存
|
|
460
|
+
this.L3 = new DiskCache(this.config.global); // 全局磁盘缓存
|
|
461
|
+
|
|
462
|
+
// 统计信息
|
|
463
|
+
this.totalStats = {
|
|
464
|
+
l1Hits: 0,
|
|
465
|
+
l2Hits: 0,
|
|
466
|
+
l3Hits: 0,
|
|
467
|
+
totalMisses: 0,
|
|
468
|
+
promotions: 0 // 下级向上级的回填次数
|
|
469
|
+
};
|
|
470
|
+
|
|
471
|
+
// 启动定期清理任务
|
|
472
|
+
this._cleanupInterval = null;
|
|
473
|
+
this._startCleanupTask();
|
|
474
|
+
|
|
475
|
+
log('info', '[Cache] Three-level cache system initialized');
|
|
476
|
+
log(`info`, ` L1 (Session): max=${this.config.session.maxSize}, ttl=${this.formatTTL(this.config.session.ttl)}`);
|
|
477
|
+
log(`info`, ` L2 (Project): max=${this.config.project.maxSize}, ttl=${this.formatTTL(this.config.project.ttl)}`);
|
|
478
|
+
log(`info`, ` L3 (Global): max=${this.config.global.maxSize}, ttl=${this.formatTTL(this.config.global.ttl)}, dir=${this.config.global.diskPath}`);
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
/**
|
|
482
|
+
* 格式化TTL显示
|
|
483
|
+
* @private
|
|
484
|
+
*/
|
|
485
|
+
formatTTL(ttl) {
|
|
486
|
+
if (ttl === null) return 'session';
|
|
487
|
+
if (ttl < 1000) return `${ttl}ms`;
|
|
488
|
+
if (ttl < 60000) return `${(ttl / 1000).toFixed(0)}s`;
|
|
489
|
+
if (ttl < 3600000) return `${(ttl / 60000).toFixed(0)}min`;
|
|
490
|
+
return `${(ttl / 3600000).toFixed(1)}h`;
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/**
|
|
494
|
+
* 启动定期清理任务
|
|
495
|
+
* @private
|
|
496
|
+
*/
|
|
497
|
+
_startCleanupTask() {
|
|
498
|
+
// 每10分钟清理一次L3过期缓存
|
|
499
|
+
this._cleanupInterval = setInterval(() => {
|
|
500
|
+
try {
|
|
501
|
+
this.L3.cleanupExpired();
|
|
502
|
+
} catch (e) {
|
|
503
|
+
/* ignore cleanup errors */
|
|
504
|
+
}
|
|
505
|
+
}, 10 * 60 * 1000);
|
|
506
|
+
|
|
507
|
+
// 防止进程因定时器而无法退出
|
|
508
|
+
if (this._cleanupInterval.unref) {
|
|
509
|
+
this._cleanupInterval.unref();
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
/**
|
|
514
|
+
* 获取缓存值(逐级查找)
|
|
515
|
+
*
|
|
516
|
+
* 查找流程:
|
|
517
|
+
* 1. 先查找L1(会话缓存)- 最快
|
|
518
|
+
* 2. 未命中则查找L2(项目缓存)
|
|
519
|
+
* 3. 未命中则查找L3(磁盘缓存)
|
|
520
|
+
* 4. 在低级别命中的数据会回填到高级别缓存
|
|
521
|
+
*
|
|
522
|
+
* @param {string} key - 缓存键
|
|
523
|
+
* @param {number} [level=1] - 最小查找级别(1=L1, 2=L2, 3=L3)
|
|
524
|
+
* @returns {Promise<*>} 缓存值或undefined
|
|
525
|
+
*/
|
|
526
|
+
async get(key, level = 1) {
|
|
527
|
+
// L1 查找
|
|
528
|
+
if (level <= 1 && this.config.session.enabled) {
|
|
529
|
+
const value = this.L1.get(key);
|
|
530
|
+
if (value !== null) {
|
|
531
|
+
this.totalStats.l1Hits++;
|
|
532
|
+
return value;
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
// L2 查找
|
|
537
|
+
if (level <= 2 && this.config.project.enabled) {
|
|
538
|
+
const value = this.L2.get(key);
|
|
539
|
+
if (value !== null) {
|
|
540
|
+
this.totalStats.l2Hits++;
|
|
541
|
+
// 回填到L1
|
|
542
|
+
if (this.config.session.enabled) {
|
|
543
|
+
this.L1.set(key, value, { ttl: this.config.session.ttl });
|
|
544
|
+
this.totalStats.promotions++;
|
|
545
|
+
}
|
|
546
|
+
return value;
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
// L3 查找
|
|
551
|
+
if (level <= 3 && this.config.global.enabled) {
|
|
552
|
+
const value = this.L3.get(key);
|
|
553
|
+
if (value !== null) {
|
|
554
|
+
this.totalStats.l3Hits++;
|
|
555
|
+
// 回填到L2和L1
|
|
556
|
+
if (this.config.project.enabled) {
|
|
557
|
+
this.L2.set(key, value, { ttl: this.config.project.ttl });
|
|
558
|
+
}
|
|
559
|
+
if (this.config.session.enabled) {
|
|
560
|
+
this.L1.set(key, value, { ttl: this.config.session.ttl });
|
|
561
|
+
this.totalStats.promotions++;
|
|
562
|
+
}
|
|
563
|
+
return value;
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
// 所有级别都未命中
|
|
568
|
+
this.totalStats.totalMisses++;
|
|
569
|
+
return undefined;
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
/**
|
|
573
|
+
* 设置缓存值
|
|
574
|
+
*
|
|
575
|
+
* 根据推荐策略或指定级别写入缓存。
|
|
576
|
+
* 支持同时写入多个级别(回写策略)
|
|
577
|
+
*
|
|
578
|
+
* @param {string} key - 缓存键
|
|
579
|
+
* @param {*} value - 缓存值
|
|
580
|
+
* @param {Object} [options={}] - 选项
|
|
581
|
+
* @param {number} options.level - 目标缓存级别(1/2/3)
|
|
582
|
+
* @param {number} options.ttl - 自定义TTL(毫秒)
|
|
583
|
+
* @param {boolean} options.writeThrough - 是否透写所有下级缓存
|
|
584
|
+
* @param {string} options.priority - 优先级(critical/high/medium/low)
|
|
585
|
+
* @returns {Promise<void>}
|
|
586
|
+
*/
|
|
587
|
+
async set(key, value, options = {}) {
|
|
588
|
+
// 获取推荐的缓存策略
|
|
589
|
+
const strategy = getRecommendedStrategy(key);
|
|
590
|
+
const targetLevel = options.level || strategy.level;
|
|
591
|
+
const ttl = options.ttl || strategy.ttl;
|
|
592
|
+
|
|
593
|
+
const cacheOptions = {
|
|
594
|
+
ttl: ttl,
|
|
595
|
+
priority: options.priority || strategy.priority
|
|
596
|
+
};
|
|
597
|
+
|
|
598
|
+
// 根据目标级别写入
|
|
599
|
+
switch (targetLevel) {
|
|
600
|
+
case CacheLevel.SESSION:
|
|
601
|
+
if (this.config.session.enabled) {
|
|
602
|
+
this.L1.set(key, value, cacheOptions);
|
|
603
|
+
}
|
|
604
|
+
break;
|
|
605
|
+
|
|
606
|
+
case CacheLevel.PROJECT:
|
|
607
|
+
if (this.config.project.enabled) {
|
|
608
|
+
this.L2.set(key, value, cacheOptions);
|
|
609
|
+
}
|
|
610
|
+
// 同时写入L1(可选优化)
|
|
611
|
+
if (options.writeThrough && this.config.session.enabled) {
|
|
612
|
+
this.L1.set(key, value, { ...cacheOptions, ttl: this.config.session.ttl });
|
|
613
|
+
}
|
|
614
|
+
break;
|
|
615
|
+
|
|
616
|
+
case CacheLevel.GLOBAL:
|
|
617
|
+
if (this.config.global.enabled) {
|
|
618
|
+
this.L3.set(key, value, cacheOptions);
|
|
619
|
+
}
|
|
620
|
+
// 同时写入L2和L1
|
|
621
|
+
if (options.writeThrough) {
|
|
622
|
+
if (this.config.project.enabled) {
|
|
623
|
+
this.L2.set(key, value, { ...cacheOptions, ttl: this.config.project.ttl });
|
|
624
|
+
}
|
|
625
|
+
if (this.config.session.enabled) {
|
|
626
|
+
this.L1.set(key, value, { ...cacheOptions, ttl: this.config.session.ttl });
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
break;
|
|
630
|
+
|
|
631
|
+
default:
|
|
632
|
+
log('warn', `[Cache] Unknown cache level: ${targetLevel}`);
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
/**
|
|
637
|
+
* 使指定key在所有级别失效
|
|
638
|
+
* @param {string} key - 缓存键
|
|
639
|
+
* @returns {Promise<boolean>} 是否成功删除
|
|
640
|
+
*/
|
|
641
|
+
async invalidate(key) {
|
|
642
|
+
let deleted = false;
|
|
643
|
+
|
|
644
|
+
deleted = this.L1.delete(key) || deleted;
|
|
645
|
+
deleted = this.L2.delete(key) || deleted;
|
|
646
|
+
deleted = this.L3.delete(key) || deleted;
|
|
647
|
+
|
|
648
|
+
if (deleted) {
|
|
649
|
+
log('debug', `[Cache] Invalidated key: ${key}`);
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
return deleted;
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
/**
|
|
656
|
+
* 使匹配前缀的所有key失效
|
|
657
|
+
* @param {string} prefix - 键前缀
|
|
658
|
+
* @returns {Promise<number>} 失效的条目数
|
|
659
|
+
*/
|
|
660
|
+
async invalidateByPrefix(prefix) {
|
|
661
|
+
let count = 0;
|
|
662
|
+
|
|
663
|
+
// 注意:内存缓存的完整遍历需要更复杂的实现
|
|
664
|
+
// 这里简化处理,实际生产环境可能需要维护索引
|
|
665
|
+
|
|
666
|
+
// L3磁盘缓存可以通过扫描文件来处理
|
|
667
|
+
if (this.config.global.enabled) {
|
|
668
|
+
count += this.L3.cleanupExpired(); // 简化:仅清理过期
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
log('info', `[Cache] Invalidated ${count} keys with prefix: ${prefix}`);
|
|
672
|
+
return count;
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
/**
|
|
676
|
+
* 清空指定级别的缓存
|
|
677
|
+
* @param {number} level - 缓存级别(1/2/3)
|
|
678
|
+
* @returns {Promise<void>}
|
|
679
|
+
*/
|
|
680
|
+
async clearLevel(level) {
|
|
681
|
+
switch (level) {
|
|
682
|
+
case CacheLevel.SESSION:
|
|
683
|
+
this.L1.clear();
|
|
684
|
+
log('info', '[Cache] L1 (Session) cache cleared');
|
|
685
|
+
break;
|
|
686
|
+
|
|
687
|
+
case CacheLevel.PROJECT:
|
|
688
|
+
this.L2.clear();
|
|
689
|
+
log('info', '[Cache] L2 (Project) cache cleared');
|
|
690
|
+
break;
|
|
691
|
+
|
|
692
|
+
case CacheLevel.GLOBAL:
|
|
693
|
+
this.L3.clear();
|
|
694
|
+
log('info', '[Cache] L3 (Global/Disk) cache cleared');
|
|
695
|
+
break;
|
|
696
|
+
|
|
697
|
+
default:
|
|
698
|
+
throw new Error(`Invalid cache level: ${level}. Use 1, 2, or 3.`);
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
/**
|
|
703
|
+
* 清空所有级别的缓存
|
|
704
|
+
* @returns {Promise<void>}
|
|
705
|
+
*/
|
|
706
|
+
async clearAll() {
|
|
707
|
+
await this.clearLevel(CacheLevel.SESSION);
|
|
708
|
+
await this.clearLevel(CacheLevel.PROJECT);
|
|
709
|
+
await this.clearLevel(CacheLevel.GLOBAL);
|
|
710
|
+
log('success', '[Cache] All cache levels cleared');
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
/**
|
|
714
|
+
* 获取详细的统计信息
|
|
715
|
+
* @returns {Object} 各级缓存的统计数据
|
|
716
|
+
*/
|
|
717
|
+
async stats() {
|
|
718
|
+
const now = new Date().toISOString();
|
|
719
|
+
|
|
720
|
+
return {
|
|
721
|
+
timestamp: now,
|
|
722
|
+
system: {
|
|
723
|
+
version: '3.0.0',
|
|
724
|
+
levels: 3,
|
|
725
|
+
config: {
|
|
726
|
+
session: {
|
|
727
|
+
enabled: this.config.session.enabled,
|
|
728
|
+
maxSize: this.config.session.maxSize,
|
|
729
|
+
ttl: this.formatTTL(this.config.session.ttl)
|
|
730
|
+
},
|
|
731
|
+
project: {
|
|
732
|
+
enabled: this.config.project.enabled,
|
|
733
|
+
maxSize: this.config.project.maxSize,
|
|
734
|
+
ttl: this.formatTTL(this.config.project.ttl)
|
|
735
|
+
},
|
|
736
|
+
global: {
|
|
737
|
+
enabled: this.config.global.enabled,
|
|
738
|
+
maxSize: this.config.global.maxSize,
|
|
739
|
+
ttl: this.formatTTL(this.config.global.ttl),
|
|
740
|
+
diskPath: this.config.global.diskPath
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
},
|
|
744
|
+
levels: {
|
|
745
|
+
l1_session: {
|
|
746
|
+
...this.L1.getStats(),
|
|
747
|
+
type: 'Memory (LRU)',
|
|
748
|
+
description: 'Fastest, per-session scope'
|
|
749
|
+
},
|
|
750
|
+
l2_project: {
|
|
751
|
+
...this.L2.getStats(),
|
|
752
|
+
type: 'Memory (LRU)',
|
|
753
|
+
description: 'Fast, shared within project'
|
|
754
|
+
},
|
|
755
|
+
l3_global: {
|
|
756
|
+
...this.L3.getStats(),
|
|
757
|
+
type: 'Disk (LFU)',
|
|
758
|
+
description: 'Persistent, cross-session'
|
|
759
|
+
}
|
|
760
|
+
},
|
|
761
|
+
overall: {
|
|
762
|
+
...this.totalStats,
|
|
763
|
+
totalRequests: this.totalStats.l1Hits + this.totalStats.l2Hits +
|
|
764
|
+
this.totalStats.l3Hits + this.totalStats.totalMisses,
|
|
765
|
+
overallHitRate: (() => {
|
|
766
|
+
const total = this.totalStats.l1Hits + this.totalStats.l2Hits +
|
|
767
|
+
this.totalStats.l3Hits + this.totalStats.totalMisses;
|
|
768
|
+
const hits = this.totalStats.l1Hits + this.totalStats.l2Hits + this.totalStats.l3Hits;
|
|
769
|
+
return total > 0 ? ((hits / total) * 100).toFixed(2) + '%' : 'N/A';
|
|
770
|
+
})(),
|
|
771
|
+
distribution: {
|
|
772
|
+
l1HitRate: (() => {
|
|
773
|
+
const t = this.totalStats.l1Hits + this.totalStats.totalMisses;
|
|
774
|
+
return t > 0 ? ((this.totalStats.l1Hits / t) * 100).toFixed(2) + '%' : '0%';
|
|
775
|
+
})(),
|
|
776
|
+
l2HitRate: (() => {
|
|
777
|
+
const t = this.totalStats.l2Hits + this.totalStats.totalMisses;
|
|
778
|
+
return t > 0 ? ((this.totalStats.l2Hits / t) * 100).toFixed(2) + '%' : '0%';
|
|
779
|
+
})(),
|
|
780
|
+
l3HitRate: (() => {
|
|
781
|
+
const t = this.totalStats.l3Hits + this.totalStats.totalMisses;
|
|
782
|
+
return t > 0 ? ((this.totalStats.l3Hits / t) * 100).toFixed(2) + '%' : '0%';
|
|
783
|
+
})()
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
};
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
/**
|
|
790
|
+
* 手动触发过期缓存清理
|
|
791
|
+
* @returns {Promise<number>} 清理的条目数
|
|
792
|
+
*/
|
|
793
|
+
async cleanup() {
|
|
794
|
+
const count = this.L3.cleanupExpired();
|
|
795
|
+
log('info', `[Cache] Manual cleanup completed: ${count} expired entries removed`);
|
|
796
|
+
return count;
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
/**
|
|
800
|
+
* 检查键是否存在(任意级别)
|
|
801
|
+
* @param {string} key - 缓存键
|
|
802
|
+
* @returns {Promise<boolean>}
|
|
803
|
+
*/
|
|
804
|
+
async has(key) {
|
|
805
|
+
return this.L1.has(key) || this.L2.has(key) || this.L3.get(key) !== null;
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
/**
|
|
809
|
+
* 销毁缓存系统,释放资源
|
|
810
|
+
* @returns {Promise<void>}
|
|
811
|
+
*/
|
|
812
|
+
async destroy() {
|
|
813
|
+
// 停止清理任务
|
|
814
|
+
if (this._cleanupInterval) {
|
|
815
|
+
clearInterval(this._cleanupInterval);
|
|
816
|
+
this._cleanupInterval = null;
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
// 清空所有缓存
|
|
820
|
+
await this.clearAll();
|
|
821
|
+
|
|
822
|
+
log('info', '[Cache] Cache system destroyed');
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
/**
|
|
827
|
+
* 创建SystemCache实例的工厂函数
|
|
828
|
+
* @param {Object} config - 配置对象
|
|
829
|
+
* @returns {SystemCache} 缓存实例
|
|
830
|
+
*
|
|
831
|
+
* @example
|
|
832
|
+
* ```javascript
|
|
833
|
+
* import { createSystemCache } from './lib/cache/system-cache.js';
|
|
834
|
+
*
|
|
835
|
+
* const cache = createSystemCache({
|
|
836
|
+
* session: { maxSize: 200 },
|
|
837
|
+
* project: { ttl: 60 * 60 * 1000 } // 1小时
|
|
838
|
+
* });
|
|
839
|
+
*
|
|
840
|
+
* // 使用缓存
|
|
841
|
+
* await cache.set('spec:feat-001', specData);
|
|
842
|
+
* const data = await cache.get('spec:feat-001');
|
|
843
|
+
*
|
|
844
|
+
* // 查看统计
|
|
845
|
+
* console.log(await cache.stats());
|
|
846
|
+
* ```
|
|
847
|
+
*/
|
|
848
|
+
export function createSystemCache(config = {}) {
|
|
849
|
+
return new SystemCache(config);
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
export default SystemCache;
|