From 6733685ab6a694f5bc4698f61944c4bd1616407f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mor=C3=A1vek=20=5BXificurk=5D?= Date: Sun, 5 Apr 2015 00:40:27 +0200 Subject: [PATCH] =?UTF-8?q?CacheDecorator:=20optimalizace=20ov=C4=9B=C5=99?= =?UTF-8?q?ov=C3=A1n=C3=AD=20platn=C3=A9ho=20LoginID?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Wsdl/Decorator/Cache/CacheDecorator.php | 13 +++++++++---- tests/Unit/CacheDecoratorTest.php | 12 +++++------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/Wsdl/Decorator/Cache/CacheDecorator.php b/src/Wsdl/Decorator/Cache/CacheDecorator.php index 0e473f4..bd2b516 100644 --- a/src/Wsdl/Decorator/Cache/CacheDecorator.php +++ b/src/Wsdl/Decorator/Cache/CacheDecorator.php @@ -2,6 +2,7 @@ namespace Skautis\Wsdl\Decorator\Cache; +use Skautis\User; use Skautis\Wsdl\Decorator\AbstractDecorator; use Skautis\Wsdl\WebServiceInterface; @@ -13,10 +14,14 @@ class CacheDecorator extends AbstractDecorator protected $cache; /** - * @var bool + * @var array */ - protected $succesfullRequest; + protected static $checkedLoginIds = array(); + /** + * @param WebServiceInterface $webService + * @param CacheInterface $cache + */ public function __construct(WebServiceInterface $webService, CacheInterface $cache) { $this->webService = $webService; @@ -31,10 +36,10 @@ public function call($functionName, array $arguments = []) $callHash = $this->hashCall($functionName, $arguments); // Pozaduj alespon 1 supesny request na server (zadna Exception) - if (!$this->succesfullRequest) { + if (isset($arguments[User::ID_LOGIN]) && !in_array($arguments[User::ID_LOGIN], static::$checkedLoginIds)) { $response = $this->webService->call($functionName, $arguments); $this->cache->set($callHash, $response); - $this->succesfullRequest = true; + static::$checkedLoginIds[] = $arguments[User::ID_LOGIN]; return $response; } diff --git a/tests/Unit/CacheDecoratorTest.php b/tests/Unit/CacheDecoratorTest.php index 64077dd..3d5c374 100644 --- a/tests/Unit/CacheDecoratorTest.php +++ b/tests/Unit/CacheDecoratorTest.php @@ -2,6 +2,7 @@ namespace Test\Skautis; +use Skautis\User; use Skautis\Wsdl\Decorator\Cache\ArrayCache; use Skautis\Wsdl\Decorator\Cache\CacheDecorator; @@ -16,7 +17,7 @@ protected function tearDown() public function testDecoratorRequireRequest() { $value = ['id' => 'response']; - $args = ['asd', 'uv']; + $args = ['asd', 'uv', User::ID_LOGIN => 'a']; /** @var Skautis\Wsdl\WebServiceInterface */ $webService = \Mockery::mock('Skautis\Wsdl\WebServiceInterface'); @@ -30,19 +31,16 @@ public function testDecoratorRequireRequest() $this->assertEquals($value, $response); - //Stejny request jina odpoved - $valueB = ['id' => 'Different response']; - + //Jina instance WS /** @var Skautis\Wsdl\WebServiceInterface */ $webServiceB = \Mockery::mock('Skautis\Wsdl\WebServiceInterface'); - $webServiceB->shouldReceive('call')->with('funkceA', $args)->once()->andReturn($valueB); //Cache naplnena z prechoziho requestu $decoratedServiceB = new CacheDecorator($webServiceB, $cache); $response = $decoratedServiceB->call('funkceA', $args); - //Jelikoz tato instance provadi prvni request, nevrati data z Cache - $this->assertEquals($valueB, $response); + //Vraci data z cache + $this->assertEquals($value, $response); } public function testDecorator()