2018-04-02 01:11:40 +00:00
|
|
|
import 'dart:async';
|
2021-05-30 00:46:13 +00:00
|
|
|
import 'package:angel3_framework/angel3_framework.dart';
|
2018-04-02 01:11:40 +00:00
|
|
|
|
|
|
|
/// 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.
|
|
|
|
RequestHandler cacheSerializationResults(
|
2021-05-30 00:46:13 +00:00
|
|
|
{Duration? timeout,
|
|
|
|
FutureOr<bool> Function(RequestContext, ResponseContext, Object)?
|
2018-10-21 09:35:04 +00:00
|
|
|
shouldCache}) {
|
2018-04-02 01:11:40 +00:00
|
|
|
return (RequestContext req, ResponseContext res) async {
|
|
|
|
var oldSerializer = res.serializer;
|
2021-02-14 05:22:25 +00:00
|
|
|
|
|
|
|
// TODO: Commented out as it is not doing anything useful
|
2021-06-22 10:42:26 +00:00
|
|
|
var cache = <dynamic, String>{};
|
|
|
|
|
2018-04-02 01:11:40 +00:00
|
|
|
res.serializer = (value) {
|
2021-06-22 10:42:26 +00:00
|
|
|
if (shouldCache == null) {
|
|
|
|
return cache.putIfAbsent(value, () => oldSerializer(value) as String);
|
|
|
|
}
|
2018-04-02 01:11:40 +00:00
|
|
|
|
2021-06-20 13:29:23 +00:00
|
|
|
return oldSerializer(value);
|
2018-04-02 01:11:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
}
|