diff --git a/lib/API/JSON/v1/Settings.php b/lib/API/JSON/v1/Settings.php index bd0999d86f..5e87a418c9 100644 --- a/lib/API/JSON/v1/Settings.php +++ b/lib/API/JSON/v1/Settings.php @@ -259,6 +259,8 @@ class Settings extends APIEndpoint { } } $this->subscribersCountsController->recalculateSubscribersWithoutSegmentStatisticsCache(); + // remove redundancies from cache + $this->subscribersCountsController->removeRedundancyFromStatisticsCache(); return $this->successResponse(); } } diff --git a/lib/Cache/TransientCache.php b/lib/Cache/TransientCache.php index 2b6d82c9b8..94d1d3bb12 100644 --- a/lib/Cache/TransientCache.php +++ b/lib/Cache/TransientCache.php @@ -68,7 +68,7 @@ class TransientCache { $this->wp->setTransient($key, $items); } - private function getItems(string $key): array { + public function getItems(string $key): array { return $this->wp->getTransient($key) ?: []; } } diff --git a/lib/Cron/Workers/SubscribersCountCacheRecalculation.php b/lib/Cron/Workers/SubscribersCountCacheRecalculation.php index e3a48832cc..95031b41fa 100644 --- a/lib/Cron/Workers/SubscribersCountCacheRecalculation.php +++ b/lib/Cron/Workers/SubscribersCountCacheRecalculation.php @@ -46,6 +46,9 @@ class SubscribersCountCacheRecalculation extends SimpleWorker { // update cache for subscribers without segment $this->recalculateSegmentCache($timer, 0); + // remove redundancies from cache + $this->subscribersCountsController->removeRedundancyFromStatisticsCache(); + return true; } diff --git a/lib/Subscribers/SubscribersCountsController.php b/lib/Subscribers/SubscribersCountsController.php index dc76bb1583..b7930c1602 100644 --- a/lib/Subscribers/SubscribersCountsController.php +++ b/lib/Subscribers/SubscribersCountsController.php @@ -90,4 +90,21 @@ class SubscribersCountsController { $this->transientCache->setItem(TransientCache::SUBSCRIBERS_STATISTICS_COUNT_KEY, $result, 0); return $result; } + + public function removeRedundancyFromStatisticsCache() { + $segments = $this->segmentsRepository->findAll(); + $segmentIds = array_map(function (SegmentEntity $segment): int { + return (int)$segment->getId(); + }, $segments); + foreach ($this->transientCache->getItems(TransientCache::SUBSCRIBERS_STATISTICS_COUNT_KEY) as $id => $item) { + if (!in_array($id, $segmentIds)) { + $this->transientCache->invalidateItem(TransientCache::SUBSCRIBERS_STATISTICS_COUNT_KEY, $id); + } + } + foreach ($this->transientCache->getItems(TransientCache::SUBSCRIBERS_GLOBAL_STATUS_STATISTICS_COUNT_KEY) as $id => $item) { + if (!in_array($id, $segmentIds)) { + $this->transientCache->invalidateItem(TransientCache::SUBSCRIBERS_GLOBAL_STATUS_STATISTICS_COUNT_KEY, $id); + } + } + } }