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

27 lines
830 B
Dart
Raw Normal View History

2018-04-02 01:11:40 +00:00
import 'dart:async';
import 'package:angel_framework/angel_framework.dart';
/// 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(
2018-10-21 09:35:04 +00:00
{Duration timeout,
FutureOr<bool> Function(RequestContext, ResponseContext, Object)
shouldCache}) {
2018-04-02 01:11:40 +00:00
return (RequestContext req, ResponseContext res) async {
var oldSerializer = res.serializer;
var cache = <dynamic, String>{};
res.serializer = (value) {
if (shouldCache == null) {
return cache.putIfAbsent(value, () => oldSerializer(value));
}
return oldSerializer(value);
};
return true;
};
}