Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions Api/QueueInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace SamJUK\CacheDebounce\Api;

interface QueueInterface
{
/**
* Add new tags to the purge queue
*/
public function add(array $tags) : void;

/**
* Get all tags from the purge queue
*/
public function get() : array;

/**
* Purge all queued tags, and clear the queue
*/
public function flush() : void;
}
12 changes: 6 additions & 6 deletions Console/Command/Flush.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use SamJUK\CacheDebounce\Model\Entries as CacheDebouncedEntries;
use SamJUK\CacheDebounce\Api\QueueInterface;

class Flush extends Command
{
private $cacheDebouncedEntries;
private $queue;

/**
* @param CacheDebouncedEntries $cacheDebouncedEntries
* @param QueueInterface $queue
* @param string|null $name
*/
public function __construct(
CacheDebouncedEntries $cacheDebouncedEntries,
QueueInterface $queue,
$name = null
) {
$this->cacheDebouncedEntries = $cacheDebouncedEntries;
$this->queue = $queue;
parent::__construct($name);
}

Expand All @@ -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('<info>Flushed Debounced Cache Purges.</info>');
return 0;
} catch (LocalizedException $e) {
Expand Down
12 changes: 6 additions & 6 deletions Cron/Flush.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
11 changes: 6 additions & 5 deletions Model/Entries.php → Model/Queue/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -40,7 +41,7 @@ public function __construct(
}

/**
* Add new tags to the purge queue
* {@inheritDoc}
*/
public function add(array $tags) : void
{
Expand All @@ -49,7 +50,7 @@ public function add(array $tags) : void
}

/**
* Get all tags from the purge queue
* {@inheritDoc}
*/
public function get() : array
{
Expand All @@ -59,7 +60,7 @@ public function get() : array
}

/**
* Purge all queued tags, and clear the queue
* {@inheritDoc}
*/
public function flush() : void
{
Expand Down
12 changes: 6 additions & 6 deletions Plugin/PurgeCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@
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
{
/** @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;
}

/**
Expand All @@ -42,7 +42,7 @@ public function aroundSendPurgeRequest(Subject $subject, callable $proceed, $tag
$tags = [$tags];
}

$this->cacheDebouncedEntries->add($tags);
$this->queue->add($tags);
return true;
}
}
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
29 changes: 0 additions & 29 deletions Test/Integration/Model/EntriesTest.php

This file was deleted.

31 changes: 31 additions & 0 deletions Test/Integration/Model/Queue/DatabaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace SamJUK\CacheDebounce\Test\Integration\Model\Queue;

use PHPUnit\Framework\TestCase;
use Magento\TestFramework\ObjectManager;

class DatabaseTest extends TestCase
{
private const CACHE_TAGS = ['cat_c_1', 'cat_c_2', 'cat_c_p_1'];

protected $objectManager;
private $queue;

protected function setUp(): void
{
$this->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());
}
}
4 changes: 3 additions & 1 deletion Test/Unit/Model/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace SamJUK\CacheDebounce\Test\Unit\Model;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php declare(strict_types=1);
<?php

namespace SamJUK\CacheDebounce\Test\Unit\Model;
declare(strict_types=1);

namespace SamJUK\CacheDebounce\Test\Unit\Model\Queue;

use PHPUnit\Framework\TestCase;
use SamJUK\CacheDebounce\Model\Entries as CacheDebounceEntries;
use SamJUK\CacheDebounce\Model\Queue\Database as DatabaseQueue;

class EntriesTest extends TestCase
class DatabaseTest extends TestCase
{
private const CACHE_TAGS = ['cat_c_1', 'cat_c_2', 'cat_c_p_1'];

Expand All @@ -14,7 +16,7 @@ class EntriesTest extends TestCase
private $connection;
private $resourceConnection;
private $loggerInterface;
private $cacheDebounceEntries;
private $databaseQueue;
private $select;

protected function setUp(): void
Expand All @@ -29,7 +31,7 @@ protected function setUp(): void
$this->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,
Expand All @@ -47,6 +49,6 @@ public function testFlushTags()
->method('sendPurgeRequest')
->with([self::CACHE_TAGS]);

$this->cacheDebounceEntries->flush();
$this->databaseQueue->flush();
}
}
14 changes: 8 additions & 6 deletions Test/Unit/Plugin/PurgeCacheTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace SamJUK\CacheDebounce\Test\Unit\Plugin;

use PHPUnit\Framework\TestCase;
use SamJUK\CacheDebounce\Model\Config as CacheDebounceConfig;
use SamJUK\CacheDebounce\Model\Entries as CacheDebounceEntries;
use SamJUK\CacheDebounce\Api\QueueInterface;
use SamJUK\CacheDebounce\Plugin\PurgeCache as PurgeCachePlugin;
use Magento\CacheInvalidate\Model\PurgeCache as PurgeCacheModel;

Expand All @@ -14,8 +16,8 @@ public function testPluginDebouncesPurgeRequest()
{
$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(true);
Expand All @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion etc/di.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="SamJUK\CacheDebounce\Api\QueueInterface" type="SamJUK\CacheDebounce\Model\Queue\Database" />

<type name="Magento\CacheInvalidate\Model\PurgeCache">
<plugin name="samjuk_cachedebounce_purge_cache" type="SamJUK\CacheDebounce\Plugin\PurgeCache" />
</type>
Expand All @@ -11,5 +13,4 @@
</argument>
</arguments>
</type>

</config>