Skip to content

Commit b50fb76

Browse files
authored
Merge pull request #3 from php-elementary/memcached
Memcached
2 parents fd970ca + bb4ecb7 commit b50fb76

3 files changed

Lines changed: 64 additions & 6 deletions

File tree

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "elementary/mem-cache",
3-
"description": "Cache your data while PHP application is working.",
3+
"description": "Cache your data in Memcached.",
44
"license": "MIT",
55
"authors": [
66
{
@@ -17,6 +17,8 @@
1717
"require": {
1818
"php": ">=5.6",
1919
"phpunit/phpunit": ">=5.7",
20-
"elementary/runtime-cache": "^1.0.0"
20+
"psr/simple-cache": "^1.0",
21+
"elementary/runtime-cache": "^1.0.0",
22+
"elementary/singleton": "^1.0.0"
2123
}
2224
}

src/MemCache.php

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ class MemCache
2626
*/
2727
protected $servers = [];
2828

29+
/**
30+
* @var array
31+
*/
32+
protected $options = [];
33+
2934
/**
3035
* @link http://php.net/manual/en/memcached.add.php
3136
* @param string $key
@@ -318,9 +323,6 @@ public function getMemcached()
318323
{
319324
if ($this->memcached === null) {
320325
$this->setMemcached(new Memcached());
321-
322-
$servers = array_values($this->getServers());
323-
$this->memcached->addServers($servers);
324326
}
325327

326328
return $this->memcached;
@@ -335,6 +337,16 @@ public function setMemcached(Memcached $cache)
335337
{
336338
$this->memcached = $cache;
337339

340+
$servers = array_values($this->getServers());
341+
if ($servers) {
342+
$this->memcached->addServers($servers);
343+
}
344+
345+
$options = $this->getOptions();
346+
if ($options) {
347+
$this->memcached->setOptions($options);
348+
}
349+
338350
return $this;
339351
}
340352

@@ -386,7 +398,7 @@ public function setServers( array $servers)
386398

387399
$key = implode('', $server);
388400
if (!array_key_exists($key, $this->servers)) {
389-
$nServers[] = $servers;
401+
$nServers[] = $server;
390402
$this->servers[$key] = $server;
391403
}
392404
}
@@ -398,4 +410,29 @@ public function setServers( array $servers)
398410

399411
return $this;
400412
}
413+
414+
/**
415+
* @return array
416+
*/
417+
public function getOptions()
418+
{
419+
return $this->options;
420+
}
421+
422+
/**
423+
* @link http://php.net/manual/en/memcached.setoptions.php
424+
* @param array $options
425+
*
426+
* @return $this
427+
*/
428+
public function setOptions(array $options)
429+
{
430+
$this->options = array_merge($this->options, $options);
431+
432+
if ($this->memcached !== null && !empty($options)) {
433+
$this->memcached->setOptions($options);
434+
}
435+
436+
return $this;
437+
}
401438
}

tests/MemCacheTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ public function stats()
200200
*/
201201
public function getResult()
202202
{
203+
fwrite(STDOUT, "\n". __METHOD__);
204+
203205
$this->assertInternalType('integer', $this->getMemory()->getResultCode());
204206
$this->assertInternalType('string', $this->getMemory()->getResultMessage());
205207
}
@@ -214,6 +216,8 @@ public function getResult()
214216
*/
215217
public function servers()
216218
{
219+
fwrite(STDOUT, "\n". __METHOD__);
220+
217221
$mem = new MemCache();
218222
$mem->addServer('test', 123);
219223
$this->assertEquals([['test', 123, 0]], array_values($mem->getServers()));
@@ -225,6 +229,21 @@ public function servers()
225229
$this->assertEquals([], $mem->getServers());
226230
}
227231

232+
/**
233+
* @test
234+
* @covers ::getOptions
235+
* @covers ::setOptions
236+
*/
237+
public function options()
238+
{
239+
fwrite(STDOUT, "\n". __METHOD__);
240+
241+
$mem = new MemCache();
242+
$mem->setOptions(['test' => 123]);
243+
$mem->setOptions(['test' => 1234, 'test2' => 12345]);
244+
$this->assertEquals(['test' => 123, 'test2' => 12345], $mem->getOptions());
245+
}
246+
228247
protected function setUp()
229248
{
230249
$this->setMemory(new MemCache())

0 commit comments

Comments
 (0)