Skip to content

Commit 76ed46a

Browse files
authored
Merge pull request #4 from dorantor/master
Fixed broken server config merge.
2 parents dff7f0e + 34c35e9 commit 76ed46a

4 files changed

Lines changed: 139 additions & 5 deletions

File tree

src/PHPixie/Cache/Drivers/Driver.php

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,25 @@ public function __construct($builder, $configData)
2828
$this->configData = $configData;
2929
}
3030

31+
/**
32+
* Delete item
33+
*
34+
* @param $key
35+
* @return bool
36+
*/
3137
public function deleteItem($key)
3238
{
3339
$this->deleteItems(array($key));
3440
return true;
3541
}
3642

43+
/**
44+
* Save multiple items
45+
*
46+
* @param array(Item) $items
47+
* @param array $expiryTimes corresponding expiry times
48+
* @return bool
49+
*/
3750
public function saveMultiple($items, $expiryTimes)
3851
{
3952
/** @var Item $item */
@@ -44,6 +57,13 @@ public function saveMultiple($items, $expiryTimes)
4457
return true;
4558
}
4659

60+
/**
61+
* Build Items
62+
*
63+
* @param array $keys
64+
* @param array $data
65+
* @return array
66+
*/
4767
protected function buildItems($keys, $data)
4868
{
4969
$result = array();
@@ -58,11 +78,24 @@ protected function buildItems($keys, $data)
5878
return $result;
5979
}
6080

81+
/**
82+
* Build Item object
83+
*
84+
* @param string $key
85+
* @param bool $isHit
86+
* @param mixed $value stored value
87+
* @return Item
88+
*/
6189
public function buildItem($key, $isHit, $value = null)
6290
{
6391
return new Item($key, $isHit, $value);
6492
}
6593

94+
/**
95+
* Check if cleanup is required. And perform cleanup(if required)/
96+
*
97+
* @return void
98+
*/
6699
protected function cleanupCheck()
67100
{
68101
if($this->cleanupProbability === null) {
@@ -74,10 +107,13 @@ protected function cleanupCheck()
74107
}
75108
}
76109

110+
/**
111+
* Default cleanup implementation.
112+
*
113+
* @return void
114+
*/
77115
public function cleanup()
78-
{
79-
80-
}
116+
{}
81117

82118
/**
83119
* @param string $key

src/PHPixie/Cache/Drivers/Type/Memcached.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,19 @@ class Memcached extends Driver
1010
/** @var \Memcached */
1111
protected $client;
1212

13-
13+
/**
14+
* @inheritdoc
15+
*/
1416
public function hasItem($key)
1517
{
1618
$client = $this->client();
1719
$client->get($key);
1820
return $client->getResultCode() == \Memcached::RES_SUCCESS;
1921
}
2022

23+
/**
24+
* @inheritdoc
25+
*/
2126
public function getItems(array $keys = array())
2227
{
2328
if(empty($keys)) {
@@ -33,12 +38,18 @@ public function getItems(array $keys = array())
3338
return $this->buildItems($keys, $data);
3439
}
3540

41+
/**
42+
* @inheritdoc
43+
*/
3644
public function deleteItems(array $keys = array())
3745
{
3846
$this->client()->deleteMulti($keys);
3947
return true;
4048
}
4149

50+
/**
51+
* @inheritdoc
52+
*/
4253
public function clear()
4354
{
4455
$this->client()->flush();
@@ -65,6 +76,9 @@ public function saveItem($item, $expiresAt)
6576
return true;
6677
}
6778

79+
/**
80+
* @inheritdoc
81+
*/
6882
public function getItem($key)
6983
{
7084
$client = $this->client();
@@ -88,14 +102,31 @@ public function client()
88102
return $this->client;
89103
}
90104

105+
/**
106+
* Prepare server config, because in app config it could be defined w/o port or weight
107+
*
108+
* @param array $value
109+
* @return array
110+
*/
111+
protected function prepareServerConfig(array $value)
112+
{
113+
return $value + array(null, 11211, 1);
114+
}
115+
116+
/**
117+
* Build client
118+
*
119+
* @return \Memcached
120+
*/
91121
protected function buildClient()
92122
{
93123
$client = new \Memcached();
94124
$servers = $this->configData->getRequired('servers');
95125
foreach($servers as $key => $value) {
96-
$servers[$key] = array(null, 11211, 1) + $value;
126+
$servers[$key] = $this->prepareServerConfig($value);
97127
}
98128
$client->addServers($servers);
129+
99130
return $client;
100131
}
101132
}

