Problem/Motivation
The idea is to
- Provide a way to attach cacheability metadata to the return values of normalizers (as described in #3028080: Add CacheableNormalization for Normalizer to return Normalized value and Cacheablity, but implemented just for this module)
- Provide a cached normalizer service that automatically caches the return values, using the format, context array and cacheability metadata attached to the return value as cache contexts.
This would be kind of like the dynamic page cache, improving performance in API endpoints where whole responses cannot be cached due to high cardinality cache contexts like session and user.
Issue fork api_toolkit-3370423
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
dieterholvoet commentedComment #4
dieterholvoet commentedThe MR has a working version now. Still testing, not yet using in a production environment.
Comment #5
dieterholvoet commentedIn Symfony 6
Symfony\Component\Serializer\Normalizer\NormalizerInterfacegot more function argument types, including onemixedtype. In order to support both Symfony 5 and 6, we'll have to add those types toDrupal\api_toolkit\Normalizer\CachedNormalizer, which means that we'll have to bump the minimum PHP version to 8.0 if we want to add themixedtype. I think that's reasonable.Comment #6
dieterholvoet commentedWe should recommend using a Permanent Cache Bin backend for the normalizer cache, to make it more efficient. This needs to be added to settings.php:
Comment #7
dieterholvoet commentedI added something new, an experiment: a way to do placeholdering with lazy builders, similar to how it's already possible in other places in Drupal using
#lazy_builder. It can be used to add highly dynamic data to normalization results while keeping their cacheability. Here's an example in action:Comment #8
dieterholvoet commentedThe previous approach didn't replace placeholders in case of nested normalizers, so I rewrote part of the implementation. The end result is a lot simpler:
Entity, field item list & field item arguments that are passed to placeholder callbacks are automatically normalized to a simple string format and the entity in question is reloaded from the database before placeholder replacement happens. A side result of the current implementation is that you can now also pass
[$this->serializer, 'normalize']to a placeholder, which will cause the nested normalizations to not be part of the cached normalization anymore, which should in turn decrease cache sizes in case of a lot of nested normalizations.Comment #10
dieterholvoet commented