Skip to content

Commit 64ee912

Browse files
fix: prevent cache deletion during Registry::load()
The Registry::load() method was incorrectly calling clear() which deleted the cache before attempting to read from it, making caching completely ineffective. Modified clear() to accept an optional $includeCache parameter (default: true) to maintain backward compatibility while allowing load() to clear only local elements without destroying the cache.
1 parent a1f67ab commit 64ee912

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/Registry.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function load(): void
8585
return;
8686
}
8787

88-
$this->clear();
88+
$this->clear(false);
8989

9090
try {
9191
$cached = $this->cache->get(self::DISCOVERED_ELEMENTS_CACHE_KEY);
@@ -176,8 +176,6 @@ public function load(): void
176176
$this->logger->debug("Loaded {$loadCount} elements from cache.");
177177
} catch (CacheInvalidArgumentException $e) {
178178
$this->logger->error('Invalid registry cache key used.', ['key' => self::DISCOVERED_ELEMENTS_CACHE_KEY, 'exception' => $e]);
179-
} catch (DefinitionException $e) {
180-
$this->logger->error('Error hydrating definition from cache.', ['exception' => $e]);
181179
} catch (Throwable $e) {
182180
$this->logger->error('Unexpected error loading from cache.', ['key' => self::DISCOVERED_ELEMENTS_CACHE_KEY, 'exception' => $e]);
183181
}
@@ -351,9 +349,14 @@ public function hasElements(): bool
351349
|| ! empty($this->resourceTemplates);
352350
}
353351

354-
public function clear(): void
352+
/**
353+
* Clear discovered elements from registry
354+
*
355+
* @param bool $includeCache Whether to clear the cache as well (default: true)
356+
*/
357+
public function clear(bool $includeCache = true): void
355358
{
356-
if ($this->cache !== null) {
359+
if ($includeCache && $this->cache !== null) {
357360
try {
358361
$this->cache->delete(self::DISCOVERED_ELEMENTS_CACHE_KEY);
359362
$this->logger->debug('Registry cache cleared.');
@@ -438,24 +441,24 @@ public function getPrompt(string $name): ?RegisteredPrompt
438441
/** @return array<string, Tool> */
439442
public function getTools(): array
440443
{
441-
return array_map(fn ($tool) => $tool->schema, $this->tools);
444+
return array_map(fn($tool) => $tool->schema, $this->tools);
442445
}
443446

444447
/** @return array<string, Resource> */
445448
public function getResources(): array
446449
{
447-
return array_map(fn ($resource) => $resource->schema, $this->resources);
450+
return array_map(fn($resource) => $resource->schema, $this->resources);
448451
}
449452

450453
/** @return array<string, Prompt> */
451454
public function getPrompts(): array
452455
{
453-
return array_map(fn ($prompt) => $prompt->schema, $this->prompts);
456+
return array_map(fn($prompt) => $prompt->schema, $this->prompts);
454457
}
455458

456459
/** @return array<string, ResourceTemplate> */
457460
public function getResourceTemplates(): array
458461
{
459-
return array_map(fn ($template) => $template->schema, $this->resourceTemplates);
462+
return array_map(fn($template) => $template->schema, $this->resourceTemplates);
460463
}
461464
}

0 commit comments

Comments
 (0)