Skip to content

Commit

Permalink
Merge pull request #55 from xificurk/cache-decorator
Browse files Browse the repository at this point in the history
CacheDecorator: optimalizace ověřování platného LoginID
  • Loading branch information
JindrichPilar committed Apr 7, 2015
2 parents 6d0b73f + 6733685 commit a0c6bce
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
13 changes: 9 additions & 4 deletions src/Wsdl/Decorator/Cache/CacheDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Skautis\Wsdl\Decorator\Cache;

use Skautis\User;
use Skautis\Wsdl\Decorator\AbstractDecorator;
use Skautis\Wsdl\WebServiceInterface;

Expand All @@ -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;
Expand All @@ -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;
}
Expand Down
12 changes: 5 additions & 7 deletions tests/Unit/CacheDecoratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Test\Skautis;

use Skautis\User;
use Skautis\Wsdl\Decorator\Cache\ArrayCache;
use Skautis\Wsdl\Decorator\Cache\CacheDecorator;

Expand All @@ -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');
Expand All @@ -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()
Expand Down

0 comments on commit a0c6bce

Please sign in to comment.