11use dashmap:: DashMap ;
22use ntex:: web:: HttpResponse ;
33use redis:: Commands ;
4- use sonic_rs:: json;
54
65use crate :: {
76 plugins:: traits:: {
8- ControlFlow , OnExecuteEnd , OnExecuteEndPayload , OnExecuteStart , OnExecuteStartPayload ,
9- OnSchemaReload , OnSchemaReloadPayload ,
7+ ControlFlow , OnExecutePayload , OnSchemaReloadPayload , RouterPlugin
108 } ,
119 utils:: consts:: TYPENAME_FIELD_NAME ,
1210} ;
@@ -26,20 +24,15 @@ impl ResponseCachePlugin {
2624 }
2725}
2826
29- pub struct ResponseCacheContext {
30- key : String ,
31- }
32-
33- impl OnExecuteStart for ResponseCachePlugin {
34- fn on_execute_start ( & self , payload : OnExecuteStartPayload ) -> ControlFlow {
27+ impl RouterPlugin for ResponseCachePlugin {
28+ fn on_execute < ' exec > (
29+ & self ,
30+ payload : OnExecutePayload < ' exec > ,
31+ ) -> ControlFlow < ' exec , OnExecutePayload < ' exec > > {
3532 let key = format ! (
3633 "response_cache:{}:{:?}" ,
3734 payload. query_plan, payload. variable_values
3835 ) ;
39- payload
40- . router_http_request
41- . extensions_mut ( )
42- . insert ( ResponseCacheContext { key : key. clone ( ) } ) ;
4336 if let Ok ( mut conn) = self . redis_client . get_connection ( ) {
4437 let cached_response: Option < Vec < u8 > > = conn. get ( & key) . ok ( ) ;
4538 if let Some ( cached_response) = cached_response {
@@ -49,24 +42,12 @@ impl OnExecuteStart for ResponseCachePlugin {
4942 . body ( cached_response) ,
5043 ) ;
5144 }
52- }
53- ControlFlow :: Continue
54- }
55- }
45+ ControlFlow :: OnEnd ( Box :: new ( move |payload : OnExecutePayload | {
46+ // Do not cache if there are errors
47+ if !payload. errors . is_empty ( ) {
48+ return ControlFlow :: Continue ;
49+ }
5650
57- impl OnExecuteEnd for ResponseCachePlugin {
58- fn on_execute_end ( & self , payload : OnExecuteEndPayload ) -> ControlFlow {
59- // Do not cache if there are errors
60- if !payload. errors . is_empty ( ) {
61- return ControlFlow :: Continue ;
62- }
63- if let Some ( key) = payload
64- . router_http_request
65- . extensions ( )
66- . get :: < ResponseCacheContext > ( )
67- . map ( |ctx| & ctx. key )
68- {
69- if let Ok ( mut conn) = self . redis_client . get_connection ( ) {
7051 if let Ok ( serialized) = sonic_rs:: to_vec ( & payload. data ) {
7152 // Decide on the ttl somehow
7253 // Get the type names
@@ -93,18 +74,16 @@ impl OnExecuteEnd for ResponseCachePlugin {
9374 // Insert the ttl into extensions for client awareness
9475 payload
9576 . extensions
96- . insert ( "response_cache_ttl" . to_string ( ) , json ! ( max_ttl) ) ;
77+ . insert ( "response_cache_ttl" . to_string ( ) , sonic_rs :: json!( max_ttl) ) ;
9778
9879 // Set the cache with the decided ttl
9980 let _: ( ) = conn. set_ex ( key, serialized, max_ttl) . unwrap_or ( ( ) ) ;
10081 }
101- }
82+ ControlFlow :: Continue
83+ } ) ) ;
10284 }
10385 ControlFlow :: Continue
10486 }
105- }
106-
107- impl OnSchemaReload for ResponseCachePlugin {
10887 fn on_schema_reload ( & self , payload : OnSchemaReloadPayload ) {
10988 // Visit the schema and update ttl_per_type based on some directive
11089 payload
0 commit comments