Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
240 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
/** | ||
* This file is part of the prooph/service-bus. | ||
* (c) 2014-2018 prooph software GmbH <contact@prooph.de> | ||
* (c) 2015-2018 Sascha-Oliver Prolic <saschaprolic@googlemail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Prooph\ServiceBus\Plugin; | ||
|
||
use Prooph\Common\Messaging\Query; | ||
|
||
interface CacheKeyGenerator | ||
{ | ||
public function getCacheKey(Query $query): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
/** | ||
* This file is part of the prooph/service-bus. | ||
* (c) 2014-2018 prooph software GmbH <contact@prooph.de> | ||
* (c) 2015-2018 Sascha-Oliver Prolic <saschaprolic@googlemail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Prooph\ServiceBus\Plugin\CacheKeyGenerator; | ||
|
||
use Prooph\Common\Messaging\Query; | ||
use Prooph\ServiceBus\Plugin\CacheKeyGenerator; | ||
|
||
final class Standard implements CacheKeyGenerator | ||
{ | ||
public function getCacheKey(Query $query): string | ||
{ | ||
$keyParts = [$query->messageName()]; | ||
foreach ($query->payload() as $key => $value) { | ||
$keyParts[] = $key; | ||
$keyParts[] = $value; | ||
} | ||
|
||
return sha1(json_encode($keyParts)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
/** | ||
* This file is part of the prooph/service-bus. | ||
* (c) 2014-2018 prooph software GmbH <contact@prooph.de> | ||
* (c) 2015-2018 Sascha-Oliver Prolic <saschaprolic@googlemail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Prooph\ServiceBus\Plugin; | ||
|
||
use Prooph\Common\Event\ActionEvent; | ||
use Prooph\ServiceBus\MessageBus; | ||
use Prooph\ServiceBus\QueryBus; | ||
use Psr\SimpleCache\CacheInterface; | ||
|
||
final class CachePlugin extends AbstractPlugin | ||
{ | ||
private $cache; | ||
private $keyGenerator; | ||
|
||
public function __construct( | ||
CacheInterface $cache, | ||
CacheKeyGenerator $keyGenerator = null | ||
) { | ||
$this->cache = $cache; | ||
$this->keyGenerator = $keyGenerator ?: new CacheKeyGenerator\Standard(); | ||
} | ||
|
||
public function attachToMessageBus(MessageBus $messageBus): void | ||
{ | ||
if (! $messageBus instanceof QueryBus) { | ||
throw new \InvalidArgumentException(sprintf( | ||
'The cache plugin can only be attached to an instance of "Prooph\ServiceBus\QueryBus", got "%s".', | ||
get_class($messageBus) | ||
)); | ||
} | ||
|
||
$this->listenerHandlers[] = $messageBus->attach( | ||
QueryBus::EVENT_DISPATCH, | ||
function (ActionEvent $actionEvent): void { | ||
if ($actionEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE_HANDLED, false)) { | ||
return; | ||
} | ||
|
||
$query = $actionEvent->getParam(QueryBus::EVENT_PARAM_MESSAGE); | ||
|
||
$deferred = $actionEvent->getParam(QueryBus::EVENT_PARAM_DEFERRED); | ||
|
||
$key = $this->keyGenerator->getCacheKey($query); | ||
if (null !== $result = $this->cache->get($key)) { | ||
$deferred->resolve($result); | ||
$actionEvent->setParam(MessageBus::EVENT_PARAM_MESSAGE_HANDLER, null); | ||
|
||
return; | ||
} | ||
|
||
$deferred->promise()->then(function ($data) use ($key) { | ||
$this->cache->set($key, $data); | ||
}); | ||
}, | ||
QueryBus::PRIORITY_INVOKE_HANDLER + 1 | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<?php | ||
/** | ||
* This file is part of the prooph/service-bus. | ||
* (c) 2014-2018 prooph software GmbH <contact@prooph.de> | ||
* (c) 2015-2018 Sascha-Oliver Prolic <saschaprolic@googlemail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ProophTest\ServiceBus\Plugin; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Prooph\Common\Messaging\Query; | ||
use Prooph\ServiceBus\CommandBus; | ||
use Prooph\ServiceBus\EventBus; | ||
use Prooph\ServiceBus\Plugin\CacheKeyGenerator; | ||
use Prooph\ServiceBus\Plugin\CachePlugin; | ||
use Prooph\ServiceBus\Plugin\Plugin; | ||
use Prooph\ServiceBus\Plugin\Router\QueryRouter; | ||
use Prooph\ServiceBus\QueryBus; | ||
use Psr\SimpleCache\CacheInterface; | ||
use React\Promise\Deferred; | ||
|
||
class CachePluginTest extends TestCase | ||
{ | ||
private $cache; | ||
private $keyGen; | ||
private $plugin; | ||
|
||
public function setUp() | ||
{ | ||
$this->cache = $this->prophesize(CacheInterface::class); | ||
$this->keyGen = $this->prophesize(CacheKeyGenerator::class); | ||
$this->plugin = new CachePlugin( | ||
$this->cache->reveal(), | ||
$this->keyGen->reveal() | ||
); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_is_a_message_bus_plugin() | ||
{ | ||
$this->assertInstanceOf(Plugin::class, $this->plugin); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_resolves_with_cached_value_on_hit() | ||
{ | ||
$queryBus = new QueryBus(); | ||
|
||
$router = new QueryRouter(); | ||
$router->route('a-query') | ||
->to(function ($query, Deferred $deferred): void { | ||
}); | ||
$router->attachToMessageBus($queryBus); | ||
$query = $this->prophesize(Query::class); | ||
$this->plugin->attachToMessageBus($queryBus); | ||
|
||
$query->messageName()->willReturn('a-query'); | ||
$this->keyGen->getCacheKey($query)->willReturn('cache-key'); | ||
$this->cache->get('cache-key')->willReturn('Hello World'); | ||
|
||
$promise = $queryBus->dispatch($query->reveal()); | ||
$value = null; | ||
$promise->then(function ($result) use (&$value) { | ||
$value = $result; | ||
}); | ||
$this->assertEquals('Hello World', $value); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_caches_resolved_value() | ||
{ | ||
$queryBus = new QueryBus(); | ||
|
||
$router = new QueryRouter(); | ||
$router->route('a-query') | ||
->to(function ($query, Deferred $deferred): void { | ||
$deferred->resolve('Hello World'); | ||
}); | ||
$router->attachToMessageBus($queryBus); | ||
$query = $this->prophesize(Query::class); | ||
$this->plugin->attachToMessageBus($queryBus); | ||
|
||
$query->messageName()->willReturn('a-query'); | ||
$this->keyGen->getCacheKey($query)->willReturn('cache-key'); | ||
$this->cache->get('cache-key')->willReturn(null); | ||
$this->cache->set('cache-key', 'Hello World')->shouldBeCalled(); | ||
|
||
$queryBus->dispatch($query->reveal()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_does_not_attach_to_command_bus() | ||
{ | ||
$this->expectException(\InvalidArgumentException::class); | ||
$this->plugin->attachToMessageBus(new CommandBus()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_does_not_attach_to_event_bus() | ||
{ | ||
$this->expectException(\InvalidArgumentException::class); | ||
$this->plugin->attachToMessageBus(new EventBus()); | ||
} | ||
} |