cacheSerializationResults function

RequestHandler cacheSerializationResults({
  1. Duration? timeout,
  2. FutureOr<bool> shouldCache(
    1. RequestContext,
    2. ResponseContext,
    3. Object
    )?,
})

A middleware that enables the caching of response serialization.

This can improve the performance of sending objects that are complex to serialize.

You can pass a shouldCache callback to determine which values should be cached.

Implementation

RequestHandler cacheSerializationResults(
    {Duration? timeout,
    FutureOr<bool> Function(RequestContext, ResponseContext, Object)?
        shouldCache}) {
  return (RequestContext req, ResponseContext res) async {
    var oldSerializer = res.serializer;

    // TODO: Commented out as it is not doing anything useful
    var cache = <dynamic, String>{};

    res.serializer = (value) {
      if (shouldCache == null) {
        return cache.putIfAbsent(value, () => oldSerializer(value) as String);
      }

      return oldSerializer(value);
    };

    return true;
  };
}