Skip to content

fix: cache the entity registry per filament panel - #289

Merged
ManukMinasyan merged 3 commits into
3.xfrom
fix/cache-entity-registry-per-panel
Nov 6, 2026
Merged

ManukMinasyan merged 3 commits into
3.xfrom
fix/cache-entity-registry-per-panel

Conversation

@ManukMinasyan

Copy link
Copy Markdown
Member

An app with more than one Filament panel can now turn on the entity cache safely. Before, the cache served every panel the registry built by whichever panel populated it first. For example, a request in a panel without a model's resource could warm the cache. Every other panel then lost that model's resource class, its title attribute (falling back to id) and its search attributes for the full TTL. Record-lookup fields showed IDs instead of names and searched nothing. Record links could point at a resource the current panel does not register.

This PR also adds custom-fields:clear-cache so deploys can drop a registry built from the previous configuration, and makes the configured TTL take effect.

What changed

  • EntityManager: the persistent cache key is now custom_fields_entities:<panel id>, taken from Filament::getCurrentOrDefaultPanel(), with no-panel when no default panel exists. The in-memory memo is keyed the same way, so a long-lived manager (a queue worker running import jobs for several panels) serves each panel its own registry.
  • EntityDiscovery: drops its own discoveredCache memo. It held the first panel's resources for the life of the singleton, and the manager's per-panel memo already prevents rebuilds.
  • TTL: EntityServiceProvider passes cache_ttl from EntityConfigurator::cache($enabled, $ttl) into EntityManager. Before, the value was read and dropped, and a hardcoded 3600 applied.
  • custom-fields:clear-cache (ClearCacheCommand): forgets the registry key of every registered panel plus no-panel. It does nothing when caching is off.
  • Docs (essentials/configuration): the performance section shows the TTL and the deploy-time clear.

Why this way

The registry is per panel by nature. EntityDiscovery::discoverFromFilamentResources() and EntityConfigurationData::resolveIconFromFilamentResource() read Filament::getResources() of the current panel. RecordColumn and RecordEntry then build links with $resourceClass::getUrl(). A single panel-independent registry would hand one panel links to resources it does not register, so keying by panel keeps the current per-panel behaviour and just caches it.

The command is not hooked into optimize or optimize:clear. A build step that runs optimize without the cache store reachable would fail. Apps add it to their deploy commands instead.

Behavior to know about

  • With caching on, the registry is built once per panel per TTL instead of once overall.
  • After upgrading, an old global custom_fields_entities key is never read again and expires within its TTL.
  • With caching off, behaviour is unchanged, except that a manager now rebuilds when the current panel changes within one process.
  • FieldTypeConfigurator::cache() is untouched. Nothing reads its values today, but removing the method would break every config file that calls it.
  • Upgrade\Steps\ClearCachesStep still forgets an old key name (custom-fields:entity-registry). It runs right after cache:clear, so it has no effect either way, and this PR leaves it alone.
  • New command, so the next release is a minor. No migration, no new config key.

Tests

tests/Feature/EntityRegistryCacheTest.php registers a second panel without PostResource:

  • Each panel gets the registry built from its own resources, whichever panel builds first.
  • One manager instance keeps the panels apart across a panel switch, and the cache entry for the second panel never holds the first panel's entities.
  • The registry stays cached for the configured TTL and expires after it.
  • custom-fields:clear-cache removes both panels' keys, which the test confirms exist first.

All four fail on 3.x and pass here.

Checked

On baaed3d5:

  • vendor/bin/pest --parallel: 1127 passed, 3 todo. PHPStan reports no errors; Rector and Pint pass.
  • Applied the branch to an app with four panels and caching on, and ran three separate processes on one Redis store. A request in a second panel built its own registry, and the default panel's next request still read the full registry: resource class, title attribute name, two search attributes. Switching panels inside one process gave each panel its own registry. A warm cached read took 2.3 ms; the uncached build takes about 21 ms there. custom-fields:clear-cache removed every panel's key.

Not checked

  • 4.x has the same code at these lines and needs the same change in a separate PR.

Discovery builds the registry from the current panel's resources, but the cache key was shared, so a multi-panel app served every panel the registry of whichever panel built it first. The key now carries the panel id, and the configured ttl reaches the cache.
Clears the cached entity registry of every panel, so a deploy can drop a registry built from the previous configuration.
A long-lived manager, such as a queue worker running import jobs for several panels, kept serving the first panel's registry and could write it under another panel's cache key. The manager's memo is now keyed by panel id, and discovery no longer keeps its own memo.
Copilot AI balanced review requested due to automatic review settings November 6, 2026 16:10

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@ManukMinasyan
ManukMinasyan added this pull request to the merge queue Nov 6, 2026
Merged via the queue into 3.x with commit c68d9e4 Nov 6, 2026
7 checks passed
@ManukMinasyan
ManukMinasyan deleted the fix/cache-entity-registry-per-panel branch November 6, 2026 23:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants