@@ -26,6 +26,9 @@ class Adapter
2626 /** @var bool Skip SSRF validation for trusted backends */
2727 protected bool $ skipValidation = false ;
2828
29+ /** @var int Routing cache TTL in seconds (0 disables caching) */
30+ protected int $ cacheTTL = 0 ;
31+
2932 /** @var int Activity tracking interval in seconds */
3033 protected int $ interval = 30 ;
3134
@@ -82,6 +85,13 @@ public function setSkipValidation(bool $skip): static
8285 return $ this ;
8386 }
8487
88+ public function setCacheTtl (int $ seconds ): static
89+ {
90+ $ this ->cacheTTL = $ seconds ;
91+
92+ return $ this ;
93+ }
94+
8595 /**
8696 * Notify connect event
8797 *
@@ -174,20 +184,23 @@ public function getProtocol(): Protocol
174184 */
175185 public function route (string $ resourceId ): ConnectionResult
176186 {
177- $ cached = $ this ->router ->get ($ resourceId );
178187 $ now = \time ();
179188
180- if ($ cached !== false && \is_array ($ cached )) {
181- /** @var array{endpoint: string, updated: int} $cached */
182- if (($ now - $ cached ['updated ' ]) < 1 ) {
183- $ this ->stats ['cacheHits ' ]++;
184- $ this ->stats ['connections ' ]++;
189+ if ($ this ->cacheTTL > 0 ) {
190+ $ cached = $ this ->router ->get ($ resourceId );
185191
186- return new ConnectionResult (
187- endpoint: $ cached ['endpoint ' ],
188- protocol: $ this ->getProtocol (),
189- metadata: ['cached ' => true ]
190- );
192+ if ($ cached !== false && \is_array ($ cached )) {
193+ /** @var array{endpoint: string, updated: int} $cached */
194+ if (($ now - $ cached ['updated ' ]) < $ this ->cacheTTL ) {
195+ $ this ->stats ['cacheHits ' ]++;
196+ $ this ->stats ['connections ' ]++;
197+
198+ return new ConnectionResult (
199+ endpoint: $ cached ['endpoint ' ],
200+ protocol: $ this ->getProtocol (),
201+ metadata: ['cached ' => true ]
202+ );
203+ }
191204 }
192205 }
193206
@@ -227,10 +240,12 @@ public function route(string $resourceId): ConnectionResult
227240 $ this ->validate ($ endpoint );
228241 }
229242
230- $ this ->router ->set ($ resourceId , [
231- 'endpoint ' => $ endpoint ,
232- 'updated ' => $ now ,
233- ]);
243+ if ($ this ->cacheTTL > 0 ) {
244+ $ this ->router ->set ($ resourceId , [
245+ 'endpoint ' => $ endpoint ,
246+ 'updated ' => $ now ,
247+ ]);
248+ }
234249
235250 $ this ->stats ['connections ' ]++;
236251
0 commit comments