@@ -12,13 +12,14 @@ class SuperCacheInvalidationHelper
1212 /**
1313 * Insert a cache invalidation event into the database.
1414 *
15- * @param string $type 'key' or 'tag'
16- * @param string $identifier The cache key or tag to invalidate
17- * @param string|null $connection_name The Redis Connection name (optional, 'default')
18- * @param string|null $reason Reason for invalidation (optional)
19- * @param int|null $totalShards Total number of shards (from config if null)
20- * @param int|null $priority Priority of the event
21- * @param array|null $associatedIdentifiers Optional array of associated tags or keys
15+ * @param string $type 'key' or 'tag'
16+ * @param string $identifier The cache key or tag to invalidate
17+ * @param string|null $connection_name The Redis Connection name (optional, 'default')
18+ * @param string|null $reason Reason for invalidation (optional)
19+ * @param int|null $totalShards Total number of shards (from config if null)
20+ * @param int|null $priority Priority of the event
21+ * @param int|null $processed Processed = 0 o 1
22+ * @param Carbon|null $event_time Orario vero in cui si è richiesta l'invalidazione
2223 */
2324 public function insertInvalidationEvent (
2425 string $ type ,
@@ -27,19 +28,20 @@ public function insertInvalidationEvent(
2728 ?string $ reason = null ,
2829 ?int $ totalShards = 0 ,
2930 ?int $ priority = 0 ,
30- ?array $ associatedIdentifiers = [],
31+ ?int $ processed = 0 ,
32+ ?Carbon $ event_time = null ,
33+ /*?array $associatedIdentifiers = [],*/
3134 ): void {
3235 $ shard = crc32 ($ identifier ) % ($ totalShards > 0 ? $ totalShards : config ('super_cache_invalidate.total_shards ' , 10 ));
33-
3436 $ redisConnectionName = $ connection_name ?? config ('super_cache_invalidate.default_connection_name ' );
3537 $ data = [
3638 'type ' => $ type ,
3739 'identifier ' => $ identifier ,
3840 'connection_name ' => $ redisConnectionName ,
3941 'reason ' => $ reason ,
4042 'priority ' => $ priority ,
41- 'event_time ' => now (),
42- 'processed ' => 0 ,
43+ 'event_time ' => $ event_time ?? now (),
44+ 'processed ' => $ processed , // ATTENZIONE, poichè abbiamo solo 2 priorità, nel caso di priorità 1 verrà passato 1 perchè l'invalidazione la fa il progetto
4345 'shard ' => $ shard ,
4446 ];
4547
@@ -52,8 +54,17 @@ public function insertInvalidationEvent(
5254
5355 try {
5456 // Cerca di bloccare il record per l'inserimento
55- $ partitionCache_invalidation_events = $ this ->getCacheInvalidationEventsPartitionName ($ shard , $ priority );
56-
57+ switch ($ processed ) {
58+ case 0 :
59+ $ partitionCache_invalidation_events = $ this ->getCacheInvalidationEventsUnprocessedPartitionName ($ shard , $ priority );
60+ break ;
61+ case 1 :
62+ $ partitionCache_invalidation_events = $ this ->getCacheInvalidationEventsProcessedPartitionName ($ shard , $ priority , $ event_time ?? now ());
63+ break ;
64+ default :
65+ $ attempts = 5 ; // Mi fermo
66+ throw new \RuntimeException ('Invalid value for processed ' );
67+ }
5768 //$eventId = DB::table(DB::raw("`cache_invalidation_events` PARTITION ({$partitionCache_invalidation_events})"))->insertGetId($data);
5869 DB ::table (DB ::raw ("`cache_invalidation_events` PARTITION ( {$ partitionCache_invalidation_events }) " ))->insert ($ data );
5970 // Insert associated identifiers
@@ -132,71 +143,15 @@ public function releaseShardLock(int $shardId, int $priority, string $lockValue,
132143 }
133144 }
134145
135- public function getCacheInvalidationEventsPartitionName (int $ shardId , int $ priorityId ): string
136- {
137- // Calcola il valore della partizione
138- $ shards = config ('super_cache_invalidate.total_shards ' , 10 );
139- $ priorities = [0 , 1 ];
140-
141- $ partitionValueId = ($ priorityId * $ shards ) + $ shardId + 1 ;
142-
143- // Partitions for unprocessed events
144- foreach ($ priorities as $ priority ) {
145- for ($ shard = 0 ; $ shard < $ shards ; $ shard ++) {
146- $ partitionName = "p_unprocessed_s {$ shard }_p {$ priority }" ;
147- $ partitionValue = ($ priority * $ shards ) + $ shard + 1 ;
148- if ($ partitionValueId < $ partitionValue ) {
149- return $ partitionName ;
150- }
151- }
152- }
153-
154- return '' ;
155- }
156-
157- public function getCacheInvalidationTimestampsPartitionName (Carbon $ event_time ): string
146+ public function getCacheInvalidationEventsUnprocessedPartitionName (int $ shardId , int $ priorityId ): string
158147 {
159- $ now = now ();
160- $ partitionValueId = ($ now ->year * 100 + $ now ->weekOfYear ) + 1 ;
161- $ startYear = 2024 ;
162- $ endYear = 2030 ;
163-
164- for ($ year = $ startYear ; $ year <= $ endYear ; $ year ++) {
165- for ($ week = 1 ; $ week <= 53 ; $ week ++) {
166- $ partitionName = "p_ {$ year }w {$ week }" ;
167- $ partitionValue = ($ year * 100 + $ week ) + 1 ;
168- if ($ partitionValueId < $ partitionValue ) {
169- return $ partitionName ;
170- }
171- }
172- }
173-
174- return '' ;
148+ $ partitionValue = ($ priorityId * 10 ) + $ shardId ;
149+ return "p_unprocessed_ {$ partitionValue }" ;
175150 }
176151
177- public function getCacheInvalidationEventsProcessedPartitionName (int $ shardId , int $ priorityId , string $ event_time ): string
152+ public function getCacheInvalidationEventsProcessedPartitionName (int $ shardId , int $ priorityId , Carbon $ event_time ): string
178153 {
179- // Calcola il valore della partizione
180- $ shards = config ('super_cache_invalidate.total_shards ' , 10 );
181- $ priorities = [0 , 1 ];
182- $ eventTime = Carbon::parse ($ event_time );
183- $ partitionValueId = ($ eventTime ->year * 10000 ) + ($ eventTime ->weekOfYear * 100 ) + ($ priorityId * $ shards ) + $ shardId ;
184- // Partitions for processed events
185- for ($ year = $ eventTime ->year ; $ year <= ($ eventTime ->year + 1 ); $ year ++) {
186- for ($ week = $ eventTime ->weekOfYear ; $ week <= $ eventTime ->weekOfYear + 1 ; $ week ++) {
187- foreach ($ priorities as $ priority ) {
188- for ($ shard = 0 ; $ shard < $ shards ; $ shard ++) {
189- $ partitionKey = ($ year * 10000 ) + ($ week * 100 ) + ($ priority * $ shards ) + $ shard ;
190- $ partitionValue = $ partitionKey + 1 ;
191- $ partitionName = "p_s {$ shard }_p {$ priority }_ {$ year }w {$ week }" ;
192- if ($ partitionValueId < $ partitionValue ) {
193- return $ partitionName ;
194- }
195- }
196- }
197- }
198- }
199-
200- return '' ;
154+ $ partitionValue = ($ event_time ->year * 10000 ) + ($ event_time ->weekOfYear * 100 ) + ($ priorityId * 10 ) + $ shardId ;
155+ return "p_processed_ {$ partitionValue }" ;
201156 }
202157}
0 commit comments