Add cleanup subscribers counts cache

[MAILPOET-3714]
This commit is contained in:
Jan Lysý
2021-07-23 15:19:41 +02:00
committed by Veljko V
parent 2deaeb284b
commit 19fad073d0
4 changed files with 23 additions and 1 deletions

View File

@ -259,6 +259,8 @@ class Settings extends APIEndpoint {
} }
} }
$this->subscribersCountsController->recalculateSubscribersWithoutSegmentStatisticsCache(); $this->subscribersCountsController->recalculateSubscribersWithoutSegmentStatisticsCache();
// remove redundancies from cache
$this->subscribersCountsController->removeRedundancyFromStatisticsCache();
return $this->successResponse(); return $this->successResponse();
} }
} }

View File

@ -68,7 +68,7 @@ class TransientCache {
$this->wp->setTransient($key, $items); $this->wp->setTransient($key, $items);
} }
private function getItems(string $key): array { public function getItems(string $key): array {
return $this->wp->getTransient($key) ?: []; return $this->wp->getTransient($key) ?: [];
} }
} }

View File

@ -46,6 +46,9 @@ class SubscribersCountCacheRecalculation extends SimpleWorker {
// update cache for subscribers without segment // update cache for subscribers without segment
$this->recalculateSegmentCache($timer, 0); $this->recalculateSegmentCache($timer, 0);
// remove redundancies from cache
$this->subscribersCountsController->removeRedundancyFromStatisticsCache();
return true; return true;
} }

View File

@ -90,4 +90,21 @@ class SubscribersCountsController {
$this->transientCache->setItem(TransientCache::SUBSCRIBERS_STATISTICS_COUNT_KEY, $result, 0); $this->transientCache->setItem(TransientCache::SUBSCRIBERS_STATISTICS_COUNT_KEY, $result, 0);
return $result; 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);
}
}
}
} }