src/PHPixie/Cache/Item.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,49 @@ public function __construct($key, $isHit, $value = null, $expiresAt = null)
1919
$this->expiresAt = $expiresAt;
2020
}
2121

22+
/**
23+
* @inheritdoc
24+
*/
2225
public function getKey()
2326
{
2427
return $this->key;
2528
}
2629

30+
/**
31+
* @inheritdoc
32+
*/
2733
public function get()
2834
{
2935
return $this->value;
3036
}
3137

38+
/**
39+
* @inheritdoc
40+
*/
3241
public function isHit()
3342
{
3443
return $this->isHit;
3544
}
3645

46+
/**
47+
* @inheritdoc
48+
*/
3749
public function set($value)
3850
{
3951
$this->value = $value;
4052
}
4153

54+
/**
55+
* @inheritdoc
56+
*/
4257
public function expiresAt($expiresAt)
4358
{
4459
$this->expiresAt = $expiresAt;
4560
}
4661

62+
/**
63+
* @inheritdoc
64+
*/
4765
public function expiresAfter($interval)
4866
{
4967
if($interval === null) {
@@ -59,6 +77,11 @@ public function expiresAfter($interval)
5977
$this->expiresAt = $expires->add($interval);
6078
}
6179

80+
/**
81+
* Get exiration datetime
82+
*
83+
* @return null|\DateTimeInterface
84+
*/
6285
public function getExpiresAt()
6386
{
6487
return $this->expiresAt;

tests/PHPixie/Tests/Cache/Driver/MemcachedTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,48 @@ class MemcachedTest extends DriverTest
1616
)
1717
)
1818
);
19+
20+
public function testServerConfig()
21+
{
22+
$driver = $this->cache->storage('default')->driver();
23+
24+
$params = array(
25+
array('127.0.0.1'),
26+
array('192.168.1.100'),
27+
array('192.168.1.100', 11212),
28+
array('192.168.1.100', 11212, 3),
29+
);
30+
$expected = array(
31+
array('127.0.0.1', 11211, 1),
32+
array('192.168.1.100', 11211, 1),
33+
array('192.168.1.100', 11212, 1),
34+
array('192.168.1.100', 11212, 3),
35+
);
36+
37+
foreach (array_keys($params) as $key) {
38+
$this->assertEquals(
39+
$expected[$key],
40+
$this->invokeMethod($driver, 'prepareServerConfig', array($params[$key]))
41+
);
42+
}
43+
}
44+
45+
/**
46+
* Call protected/private method of a class.
47+
*
48+
* @param object &$object Instantiated object that we will run method on.
49+
* @param string $methodName Method name to call
50+
* @param array $parameters Array of parameters to pass into method.
51+
* @link https://jtreminio.com/2013/03/unit-testing-tutorial-part-3-testing-protected-private-methods-coverage-reports-and-crap/
52+
*
53+
* @return mixed Method return.
54+
*/
55+
public function invokeMethod(&$object, $methodName, array $parameters = array())
56+
{
57+
$reflection = new \ReflectionClass(get_class($object));
58+
$method = $reflection->getMethod($methodName);
59+
$method->setAccessible(true);
60+
61+
return $method->invokeArgs($object, $parameters);
62+
}
1963
}

0 commit comments

Comments
 (0)