Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.

Commit 51571a7

Browse files
dexweierophinney
authored andcommitted
Added memcached
1 parent 2d0f369 commit 51571a7

File tree

3 files changed

+121
-2
lines changed

3 files changed

+121
-2
lines changed

docs/book/diagnostics.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,32 @@ use ZendDiagnostics\Check\Memcache;
240240
$checkLocal = new Memcache('127.0.0.1'); // default port
241241
$checkBackup = new Memcache('10.0.30.40', 11212);
242242
```
243+
244+
## Memcached
245+
246+
Attempt to connect to the given Memcached server.
247+
248+
```php
249+
<?php
250+
use ZendDiagnostics\Check\Memcached;
251+
252+
$checkLocal = new Memcached('127.0.0.1'); // default port
253+
$checkBackup = new Memcached('10.0.30.40', 11212);
254+
```
255+
256+
### MongoDb
257+
Check if connection to MongoDb is possible
258+
259+
````php
260+
<?php
261+
use ZendDiagnostics\Check\Mongo;
262+
263+
$mongoCheck = new Mongo('mongodb://127.0.0.1:27017');
264+
// and with user/password
265+
$mongoCheck = new Mongo('mongodb://user:[email protected]:27017');
266+
````
267+
268+
243269

244270
## MongoDb
245271

src/Check/Memcache.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Memcache extends AbstractCheck
3131
* @param int $port
3232
* @throws \InvalidArgumentException
3333
*/
34-
public function __construct($host, $port = 11211)
34+
public function __construct($host = '127.0.0.1', $port = 11211)
3535
{
3636
if (! is_string($host)) {
3737
throw new InvalidArgumentException(sprintf(
@@ -40,7 +40,7 @@ public function __construct($host, $port = 11211)
4040
));
4141
}
4242

43-
$port = (int)$port;
43+
$port = (int) $port;
4444
if ($port < 1) {
4545
throw new InvalidArgumentException(sprintf(
4646
'Invalid port number - expecting a positive integer',

src/Check/Memcached.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
/**
3+
* @see https://github.com/zendframework/zend-diagnostics for the canonical source repository
4+
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (https://www.zend.com)
5+
* @license https://github.com/zendframework/zend-diagnostics/blob/master/LICENSE.md New BSD License
6+
*/
7+
8+
namespace ZendDiagnostics\Check;
9+
10+
use InvalidArgumentException;
11+
use ZendDiagnostics\Result\Failure;
12+
use ZendDiagnostics\Result\Success;
13+
14+
/**
15+
* Check if MemCached extension is loaded and given server is reachable.
16+
*/
17+
class Memcached extends AbstractCheck
18+
{
19+
/**
20+
* @var string
21+
*/
22+
protected $host;
23+
24+
/**
25+
* @var int
26+
*/
27+
protected $port;
28+
29+
/**
30+
* @param string $host
31+
* @param int $port
32+
* @throws \InvalidArgumentException
33+
*/
34+
public function __construct($host = '127.0.0.1', $port = 11211)
35+
{
36+
if (! is_string($host)) {
37+
throw new InvalidArgumentException(sprintf(
38+
'Cannot use %s as host - expecting a string',
39+
gettype($host)
40+
));
41+
}
42+
43+
$port = (int)$port;
44+
if ($port < 1) {
45+
throw new InvalidArgumentException(sprintf(
46+
'Invalid port number - expecting a positive integer',
47+
gettype($host)
48+
));
49+
}
50+
51+
$this->host = $host;
52+
$this->port = $port;
53+
}
54+
55+
/**
56+
* @see ZendDiagnostics\CheckInterface::check()
57+
*/
58+
public function check()
59+
{
60+
if (! class_exists('Memcached', false)) {
61+
return new Failure('Memcached extension is not loaded');
62+
}
63+
64+
try {
65+
$memcached = new \Memcached();
66+
$memcached->addServer($this->host, $this->port);
67+
$stats = @$memcached->getStats();
68+
69+
if (! $stats ||
70+
! is_array($stats) ||
71+
! isset($stats[$this->host . ':' . $this->port]) ||
72+
($stats[$this->host . ':' . $this->port] === false)
73+
) {
74+
// Attempt a connection to make sure that the server is really down
75+
if (! @$memcached->getLastDisconnectedServer($this->host, $this->port)) {
76+
return new Failure(sprintf(
77+
'No memcached server running at host %s on port %s',
78+
$this->host,
79+
$this->port
80+
));
81+
}
82+
}
83+
} catch (\Exception $e) {
84+
return new Failure($e->getMessage());
85+
}
86+
87+
return new Success(sprintf(
88+
'Memcached server running at host %s on port %s',
89+
$this->host,
90+
$this->port
91+
));
92+
}
93+
}

0 commit comments

Comments
 (0)