I have custom controllers which provide
CacheableJsonResponse responses. Those responses are quite heavy to build (couple of seconds), so I want to build them better. I build those responses by getting the entity object(s), normalizing its content—referenced entities included—and putting the data directly into the response object.
My JSON responses varies by user, checks the role access, and includes some user related content (e.g. flags). I have tried to put a cache context on user (and url) on the
CacheableJsonResponse, but this is not a viable way to handle cache, from my perspective; it would create a cache entry for each user and url request and would be ineffective (the cache will HIT only when the same request is asked twice or more by the same user).
Standard Drupal renderer seems to include a placeholder system which makes the majority of the page cached with proper cache context+tags, and the little parts with high-cardinality/invalidation rate rendered last out of the cache system (from my understanding) and then would be useful for my needs.
How can I easily reproduce the same system for my controllers? Is there any standard mechanism/pattern out there?