cacheSerializationResults
This commit is contained in:
parent
d4226ab83f
commit
b64bebf18f
2 changed files with 25 additions and 0 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -60,3 +60,4 @@ com_crashlytics_export_strings.xml
|
||||||
crashlytics.properties
|
crashlytics.properties
|
||||||
crashlytics-build.properties
|
crashlytics-build.properties
|
||||||
fabric.properties
|
fabric.properties
|
||||||
|
.dart_tool
|
24
lib/src/serializer.dart
Normal file
24
lib/src/serializer.dart
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
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(
|
||||||
|
{Duration timeout, FutureOr<bool> Function(RequestContext, ResponseContext, Object) shouldCache}) {
|
||||||
|
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;
|
||||||
|
};
|
||||||
|
}
|
Loading…
Reference in a new issue