





















@@ -7,6 +7,7 @@ import { buildPluginApi } from "./api-builder.js";
77import { collectPluginConfigContractMatches } from "./config-contracts.js";
88import { getCachedPluginJitiLoader, type PluginJitiLoaderCache } from "./jiti-loader-cache.js";
99import type { PluginManifestRecord } from "./manifest-registry.js";
10+import { PluginLruCache, type PluginLruCacheResult } from "./plugin-lru-cache.js";
1011import { loadPluginManifestRegistryForPluginRegistry } from "./plugin-registry.js";
1112import { resolvePluginCacheInputs } from "./roots.js";
1213import type { PluginRuntime } from "./runtime/types.js";
@@ -86,20 +87,22 @@ const NOOP_LOGGER: PluginLogger = {
8687const MAX_SETUP_LOOKUP_CACHE_ENTRIES = 128;
87888889const jitiLoaders: PluginJitiLoaderCache = new Map();
89-const setupRegistryCache = new Map<string, PluginSetupRegistry>();
90-const setupProviderCache = new Map<string, ProviderPlugin | null>();
91-const setupCliBackendCache = new Map<string, SetupCliBackendEntry | null>();
92-let setupLookupCacheEntryCap = MAX_SETUP_LOOKUP_CACHE_ENTRIES;
90+const setupRegistryCache = new PluginLruCache<PluginSetupRegistry>(MAX_SETUP_LOOKUP_CACHE_ENTRIES);
91+const setupProviderCache = new PluginLruCache<ProviderPlugin | null>(
92+MAX_SETUP_LOOKUP_CACHE_ENTRIES,
93+);
94+const setupCliBackendCache = new PluginLruCache<SetupCliBackendEntry | null>(
95+MAX_SETUP_LOOKUP_CACHE_ENTRIES,
96+);
93979498export const __testing = {
9599get maxSetupLookupCacheEntries() {
96-return setupLookupCacheEntryCap;
100+return setupRegistryCache.maxEntries;
97101},
98102setMaxSetupLookupCacheEntriesForTest(value?: number) {
99-setupLookupCacheEntryCap =
100-typeof value === "number" && Number.isFinite(value) && value > 0
101- ? Math.max(1, Math.floor(value))
102- : MAX_SETUP_LOOKUP_CACHE_ENTRIES;
103+setupRegistryCache.setMaxEntriesForTest(value);
104+setupProviderCache.setMaxEntriesForTest(value);
105+setupCliBackendCache.setMaxEntriesForTest(value);
103106},
104107getCacheSizes() {
105108return {
@@ -125,31 +128,12 @@ function getJiti(modulePath: string) {
125128});
126129}
127130128-function getCachedSetupValue<T>(
129-cache: Map<string, T>,
130-key: string,
131-): { hit: true; value: T } | { hit: false } {
132-if (!cache.has(key)) {
133-return { hit: false };
134-}
135-const cached = cache.get(key) as T;
136-cache.delete(key);
137-cache.set(key, cached);
138-return { hit: true, value: cached };
131+function getCachedSetupValue<T>(cache: PluginLruCache<T>, key: string): PluginLruCacheResult<T> {
132+return cache.getResult(key);
139133}
140134141-function setCachedSetupValue<T>(cache: Map<string, T>, key: string, value: T): void {
142-if (cache.has(key)) {
143-cache.delete(key);
144-}
135+function setCachedSetupValue<T>(cache: PluginLruCache<T>, key: string, value: T): void {
145136cache.set(key, value);
146-while (cache.size > setupLookupCacheEntryCap) {
147-const oldestKey = cache.keys().next().value;
148-if (typeof oldestKey !== "string") {
149-break;
150-}
151-cache.delete(oldestKey);
152-}
153137}
154138155139function buildSetupRegistryCacheKey(params: {
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。