Skip to content

Commit a0c6bce

Browse files
committed
Merge pull request #55 from xificurk/cache-decorator
CacheDecorator: optimalizace ověřování platného LoginID
2 parents 6d0b73f + 6733685 commit a0c6bce

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

src/Wsdl/Decorator/Cache/CacheDecorator.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Skautis\Wsdl\Decorator\Cache;
44

5+
use Skautis\User;
56
use Skautis\Wsdl\Decorator\AbstractDecorator;
67
use Skautis\Wsdl\WebServiceInterface;
78

@@ -13,10 +14,14 @@ class CacheDecorator extends AbstractDecorator
1314
protected $cache;
1415

1516
/**
16-
* @var bool
17+
* @var array
1718
*/
18-
protected $succesfullRequest;
19+
protected static $checkedLoginIds = array();
1920

21+
/**
22+
* @param WebServiceInterface $webService
23+
* @param CacheInterface $cache
24+
*/
2025
public function __construct(WebServiceInterface $webService, CacheInterface $cache)
2126
{
2227
$this->webService = $webService;
@@ -31,10 +36,10 @@ public function call($functionName, array $arguments = [])
3136
$callHash = $this->hashCall($functionName, $arguments);
3237

3338
// Pozaduj alespon 1 supesny request na server (zadna Exception)
34-
if (!$this->succesfullRequest) {
39+
if (isset($arguments[User::ID_LOGIN]) && !in_array($arguments[User::ID_LOGIN], static::$checkedLoginIds)) {
3540
$response = $this->webService->call($functionName, $arguments);
3641
$this->cache->set($callHash, $response);
37-
$this->succesfullRequest = true;
42+
static::$checkedLoginIds[] = $arguments[User::ID_LOGIN];
3843

3944
return $response;
4045
}

tests/Unit/CacheDecoratorTest.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Test\Skautis;
44

5+
use Skautis\User;
56
use Skautis\Wsdl\Decorator\Cache\ArrayCache;
67
use Skautis\Wsdl\Decorator\Cache\CacheDecorator;
78

@@ -16,7 +17,7 @@ protected function tearDown()
1617
public function testDecoratorRequireRequest()
1718
{
1819
$value = ['id' => 'response'];
19-
$args = ['asd', 'uv'];
20+
$args = ['asd', 'uv', User::ID_LOGIN => 'a'];
2021

2122
/** @var Skautis\Wsdl\WebServiceInterface */
2223
$webService = \Mockery::mock('Skautis\Wsdl\WebServiceInterface');
@@ -30,19 +31,16 @@ public function testDecoratorRequireRequest()
3031
$this->assertEquals($value, $response);
3132

3233

33-
//Stejny request jina odpoved
34-
$valueB = ['id' => 'Different response'];
35-
34+
//Jina instance WS
3635
/** @var Skautis\Wsdl\WebServiceInterface */
3736
$webServiceB = \Mockery::mock('Skautis\Wsdl\WebServiceInterface');
38-
$webServiceB->shouldReceive('call')->with('funkceA', $args)->once()->andReturn($valueB);
3937

4038
//Cache naplnena z prechoziho requestu
4139
$decoratedServiceB = new CacheDecorator($webServiceB, $cache);
4240
$response = $decoratedServiceB->call('funkceA', $args);
4341

44-
//Jelikoz tato instance provadi prvni request, nevrati data z Cache
45-
$this->assertEquals($valueB, $response);
42+
//Vraci data z cache
43+
$this->assertEquals($value, $response);
4644
}
4745

4846
public function testDecorator()

0 commit comments

Comments
 (0)