platform/packages/cache/lib/src/serializer.dart

29 lines
905 B
Dart
Raw Normal View History

2018-04-02 01:11:40 +00:00
import 'dart:async';
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(
{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
//var cache = <dynamic, String>{};
2018-04-02 01:11:40 +00:00
res.serializer = (value) {
2021-02-14 05:22:25 +00:00
//if (shouldCache == null) {
// return cache.putIfAbsent(value, () => oldSerializer(value));
//}
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;
};
}