File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 33const fastJsonStringify = require ( 'fast-json-stringify' )
44
55function SerializerSelector ( ) {
6+ const cache = new Map ( )
67 return function buildSerializerFactory ( externalSchemas , serializerOpts ) {
78 const fjsOpts = Object . assign ( { } , serializerOpts , { schema : externalSchemas } )
8- return responseSchemaCompiler . bind ( null , fjsOpts )
9+ return function cachedResponseSchemaCompiler ( opts ) {
10+ const key = JSON . stringify ( opts . schema )
11+ const cached = cache . get ( key )
12+ if ( cached ) return cached
13+ const result = responseSchemaCompiler ( fjsOpts , opts )
14+ cache . set ( key , result )
15+ return result
16+ }
917 }
1018}
1119
Original file line number Diff line number Diff line change @@ -36,6 +36,20 @@ test('basic usage', t => {
3636 t . assert . equal ( result , '{"name":"hello"}' )
3737} )
3838
39+ test ( 'cache hit for identical schemas across factories' , t => {
40+ t . plan ( 2 )
41+ const factory = FjsCompiler ( )
42+
43+ const compiler1 = factory ( externalSchemas1 , fastifyFjsOptionsDefault )
44+ const compiler2 = factory ( externalSchemas1 , fastifyFjsOptionsDefault )
45+
46+ const serialize1 = compiler1 ( { schema : sampleSchema } )
47+ const serialize2 = compiler2 ( { schema : sampleSchema } )
48+
49+ t . assert . equal ( serialize1 , serialize2 , 'same serializer function returned from cache' )
50+ t . assert . equal ( serialize1 ( { name : 'cached' } ) , '{"name":"cached"}' )
51+ } )
52+
3953test ( 'fastify integration' , async t => {
4054 const factory = FjsCompiler ( )
4155
You can’t perform that action at this time.
0 commit comments