transientCache = $transientCache; $this->segmentsRepository = $segmentsRepository; $this->subscribersCountsController = $subscribersCountsController; } public function processTaskStrategy(ScheduledTaskEntity $task, $timer) { $segments = $this->segmentsRepository->findAll(); foreach ($segments as $segment) { $this->recalculateSegmentCache($timer, (int)$segment->getId(), $segment); } // update cache for subscribers without segment $this->recalculateSegmentCache($timer, 0); $this->recalculateHomepageCache($timer); // remove redundancies from cache $this->cronHelper->enforceExecutionLimit($timer); $this->subscribersCountsController->removeRedundancyFromStatisticsCache(); return true; } private function recalculateSegmentCache($timer, int $segmentId, ?SegmentEntity $segment = null): void { $this->cronHelper->enforceExecutionLimit($timer); $now = Carbon::now(); $item = $this->transientCache->getItem(TransientCache::SUBSCRIBERS_STATISTICS_COUNT_KEY, $segmentId); if ($item === null || !isset($item['created_at']) || $now->diffInMinutes($item['created_at']) > self::EXPIRATION_IN_MINUTES) { if ($segment) { $this->subscribersCountsController->recalculateSegmentStatisticsCache($segment); } else { $this->subscribersCountsController->recalculateSubscribersWithoutSegmentStatisticsCache(); } } } private function recalculateHomepageCache($timer): void { $this->cronHelper->enforceExecutionLimit($timer); $now = Carbon::now(); $item = $this->transientCache->getItem(TransientCache::SUBSCRIBERS_HOMEPAGE_STATISTICS_COUNT_KEY, 0); if ($item === null || !isset($item['created_at']) || $now->diffInMinutes($item['created_at']) > self::EXPIRATION_IN_MINUTES) { $this->cronHelper->enforceExecutionLimit($timer); $this->subscribersCountsController->recalculateHomepageStatisticsCache(); } } public function getNextRunDate() { return Carbon::createFromTimestamp($this->wp->currentTime('timestamp')); } public function shouldBeScheduled(): bool { $scheduledOrRunningTask = $this->scheduledTasksRepository->findScheduledOrRunningTask(self::TASK_TYPE); if ($scheduledOrRunningTask) { return false; } $now = Carbon::now(); $oldestCreatedAt = $this->transientCache->getOldestCreatedAt(TransientCache::SUBSCRIBERS_STATISTICS_COUNT_KEY); return $oldestCreatedAt === null || $now->diffInMinutes($oldestCreatedAt) > self::EXPIRATION_IN_MINUTES; } }