@@ -29,13 +29,17 @@ class Tags
29
29
30
30
protected Collection $ tags ;
31
31
32
+ protected Collection $ invalidationDispatched ;
33
+
32
34
public Collection $ processedTags ;
33
35
34
36
public function __construct ()
35
37
{
36
38
$ this ->tags = collect ();
37
39
38
40
$ this ->processedTags = collect ();
41
+
42
+ $ this ->invalidationDispatched = collect ();
39
43
}
40
44
41
45
public function addTag (Model $ model ): void
@@ -182,14 +186,6 @@ public function storeCacheTags(
182
186
->map (fn (mixed $ item ) => "' " . Helpers::toString ($ item ) . "' " )
183
187
->join (', ' );
184
188
185
- $ this ->dbStatement ("
186
- update edge_flush_tags
187
- set obsolete = false
188
- where index in ( {$ indexes })
189
- and is_valid = true
190
- and obsolete = false
191
- " );
192
-
193
189
$ this ->dbStatement ("
194
190
update edge_flush_urls
195
191
set was_purged_at = null,
@@ -201,13 +197,26 @@ public function storeCacheTags(
201
197
from edge_flush_tags
202
198
where index in ( {$ indexes })
203
199
and is_valid = true
204
- and obsolete = false
200
+ and obsolete = true
205
201
)
206
202
" );
203
+
204
+ $ this ->dbStatement ("
205
+ update edge_flush_tags
206
+ set obsolete = false
207
+ where index in ( {$ indexes })
208
+ and is_valid = true
209
+ and obsolete = true
210
+ " );
207
211
}
208
212
}
209
213
210
214
public function dispatchInvalidationsForModel (Collection |string |Model $ models ): void {
215
+ Helpers::debug ([
216
+ 'dispatchInvalidationsForModel - models: ' ,
217
+ $ models ->map (fn (Model $ model ) => get_class ($ model )." ( {$ model ->id }) " )->implode (', ' )
218
+ ]);
219
+
211
220
if (blank ($ models )) {
212
221
return ;
213
222
}
@@ -218,25 +227,25 @@ public function dispatchInvalidationsForModel(Collection|string|Model $models):
218
227
return ;
219
228
}
220
229
221
- /**
222
- * @var Model $model
223
- */
224
- $ model = $ models instanceof Collection
225
- ? $ models ->first ()
226
- : $ models ;
230
+ $ models = $ this ->onlyValidModels ($ models );
227
231
228
- if (!$ this ->modelBelongsToProject ($ model )) {
232
+ $ models = $ this ->notYetDispatched ($ models );
233
+
234
+ if ($ models ->isEmpty ()) {
229
235
return ;
230
236
}
231
237
238
+ /**
239
+ * @var Model $model
240
+ */
241
+ $ model = $ models ->first ();
242
+
232
243
$ model ->wasRecentlyCreated
233
244
? $ this ->dispatchInvalidationsForCreatedModel ($ models )
234
245
: $ this ->dispatchInvalidationsForUpdatedModel ($ models );
235
246
}
236
247
237
- public function dispatchInvalidationsForCreatedModel (
238
- Collection |string |Model $ models
239
- ): void {
248
+ public function dispatchInvalidationsForCreatedModel (Collection $ models ): void {
240
249
/**
241
250
* @var string $strategy
242
251
*/
@@ -251,9 +260,7 @@ public function dispatchInvalidationsForCreatedModel(
251
260
throw new \Exception ("Strategy ' {$ strategy }' Not implemented " );
252
261
}
253
262
254
- public function dispatchInvalidationsForUpdatedModel (
255
- Collection |string |Model $ models
256
- ): void {
263
+ public function dispatchInvalidationsForUpdatedModel (Collection $ models ): void {
257
264
/**
258
265
* @var string $strategy
259
266
*/
@@ -269,23 +276,6 @@ public function dispatchInvalidationsForUpdatedModel(
269
276
throw new \Exception ("Strategy ' {$ strategy }' Not implemented " );
270
277
}
271
278
272
- if (blank ($ models )) {
273
- return ;
274
- }
275
-
276
- $ models =
277
- $ models instanceof Model ? collect ([$ models ]) : collect ($ models );
278
-
279
- $ models = $ models ->filter (
280
- fn ($ model ) => $ this ->tagIsNotExcluded (
281
- $ model instanceof Model ? get_class ($ model ) : $ model ,
282
- ),
283
- );
284
-
285
- if ($ models ->isEmpty ()) {
286
- return ;
287
- }
288
-
289
279
Helpers::debug (
290
280
'INVALIDATING tags for models: ' .
291
281
$ models
@@ -448,6 +438,8 @@ public function invalidateAll(bool $force = false): Invalidation
448
438
$ count = 0 ;
449
439
450
440
do {
441
+ Helpers::debug ('Invalidating all tags... -> ' .$ count );
442
+
451
443
if ($ count ++ > 0 ) {
452
444
sleep (2 );
453
445
}
@@ -682,10 +674,38 @@ public function markUrlsAsWarmed(Collection $urls): void
682
674
$ this ->dbStatement ($ sql );
683
675
}
684
676
685
- public function modelBelongsToProject (Model $ model ): bool
677
+ public function onlyValidModels ($ models )
678
+ {
679
+ $ models =
680
+ $ models instanceof Model ? collect ([$ models ]) : collect ($ models );
681
+
682
+ return $ models ->filter (
683
+ fn ($ model ) => $ this ->tagIsNotExcluded (
684
+ $ model instanceof Model ? get_class ($ model ) : $ model ,
685
+ ),
686
+ );
687
+ }
688
+
689
+ public function notYetDispatched ($ models )
686
690
{
687
- $ ignore = config ('edge-flush.invalidations.models.ignore ' , []);
691
+ $ tags = $ models ->mapWithKeys (
692
+ function ($ model ) {
693
+ $ tag = $ this ->makeModelName (
694
+ $ model
695
+ );
696
+
697
+ return [$ tag => $ tag ];
698
+ }
699
+ );
700
+
701
+ $ missing = $ tags ->diff ($ this ->invalidationDispatched );
702
+
703
+ $ this ->invalidationDispatched = $ this ->invalidationDispatched ->merge (
704
+ $ missing
705
+ );
688
706
689
- return !in_array (get_class ($ model ), (array ) $ ignore );
707
+ return $ models ->filter (fn ($ model ) => $ missing ->contains (
708
+ $ this ->makeModelName ($ model )
709
+ ));
690
710
}
691
711
}
0 commit comments