TransientCache.php
101 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Cache; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\WP\Functions as WPFunctions; |
| 9 | use MailPoetVendor\Carbon\Carbon; |
| 10 | |
| 11 | class TransientCache { |
| 12 | public const SUBSCRIBERS_STATISTICS_COUNT_KEY = 'mailpoet_subscribers_statistics_count_cache'; |
| 13 | public const SUBSCRIBERS_HOMEPAGE_STATISTICS_COUNT_KEY = 'mailpoet_subscribers_statistics_count_homepage_cache'; |
| 14 | public const SUBSCRIBERS_GLOBAL_STATISTICS_COUNT_KEY = 'mailpoet_subscribers_statistics_count_global_cache'; |
| 15 | |
| 16 | private $cacheEnabled; |
| 17 | |
| 18 | /** @var WPFunctions */ |
| 19 | private $wp; |
| 20 | |
| 21 | public function __construct( |
| 22 | WPFunctions $wp |
| 23 | ) { |
| 24 | $this->wp = $wp; |
| 25 | $this->cacheEnabled = $this->wp->applyFilters('mailpoet_transient_cache_enabled', true); |
| 26 | } |
| 27 | |
| 28 | public function getItem(string $key, int $id): ?array { |
| 29 | $items = $this->getItems($key); |
| 30 | return $items[$id] ?? null; |
| 31 | } |
| 32 | |
| 33 | public function getOldestCreatedAt(string $key): ?\DateTime { |
| 34 | $oldest = $this->getOldestItem($key); |
| 35 | return $oldest['created_at'] ?? null; |
| 36 | } |
| 37 | |
| 38 | public function getOldestItem(string $key): ?array { |
| 39 | $items = $this->getItems($key); |
| 40 | $oldest = null; |
| 41 | foreach ($items as $item) { |
| 42 | if ($oldest === null || $item['created_at'] < $oldest['created_at']) { |
| 43 | $oldest = $item; |
| 44 | } |
| 45 | } |
| 46 | return $oldest; |
| 47 | } |
| 48 | |
| 49 | public function setItem(string $key, array $item, int $id): void { |
| 50 | $items = $this->getItems($key); |
| 51 | $items[$id] = [ |
| 52 | 'item' => $item, |
| 53 | 'created_at' => Carbon::now(), |
| 54 | ]; |
| 55 | $this->setItems($key, $items); |
| 56 | } |
| 57 | |
| 58 | public function invalidateItem(string $key, int $id): void { |
| 59 | $items = $this->getItems($key); |
| 60 | unset($items[$id]); |
| 61 | if (count($items)) { |
| 62 | $this->setItems($key, $items); |
| 63 | } else { |
| 64 | $this->deleteItems($key); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | public function invalidateItems(string $key): void { |
| 69 | $this->deleteItems($key); |
| 70 | } |
| 71 | |
| 72 | public function invalidateAllItems(): void { |
| 73 | $this->invalidateItems(self::SUBSCRIBERS_STATISTICS_COUNT_KEY); |
| 74 | $this->invalidateItems(self::SUBSCRIBERS_HOMEPAGE_STATISTICS_COUNT_KEY); |
| 75 | $this->invalidateItems(self::SUBSCRIBERS_GLOBAL_STATISTICS_COUNT_KEY); |
| 76 | } |
| 77 | |
| 78 | private function deleteItems(string $key): void { |
| 79 | $this->wp->deleteTransient($key); |
| 80 | } |
| 81 | |
| 82 | private function setItems(string $key, array $items): void { |
| 83 | $this->wp->setTransient($key, $items); |
| 84 | } |
| 85 | |
| 86 | public function getItems(string $key): array { |
| 87 | if (!$this->cacheEnabled) { |
| 88 | return []; |
| 89 | } |
| 90 | return $this->wp->getTransient($key) ?: []; |
| 91 | } |
| 92 | |
| 93 | public function enableCache(): void { |
| 94 | $this->cacheEnabled = true; |
| 95 | } |
| 96 | |
| 97 | public function disableCache(): void { |
| 98 | $this->cacheEnabled = false; |
| 99 | } |
| 100 | } |
| 101 |