diff --git a/Api/QueueInterface.php b/Api/QueueInterface.php new file mode 100644 index 0000000..bfb85c0 --- /dev/null +++ b/Api/QueueInterface.php @@ -0,0 +1,23 @@ +cacheDebouncedEntries = $cacheDebouncedEntries; + $this->queue = $queue; parent::__construct($name); } @@ -37,7 +37,7 @@ protected function configure(): void protected function execute(InputInterface $input, OutputInterface $output): int { try { - $this->cacheDebouncedEntries->flush(); + $this->queue->flush(); $output->writeln('Flushed Debounced Cache Purges.'); return 0; } catch (LocalizedException $e) { diff --git a/Cron/Flush.php b/Cron/Flush.php index 4c0d163..9836174 100644 --- a/Cron/Flush.php +++ b/Cron/Flush.php @@ -4,21 +4,21 @@ namespace SamJUK\CacheDebounce\Cron; -use SamJUK\CacheDebounce\Model\Entries as CacheDebouncedEntries; +use SamJUK\CacheDebounce\Api\QueueInterface; class Flush { - /** @var CacheDebouncedEntries $cacheDebouncedEntries */ - private $cacheDebouncedEntries; + /** @var QueueInterface $queue */ + private $queue; public function __construct( - CacheDebouncedEntries $cacheDebouncedEntries + QueueInterface $queue ) { - $this->cacheDebouncedEntries = $cacheDebouncedEntries; + $this->queue = $queue; } public function execute() : void { - $this->cacheDebouncedEntries->flush(); + $this->queue->flush(); } } diff --git a/Model/Entries.php b/Model/Queue/Database.php similarity index 90% rename from Model/Entries.php rename to Model/Queue/Database.php index 49ad023..9ed1ab7 100644 --- a/Model/Entries.php +++ b/Model/Queue/Database.php @@ -2,14 +2,15 @@ declare(strict_types=1); -namespace SamJUK\CacheDebounce\Model; +namespace SamJUK\CacheDebounce\Model\Queue; +use SamJUK\CacheDebounce\Api\QueueInterface; use SamJUK\CacheDebounce\Model\Config; use Magento\CacheInvalidate\Model\PurgeCache; use Magento\Framework\App\ResourceConnection; use Psr\Log\LoggerInterface; -class Entries +class Database implements QueueInterface { /** @var string $tableName */ private $tableName; @@ -40,7 +41,7 @@ public function __construct( } /** - * Add new tags to the purge queue + * {@inheritDoc} */ public function add(array $tags) : void { @@ -49,7 +50,7 @@ public function add(array $tags) : void } /** - * Get all tags from the purge queue + * {@inheritDoc} */ public function get() : array { @@ -59,7 +60,7 @@ public function get() : array } /** - * Purge all queued tags, and clear the queue + * {@inheritDoc} */ public function flush() : void { diff --git a/Plugin/PurgeCache.php b/Plugin/PurgeCache.php index 20c3e27..b863acd 100644 --- a/Plugin/PurgeCache.php +++ b/Plugin/PurgeCache.php @@ -5,7 +5,7 @@ namespace SamJUK\CacheDebounce\Plugin; use SamJUK\CacheDebounce\Model\Config; -use SamJUK\CacheDebounce\Model\Entries as CacheDebounceEntries; +use SamJUK\CacheDebounce\Api\QueueInterface; use Magento\CacheInvalidate\Model\PurgeCache as Subject; class PurgeCache @@ -13,15 +13,15 @@ class PurgeCache /** @var Config $config **/ private $config; - /** @var CacheDebounceEntries $cacheDebouncedEntries */ - private $cacheDebouncedEntries; + /** @var QueueInterface $queue */ + private $queue; public function __construct( Config $config, - CacheDebounceEntries $cacheDebounceEntries + QueueInterface $queue ) { $this->config = $config; - $this->cacheDebouncedEntries = $cacheDebounceEntries; + $this->queue = $queue; } /** @@ -42,7 +42,7 @@ public function aroundSendPurgeRequest(Subject $subject, callable $proceed, $tag $tags = [$tags]; } - $this->cacheDebouncedEntries->add($tags); + $this->queue->add($tags); return true; } } diff --git a/README.md b/README.md index 464df3d..5501d7a 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,9 @@ Option | Config Path | Default | Description Enabled | `samjuk_cache_debounce/general/enabled` | `0` | Feature flag to toggle functionality of the module Flush Schedule | `samjuk_cache_debounce/cron/flush_schedule` | `*/5 0 0 0 0` | Cron schedule to run the scheduled flush +## Extension +Additional datastores can be implemented by updating the `QueueInterface` preference in `di.xml`. + ## Will this help my store? The performance improvement comes from improving cache performance by reducing the amount of cache purge requests in turn reducing system load. diff --git a/Test/Integration/Model/EntriesTest.php b/Test/Integration/Model/EntriesTest.php deleted file mode 100644 index f6b54d2..0000000 --- a/Test/Integration/Model/EntriesTest.php +++ /dev/null @@ -1,29 +0,0 @@ -objectManager = ObjectManager::getInstance(); - $this->cacheDebouncedEntries = $this->objectManager->get(\SamJUK\CacheDebounce\Model\Entries::class); - } - - public function testDebouncedEntriesStorage() - { - $this->cacheDebouncedEntries->add(self::CACHE_TAGS); - $this->assertEquals(self::CACHE_TAGS, $this->cacheDebouncedEntries->get()); - - $this->cacheDebouncedEntries->flush(); - $this->assertEquals([], $this->cacheDebouncedEntries->get()); - } -} diff --git a/Test/Integration/Model/Queue/DatabaseTest.php b/Test/Integration/Model/Queue/DatabaseTest.php new file mode 100644 index 0000000..9f28241 --- /dev/null +++ b/Test/Integration/Model/Queue/DatabaseTest.php @@ -0,0 +1,31 @@ +objectManager = ObjectManager::getInstance(); + $this->queue = $this->objectManager->get(\SamJUK\CacheDebounce\Api\QueueInterface::class); + } + + public function testDatabaseStorage() + { + $this->queue->add(self::CACHE_TAGS); + $this->assertEquals(self::CACHE_TAGS, $this->queue->get()); + + $this->queue->flush(); + $this->assertEquals([], $this->queue->get()); + } +} diff --git a/Test/Unit/Model/ConfigTest.php b/Test/Unit/Model/ConfigTest.php index 2266d1b..df94788 100644 --- a/Test/Unit/Model/ConfigTest.php +++ b/Test/Unit/Model/ConfigTest.php @@ -1,4 +1,6 @@ -resourceConnection->method('getConnection')->willReturn($this->connection); $this->purgeCacheModel = $this->createMock(\Magento\CacheInvalidate\Model\PurgeCache::class); $this->loggerInterface = $this->createMock(\Psr\Log\LoggerInterface::class); - $this->cacheDebounceEntries = new CacheDebounceEntries( + $this->databaseQueue = new DatabaseQueue( $this->cacheDebounceConfig, $this->purgeCacheModel, $this->resourceConnection, @@ -47,6 +49,6 @@ public function testFlushTags() ->method('sendPurgeRequest') ->with([self::CACHE_TAGS]); - $this->cacheDebounceEntries->flush(); + $this->databaseQueue->flush(); } } diff --git a/Test/Unit/Plugin/PurgeCacheTest.php b/Test/Unit/Plugin/PurgeCacheTest.php index 2c2295f..8493f1a 100644 --- a/Test/Unit/Plugin/PurgeCacheTest.php +++ b/Test/Unit/Plugin/PurgeCacheTest.php @@ -1,10 +1,12 @@ -createMock(PurgeCacheModel::class); $cacheDebounceConfig = $this->createMock(CacheDebounceConfig::class); - $cacheDebounceEntries = $this->createMock(CacheDebounceEntries::class); - $purgeCachePlugin = new PurgeCachePlugin($cacheDebounceConfig, $cacheDebounceEntries); + $queue = $this->createMock(QueueInterface::class); + $purgeCachePlugin = new PurgeCachePlugin($cacheDebounceConfig, $queue); $cacheDebounceConfig->method('shouldDebouncePurgeRequest') ->willReturn(true); @@ -34,8 +36,8 @@ public function testPluginSkipsDebouncePurgeRequest() { $purgeCacheModel = $this->createMock(PurgeCacheModel::class); $cacheDebounceConfig = $this->createMock(CacheDebounceConfig::class); - $cacheDebounceEntries = $this->createMock(CacheDebounceEntries::class); - $purgeCachePlugin = new PurgeCachePlugin($cacheDebounceConfig, $cacheDebounceEntries); + $queue = $this->createMock(QueueInterface::class); + $purgeCachePlugin = new PurgeCachePlugin($cacheDebounceConfig, $queue); $cacheDebounceConfig->method('shouldDebouncePurgeRequest') ->willReturn(false); diff --git a/etc/di.xml b/etc/di.xml index 36fd265..d649b48 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -1,5 +1,7 @@ + + @@ -11,5 +13,4 @@ -