1414
1515class WidgetController
1616{
17+ private int $ cacheTtl ;
18+
1719 public function __construct (
1820 private Twig $ view ,
1921 private ApiWrapper $ apiWrapper ,
2022 private FileManager $ fileManager ,
2123 private EventRepository $ eventRepository ,
2224 private StreamRepository $ streamRepository ,
2325 private WidgetRepository $ widgetRepository ,
24- ) {}
26+ ) {
27+ $ this ->cacheTtl = (int ) ($ _SERVER ['WIDGET_CACHE_TTL ' ] ?? 15 );
28+ }
2529
2630 // ── Helpers ───────────────────────────────────────────────────
2731
@@ -118,6 +122,15 @@ private function fetchStreamDonationData(string $streamGuid): array
118122 $ cacheData = $ this ->widgetRepository ->selectStreamDonationWidgetCacheData ($ charityStream )
119123 ?? ['amount ' => 0 , 'continuation_token ' => '' ];
120124
125+ // Si le cache est encore frais, on retourne les données en cache sans appeler l'API
126+ if ($ this ->widgetRepository ->isCacheFresh ($ cacheData , $ this ->cacheTtl )) {
127+ return ['stream ' => $ charityStream , 'result ' => [
128+ 'amount ' => $ cacheData ['amount ' ],
129+ 'donations ' => [],
130+ 'continuation_token ' => $ cacheData ['continuation_token ' ],
131+ ]];
132+ }
133+
121134 $ result = $ this ->apiWrapper ->getAllOrders (
122135 $ charityStream ->organization_slug ,
123136 $ charityStream ->form_slug ,
@@ -131,6 +144,12 @@ private function fetchStreamDonationData(string $streamGuid): array
131144 'amount ' => $ result ['amount ' ],
132145 'continuation_token ' => $ result ['continuation_token ' ],
133146 ]);
147+ } else {
148+ // Même si rien n'a changé, on met à jour le timestamp du cache
149+ $ this ->widgetRepository ->updateStreamDonationWidgetCacheData ($ charityStream ->guid , [
150+ 'amount ' => $ cacheData ['amount ' ],
151+ 'continuation_token ' => $ cacheData ['continuation_token ' ],
152+ ]);
134153 }
135154
136155 return ['stream ' => $ charityStream , 'result ' => $ result ];
@@ -148,12 +167,20 @@ private function fetchEventDonationData(string $eventGuid): array
148167 $ cacheData = $ this ->widgetRepository ->selectEventDonationWidgetCacheData ($ event )
149168 ?? ['amount ' => 0 , 'streams ' => []];
150169
170+ // Si le cache est encore frais, on retourne les données en cache sans appeler l'API
171+ if ($ this ->widgetRepository ->isCacheFresh ($ cacheData , $ this ->cacheTtl )) {
172+ return ['event ' => $ event , 'cacheData ' => $ cacheData ];
173+ }
174+
151175 $ streams = $ this ->streamRepository ->selectListByEvent ($ event );
152176 $ oldAmount = $ cacheData ['amount ' ];
153177 $ cacheData = $ this ->aggregateEventStreams ($ streams , $ cacheData );
154178
155179 if ($ oldAmount !== $ cacheData ['amount ' ]) {
156180 $ this ->widgetRepository ->updateEventDonationWidgetCacheData ($ event ->guid , $ cacheData );
181+ } else {
182+ // Même si rien n'a changé, on met à jour le timestamp du cache
183+ $ this ->widgetRepository ->updateEventDonationWidgetCacheData ($ event ->guid , $ cacheData );
157184 }
158185
159186 return ['event ' => $ event , 'cacheData ' => $ cacheData ];
@@ -171,6 +198,11 @@ private function fetchStreamCardData(string $streamGuid): array
171198 $ cacheData = $ this ->widgetRepository ->selectStreamCardWidgetCacheData ($ charityStream )
172199 ?? ['amount ' => 0 , 'donors ' => 0 , 'continuation_token ' => '' ];
173200
201+ // Si le cache est encore frais, on retourne les données en cache sans appeler l'API
202+ if ($ this ->widgetRepository ->isCacheFresh ($ cacheData , $ this ->cacheTtl )) {
203+ return ['stream ' => $ charityStream , 'amount ' => $ cacheData ['amount ' ], 'donors ' => $ cacheData ['donors ' ] ?? 0 ];
204+ }
205+
174206 $ result = $ this ->apiWrapper ->getAllOrders (
175207 $ charityStream ->organization_slug ,
176208 $ charityStream ->form_slug ,
@@ -188,6 +220,13 @@ private function fetchStreamCardData(string $streamGuid): array
188220 'donors ' => $ donors ,
189221 'continuation_token ' => $ result ['continuation_token ' ],
190222 ]);
223+ } else {
224+ // Même si rien n'a changé, on met à jour le timestamp du cache
225+ $ this ->widgetRepository ->updateStreamCardWidgetCacheData ($ charityStream ->guid , [
226+ 'amount ' => $ cacheData ['amount ' ],
227+ 'donors ' => $ donors ,
228+ 'continuation_token ' => $ cacheData ['continuation_token ' ],
229+ ]);
191230 }
192231
193232 return ['stream ' => $ charityStream , 'amount ' => $ result ['amount ' ], 'donors ' => $ donors ];
@@ -205,12 +244,20 @@ private function fetchEventCardData(string $eventGuid): array
205244 $ cacheData = $ this ->widgetRepository ->selectEventCardWidgetCacheData ($ event )
206245 ?? ['amount ' => 0 , 'donors ' => 0 , 'streams ' => []];
207246
247+ // Si le cache est encore frais, on retourne les données en cache sans appeler l'API
248+ if ($ this ->widgetRepository ->isCacheFresh ($ cacheData , $ this ->cacheTtl )) {
249+ return ['event ' => $ event , 'amount ' => $ cacheData ['amount ' ], 'donors ' => $ cacheData ['donors ' ] ?? 0 ];
250+ }
251+
208252 $ streams = $ this ->streamRepository ->selectListByEvent ($ event );
209253 $ oldAmount = $ cacheData ['amount ' ];
210254 $ cacheData = $ this ->aggregateEventStreams ($ streams , $ cacheData , true );
211255
212256 if ($ oldAmount !== $ cacheData ['amount ' ]) {
213257 $ this ->widgetRepository ->updateEventCardWidgetCacheData ($ event ->guid , $ cacheData );
258+ } else {
259+ // Même si rien n'a changé, on met à jour le timestamp du cache
260+ $ this ->widgetRepository ->updateEventCardWidgetCacheData ($ event ->guid , $ cacheData );
214261 }
215262
216263 return ['event ' => $ event , 'amount ' => $ cacheData ['amount ' ], 'donors ' => $ cacheData ['donors ' ]];
@@ -276,17 +323,24 @@ public function widgetAlert(Request $request, Response $response, array $args):
276323 $ cacheData = $ this ->widgetRepository ->selectAlertWidgetCacheData ($ charityStream )
277324 ?? ['continuation_token ' => '' ];
278325
279- $ result = $ this ->apiWrapper ->getAllOrders (
280- $ charityStream ->organization_slug ,
281- $ charityStream ->form_slug ,
282- 0 ,
283- $ cacheData ['continuation_token ' ],
284- );
326+ if (!$ this ->widgetRepository ->isCacheFresh ($ cacheData , $ this ->cacheTtl )) {
327+ $ result = $ this ->apiWrapper ->getAllOrders (
328+ $ charityStream ->organization_slug ,
329+ $ charityStream ->form_slug ,
330+ 0 ,
331+ $ cacheData ['continuation_token ' ],
332+ );
285333
286- if ($ cacheData ['continuation_token ' ] !== $ result ['continuation_token ' ]) {
287- $ this ->widgetRepository ->updateAlertWidgetCacheData ($ charityStream ->guid , [
288- 'continuation_token ' => $ result ['continuation_token ' ],
289- ]);
334+ if ($ cacheData ['continuation_token ' ] !== $ result ['continuation_token ' ]) {
335+ $ this ->widgetRepository ->updateAlertWidgetCacheData ($ charityStream ->guid , [
336+ 'continuation_token ' => $ result ['continuation_token ' ],
337+ ]);
338+ } else {
339+ // Même si rien n'a changé, on met à jour le timestamp du cache
340+ $ this ->widgetRepository ->updateAlertWidgetCacheData ($ charityStream ->guid , [
341+ 'continuation_token ' => $ cacheData ['continuation_token ' ],
342+ ]);
343+ }
290344 }
291345
292346 return $ this ->view ->render ($ response , 'widget/alert.html.twig ' , [
@@ -312,6 +366,15 @@ public function widgetAlertFetch(Request $request, Response $response, array $ar
312366 ?? ['continuation_token ' => '' ];
313367
314368 try {
369+ // Si le cache est encore frais, on retourne un résultat vide (pas de nouvelles donations)
370+ if ($ this ->widgetRepository ->isCacheFresh ($ cacheData , $ this ->cacheTtl )) {
371+ return $ this ->jsonResponse ($ response , [
372+ 'amount ' => 0 ,
373+ 'donations ' => [],
374+ 'continuation_token ' => $ cacheData ['continuation_token ' ],
375+ ]);
376+ }
377+
315378 $ result = $ this ->apiWrapper ->getAllOrders (
316379 $ charityStream ->organization_slug ,
317380 $ charityStream ->form_slug ,
@@ -323,6 +386,11 @@ public function widgetAlertFetch(Request $request, Response $response, array $ar
323386 $ this ->widgetRepository ->updateAlertWidgetCacheData ($ charityStream ->guid , [
324387 'continuation_token ' => $ result ['continuation_token ' ],
325388 ]);
389+ } else {
390+ // Même si rien n'a changé, on met à jour le timestamp du cache
391+ $ this ->widgetRepository ->updateAlertWidgetCacheData ($ charityStream ->guid , [
392+ 'continuation_token ' => $ cacheData ['continuation_token ' ],
393+ ]);
326394 }
327395
328396 return $ this ->jsonResponse ($ response , $ result );
0 commit comments