Skip to content

[TwigComponent] Add #[ComponentCache] attribute for persistent computed properties#3666

Open
xDeSwa wants to merge 1 commit into
symfony:3.xfrom
xDeSwa:componentCache
Open

[TwigComponent] Add #[ComponentCache] attribute for persistent computed properties#3666
xDeSwa wants to merge 1 commit into
symfony:3.xfrom
xDeSwa:componentCache

Conversation

@xDeSwa

@xDeSwa xDeSwa commented Jun 8, 2026

Copy link
Copy Markdown
Contributor
Q A
Bug fix? no
New feature? yes
Deprecations? no
Documentation? yes
Issues #
License MIT

Description

This PR introduces the #[ComponentCache] attribute, which supercharges the existing computed properties system by adding persistent cross-request caching using Symfony Cache.

While the current computed proxy memoizes results during a single request, there was no native way to persistently cache heavy component methods (like complex database queries or API calls) without polluting the component with manual CachePool injections.

With this feature, developers can easily cache the return value of any component method across requests directly from the component class, while keeping the Twig syntax ({{ computed.method }}) exactly the same.

Usage Example

use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
use Symfony\UX\TwigComponent\Attribute\ComponentCache;

#[AsTwigComponent]
class DashboardStats
{
    // Caches the return value in the 'cache.app' pool for 1 hour.
    #[ComponentCache(expiresAfter: 3600, tags: ['dashboard_stats'])]
    public function getHeavyMetrics(): array
    {
        return $this->repository->calculateComplexMetrics();
    }
}

In the template, it is accessed seamlessly via the existing proxy:

{# The first visitor triggers the DB query. For the next hour, it's served from the persistent cache in 0ms. #}
Total Users: {{ computed.heavyMetrics.totalUsers }}

Key Features

  • Auto-Key Generation: If no key is provided, a unique cache key is automatically generated using the component's class name, method name, and a hash of its public properties (get_object_vars()). This ensures the cache invalidates safely when LiveComponent props (e.g., page, filters) change!
  • Dynamic Pool Support: Supports injecting specific cache pools (e.g. pool: 'cache.redis'). Falls back to cache.app by default.
  • Tags & Expiration: Supports tags (for TagAware adapters) and expiresAfter (seconds or DateInterval).
  • LiveComponent Compatible: Works flawlessly inside LiveComponents since it hooks directly into ComputedPropertiesProxy.

@carsonbot carsonbot added Documentation Improvements or additions to documentation Feature New Feature TwigComponent Status: Needs Review Needs to be reviewed labels Jun 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Documentation Improvements or additions to documentation Feature New Feature Status: Needs Review Needs to be reviewed TwigComponent

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants